微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 IStream 读取到 MemoryStream?

如何解决如何从 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 举报,一经查实,本站将立刻删除。