none
VB.NET & COM (IPersistStream) RRS feed

  • Question

  • I have a COM object which supports IPersistStream interface.
    Is there any a simple method to save state of object into a file in VB.NET?
    Thanks!


    • Edited by riazantsev Thursday, May 11, 2017 11:58 AM
    Thursday, May 11, 2017 11:53 AM

All replies

  • What kind of object are you referring to?

    Stream/Storage is compound document (OLE) technology, which is a bit dated and not often used. Perhaps you could better explain what you are trying to do.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, May 11, 2017 12:18 PM
  • Yes, Paul. It's old OLE technology. I have a VB.net client and I have to use object was written on C++. 

                Dim len As _ULARGE_INTEGER
                Dim mPersistStreamy As IPersistStream = CType(doc, IPersistStream)
                mPersistStreamy.GetSizeMax(len)

    Here I got pointer to my interface IPersistStream  (mPersistStreamy). It's available.
    What is next? How to get global stream object and save it to file?

    Or is there any other way?

    • Edited by riazantsev Thursday, May 11, 2017 1:15 PM
    Thursday, May 11, 2017 1:14 PM
  • I posted this link to a recent question about this technology. You may want to take a look at it:

    https://sourceforge.net/projects/openmcdf/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, May 11, 2017 1:26 PM
  • Hi rizantsev,

    You may take a look at this:

    https://www.amyuni.com/forum/viewtopic.php?f=11&t=1638

    Hope it helps.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 12, 2017 8:24 AM
    Moderator
  • Thanks for help!

    It's only test code

    Private Property doc As 'COM object

    Public Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As IntPtr, ByVal fDeleteOnRelease As Boolean, ByRef ppstm As ComTypes.IStream) As Long

        Private Sub Button_Save(sender As Object, e As EventArgs) Handles Button3.Click
            If Not doc Is Nothing Then
                Dim len As _ULARGE_INTEGER
                Dim pDocPersistStream As IPersistStream = CType(doc, IPersistStream)
                pDocPersistStream.GetSizeMax(len)

                Dim hGlobal As IntPtr = Marshal.AllocHGlobal(CInt(len.QuadPart))
                Dim pStream As ComTypes.IStream = Nothing
                Dim filePath As String = "f:\test.txt"

                CreateStreamOnHGlobal(hGlobal, 0, pStream)
                pDocPersistStream.Save(pStream, 1)

                Dim bdata As Byte()
                Dim bytes As IntPtr

                pStream.Seek(0, 0, bytes)
                ReDim bdata(CInt(len.QuadPart))
                Marshal.Copy(hGlobal, bdata, 0, CInt(len.QuadPart))

                ' Save the data to a file into a VB.NET stream 
                Dim fs As New FileStream(filePath, FileMode.Create, FileAccess.Write)
                Dim objSR As New BinaryWriter(fs)
                objSR.Write(bdata)
                fs.Close()
                Marshal.FreeHGlobal(hGlobal)
            Else
                Dim message As String = _
                "There is't the COM object"
                Dim caption As String = "Form"
                Dim result = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End Sub

        '--------------------------------------------------------------------------------

        Private Sub Button_Load(sender As Object, e As EventArgs) Handles Button4.Click
            If Not doc Is Nothing Then
                Dim len As _ULARGE_INTEGER
                Dim pDocPersistStream As IPersistStream = CType(doc, IPersistStream)
                pDocPersistStream.GetSizeMax(len)

                Dim filePath As String = "f:\test.txt"

                Dim bdata As Byte()
                Dim bytes As IntPtr

                ' Load the data from the file  
                ReDim bdata(CInt(len.QuadPart))
                Dim fs As New FileStream(filePath, FileMode.Open)
                Dim objSR As New BinaryReader(fs)
                objSR.Read(bdata, 0, (CInt(len.QuadPart)))

                Dim hGlobal As IntPtr = Marshal.AllocHGlobal(CInt(len.QuadPart))
                Dim pStream As ComTypes.IStream = Nothing

                CreateStreamOnHGlobal(hGlobal, 0, pStream)
                pStream.Seek(0, 0, bytes)
                Marshal.Copy(bdata, 0, hGlobal, CInt(len.QuadPart))
                pDocPersistStream.Load(pStream)
                Marshal.FreeHGlobal(hGlobal)
                fs.Close()
            Else
                Dim message As String = _
                "There is't the COM object"
                Dim caption As String = "Form"
                Dim result = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End Sub

    Best Regards,

    Igor


    • Edited by riazantsev Friday, May 12, 2017 11:17 AM
    Friday, May 12, 2017 9:47 AM
  • Hi riazantsev,

    Since your interop method named CreateStreamOnHGlobal, which return an IStream object. we could store an IStream to a file, like this:

    Dim myPtr As System.IntPtr = (IntPtr) - 1
    Using fs As New FileStream("f:\test.txt", FileMode.OpenOrCreate)
    	Dim buffer As Byte() = New Byte(8191) {}
    	While myPtr.ToInt32() > 0
    		pStream.Read(buffer, buffer.Length, myPtr)
    		fs.Write(buffer, 0, myPtr.ToInt32())
    	End While
    End Using

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 16, 2017 9:13 AM
    Moderator