我即将为COM互操作IStream编写一个Wrapper,以便需要标准.NET Stream的代码可以使用它。
然而,我发现这种事情之前可能已经完成了(尽管我自己也没能通过网络搜索找到它)。
所以我只是把它放在这里以防万一我要重新发明轮子。
注意我遇到了实现IStream包装.NET流的代码,但我需要反过来。
答案 0 :(得分:35)
确实如此,System.Runtime.InteropServices.ComTypes.IStream。样本包装器:
using System;
using iop = System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
public class ComStreamWrapper : System.IO.Stream {
private IStream mSource;
private IntPtr mInt64;
public ComStreamWrapper(IStream source) {
mSource = source;
mInt64 = iop.Marshal.AllocCoTaskMem(8);
}
~ComStreamWrapper() {
iop.Marshal.FreeCoTaskMem(mInt64);
}
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return true; } }
public override bool CanWrite { get { return true; } }
public override void Flush() {
mSource.Commit(0);
}
public override long Length {
get {
STATSTG stat;
mSource.Stat(out stat, 1);
return stat.cbSize;
}
}
public override long Position {
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override int Read(byte[] buffer, int offset, int count) {
if (offset != 0) throw new NotImplementedException();
mSource.Read(buffer, count, mInt64);
return iop.Marshal.ReadInt32(mInt64);
}
public override long Seek(long offset, System.IO.SeekOrigin origin) {
mSource.Seek(offset, (int)origin, mInt64);
return iop.Marshal.ReadInt64(mInt64);
}
public override void SetLength(long value) {
mSource.SetSize(value);
}
public override void Write(byte[] buffer, int offset, int count) {
if (offset != 0) throw new NotImplementedException();
mSource.Write(buffer, count, IntPtr.Zero);
}
}