C# IStream implemenation of IStream

本文关键字:IStream of implemenation | 更新日期: 2023-09-27 18:25:15

首先,这与此没有重复。COM互操作IStream的包装类是否已经存在?因为我需要在另一个方向上实现。我需要创建一个从IO.Stream到IStream的IStream实现。但在我开始尝试这样做之前,我想问一下是否有人知道已经存在的实现或任何关于它的文章。我在.net框架中找不到任何东西,谷歌只给了我从IStream到IO.Stream的实现结果。有人给我一个好的提示吗?我真的不知道如何开始,因为第一个成员(克隆->创建一个新的流对象,该对象引用与原始流相同的字节,但提供了一个指向这些字节的单独查找指针)让我很麻烦。我不知道如何基于IO.Stream.

C# IStream implemenation of IStream

p>最后,我自己完成了(可以随意复制和修改):
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("0000000c-0000-0000-C000-000000000046")]
public interface IStream
{
    [PreserveSig]
    HResult Read([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] [Out] byte[] pv, int cb, IntPtr pcbRead);
    [PreserveSig]
    HResult Write([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pv, int cb, IntPtr pcbWritten);
    [PreserveSig]
    HResult Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition);
    [PreserveSig]
    HResult SetSize(long libNewSize);
    HResult CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten);
    [PreserveSig]
    HResult Commit(int grfCommitFlags);
    [PreserveSig]
    HResult Revert();
    [PreserveSig]
    HResult LockRegion(long libOffset, long cb, int dwLockType);
    [PreserveSig]
    HResult UnlockRegion(long libOffset, long cb, int dwLockType);
    [PreserveSig]
    HResult Stat(out comtypes.STATSTG pstatstg, int grfStatFlag);
    [PreserveSig]
    HResult Clone(out IStream ppstm);
}
    /// <summary>
    /// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms752876(v=vs.85).aspx
    /// </summary>
    public class ComStream : Stream, IStream
    {
        private Stream _stream;
        public ComStream(Stream stream)
            : this(stream, true)
        {
        }
        internal ComStream(Stream stream, bool sync)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (sync)
            {
                stream = Stream.Synchronized(stream);
            }
            _stream = stream;
        }
        HResult IStream.Clone(out IStream ppstm)
        {
            //ComStream newstream = new ComStream(_stream, false);
            //ppstm = newstream;
            ppstm = null;
            return HResult.E_NOTIMPL;
        }
        HResult IStream.Commit(int grfCommitFlags)
        {
            return HResult.E_NOTIMPL;
        }
        HResult IStream.CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            return HResult.E_NOTIMPL;
        }
        HResult IStream.LockRegion(long libOffset, long cb, int dwLockType)
        {
            return HResult.E_NOTIMPL;
        }
        HResult IStream.Read(byte[] pv, int cb, IntPtr pcbRead)
        {
            if (!CanRead)
                throw new InvalidOperationException("Stream not readable");
            int read = Read(pv, 0, cb);
            if (pcbRead != IntPtr.Zero)
                Marshal.WriteInt64(pcbRead, read);
            return HResult.S_OK;
        }
        HResult IStream.Revert()
        {
            return HResult.E_NOTIMPL;
        }
        HResult IStream.Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
        {
            SeekOrigin origin = (SeekOrigin)dwOrigin; //hope that the SeekOrigin enumeration won't change
            long pos = Seek(dlibMove, origin);
            if (plibNewPosition != IntPtr.Zero)
                Marshal.WriteInt64(plibNewPosition, pos);
            return HResult.S_OK;
        }
        HResult IStream.SetSize(long libNewSize)
        {
            return HResult.E_NOTIMPL;
        }
        HResult IStream.Stat(out comtypes.STATSTG pstatstg, int grfStatFlag)
        {
            pstatstg = new comtypes.STATSTG();
            pstatstg.cbSize = Length;
            return HResult.S_OK;
        }
        HResult IStream.UnlockRegion(long libOffset, long cb, int dwLockType)
        {
            return HResult.E_NOTIMPL;
        }
        HResult IStream.Write(byte[] pv, int cb, IntPtr pcbWritten)
        {
            if (!CanWrite)
                throw new InvalidOperationException("Stream is not writeable.");
            Write(pv, 0, cb);
            if (pcbWritten != null)
                Marshal.WriteInt32(pcbWritten, cb);
            return HResult.S_OK;
        }
        public override bool CanRead
        {
            get { return _stream.CanRead; }
        }
        public override bool CanSeek
        {
            get { return _stream.CanSeek; }
        }
        public override bool CanWrite
        {
            get { return _stream.CanWrite; }
        }
        public override void Flush()
        {
            _stream.Flush();
        }
        public override long Length
        {
            get { return _stream.Length; }
        }
        public override long Position
        {
            get
            {
                return _stream.Position;
            }
            set
            {
                _stream.Position = value;
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return _stream.Read(buffer, offset, count);
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            return _stream.Seek(offset, origin);
        }
        public override void SetLength(long value)
        {
            _stream.SetLength(value);
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            _stream.Write(buffer, offset, count);
        }
        protected override void Dispose(bool disposing)
        {
            if (_stream != null)
            {
                _stream.Dispose();
                _stream = null;
            }
        }
    }