Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

OleSaveToStream (ole32)
 
.

    [System.Runtime.InteropServices.DllImport("OLE32.DLL", EntryPoint = "CreateStreamOnHGlobal")]
    // Create a COM stream from a pointer in unmanaged memory
    extern public static int CreateStreamOnHGlobal(IntPtr ptr, bool delete, out System.Runtime.InteropServices.ComTypes.IStream pOutStm);

    [DllImport("OLE32.DLL", ExactSpelling = true, PreserveSig = false)]
    private static extern void OleSaveToStream(IPersistStreamInit pPStm, IStream pStm);

       public int ByteSizeOfIPStreamInit(IPersistStreamInit pPersistStream)
       {
         int     iSzOfStreamInBytes=0;
         IntPtr  ptrIStream = IntPtr.Zero;
         IStream IPtrStream;

         if (CreateStreamOnHGlobal(ptrIStream, true, out IPtrStream) == 0)
         {
              OleSaveToStream(pPersistStream,IPtrStream);
              iSzOfStreamInBytes = IStreamSizeRead(IPtrStream);
         }
         return (iSzOfStreamInBytes * 2); // (Multiply By 2 because Int32 is 4 bytes; Use caution.)
         return (iSzOfStreamInBytes);
       }

    public int IStreamSizeRead(IStream pOutStm)
    {
        pOutStm.Seek(0, 0, IntPtr.Zero);// Get size of the stream and read its contents to a byte array.
        pOutStm.Seek(0, 0, IntPtr.Zero);       // Get the size of the stream and read its contents to a byte array.
        System.Runtime.InteropServices.ComTypes.STATSTG fileinfo;
        pOutStm.Stat(out fileinfo, 0);
        byte[] data = new byte[fileinfo.cbSize];
        int sizeOfInt32 = Marshal.SizeOf(typeof(int));
        IntPtr pRead = Marshal.AllocHGlobal(sizeOfInt32);
        pOutStm.Read(data, data.Length, pRead);
        int read = Marshal.ReadInt32(pRead);
        Marshal.FreeHGlobal(pRead);
        return (data.Length);
    }

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions