如何从 IStream 读取到 MemoryStream?

问题描述

我有一个要写入 MemoryStream 的 IStream。 IStreamWrapper 类继承自我在在线研究中发现的 Stream。

我尝试了下面的代码,但它总是返回 0 作为读取的字节。

Dim IStreamObject = TryCast(Marshal.GetobjectForIUnkNown(someStorage.unionmember),IStream)
Dim Wrappedistream As IStreamWrapper = New IStreamWrapper(IStreamObject)
Dim length = Wrappedistream.Length ' this returns the correct length of the stream '

Dim buffer As Byte() = New Byte(length - 1) {}
Dim someTestOutput = Wrappedistream.Read(buffer,length) ' this is always zero.

这是来自 IStreamWrapper.Read() 方法代码

Public Overrides Function Read(ByVal buffer As Byte(),ByVal offset As Integer,ByVal count As Integer) As Integer
    If offset <> 0 Then Throw New NotSupportedException("Only 0 offset is supported!")
    If buffer.Length < count Then Throw New NotSupportedException("Buffer is not large enough!")
    Dim bytesRead As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(GetType(Integer)))

    Try
        stream.Read(buffer,count,bytesRead)
        Return Marshal.ReadInt32(bytesRead)
    Finally
        Marshal.FreeCoTaskMem(bytesRead)
    End Try
End Function

C# 或 VB.net 代码的答案对我来说很好。 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)