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

CreateStreamOnHGlobal (ole32)
 
.
Summary

C# Signature:

[DllImport("ole32.dll")]
static extern int CreateStreamOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease,
   out UCOMIStream ppstm);

VB.NET Signature:

    Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As IntPtr, ByVal fDeleteOnRelease As Boolean, ByRef ppstm As UCOMIStream) As Long

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
From
K. Lazar - C# - kentscode@yahoo.com

Use this signature

       [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);
    [System.Runtime.InteropServices.DllImport("OLE32.DLL", EntryPoint = "GetHGlobalFromStream")]
    public extern static void GetHGlobalFromStream(IStream stm, ref IntPtr hGlobal);

    public bool PersistLoadComObject(object objComCtrl, string strGUID, byte[] buffer)
    {
        IStream comStream;
        IntPtr  pIPStream = IntPtr.Zero;
        IntPtr  hGlobalMemHandle = (IntPtr)null;
        IntPtr  pIUnk = IntPtr.Zero;
        bool    bRetc=true;
        bool    fClearDirty;
        Int32   iUseCount;
        int     iResult;
        int     S_OK = 0;
        int     E_FAIL = unchecked((int)0x80004005);
        int     E_NOINTERFACE = unchecked((int)0x80004002);
        Guid    IID_IPersistStreamInit = new Guid("7FD52380-4E07-101B-AE2D-08002B2EC713");

From
K. Lazar - C#

    public bool PersistLoadComObject(object objComCtrl, string strGUID, byte[] buffer)
    {
        IStream comStream;
        IntPtr  pIPStream = IntPtr.Zero;
        IntPtr  hGlobalMemHandle = (IntPtr)null;
        IntPtr  pIUnk = IntPtr.Zero;
        bool    bRetc=true;
        bool    fClearDirty;
        Int32   iUseCount;
        int     iResult;
        int     S_OK = 0;
        int     E_FAIL = unchecked((int)0x80004005);
        int     E_NOINTERFACE = unchecked((int)0x80004002);
        Guid    IID_IPersistStreamInit = new Guid("7FD52380-4E07-101B-AE2D-08002B2EC713");

        if (Marshal.IsComObject((object)objComCtrl))
        {
        pIUnk = Marshal.GetIUnknownForObject((object)objComCtrl); // returns a pointer to a pointer of the object’s IUnknown Interface
        //Int32 usecount1 = Marshal.AddRef(pIUnk);  // increments the use count
        if (pIUnk == IntPtr.Zero)
        {
            ErrorMsg = "GetIUnknownForObject was zero.";
        }
        else
        {   // get a pointer to IPersistStreamInit interface
            iResult = Marshal.QueryInterface(pIUnk, ref IID_IPersistStreamInit, out pIPStream); // returns a pointer to a pointer for a specified Interface
            if (iResult == E_NOINTERFACE)
            {
            ErrorMsg = "No such interface supported";
            bRetc = false;
            }
            else if (iResult == E_FAIL)
            {
            ErrorMsg = "PersistStreamInit QueryInit failed.";
            bRetc = false;
            }
            else if (iResult == S_OK)
            {
            IPersistStreamInit pPersistStream = (IPersistStreamInit)Marshal.GetObjectForIUnknown(pIPStream);
            int SizeOfIstream = ByteSizeOfIPStreamInit(pPersistStream) * 2;  // two bytes per value -- See my OleSaveToStream (ole32)              
            if (CheckGUIDS(pPersistStream, strGUID))
            {
                IntPtr nativePtr = Marshal.AllocHGlobal(SizeOfIstream); //buffer.Length); // allocate space on the native heap
                Marshal.Copy(buffer, 0, nativePtr, SizeOfIstream);      // copy byte array to native heap
                if (CreateStreamOnHGlobal(nativePtr, true, out comStream) == S_OK) // Create IStream from allocated memory that has buffer data
                {
                 pPersistStream.Load(comStream);
                 fClearDirty = true;
                 pPersistStream.Save(comStream, fClearDirty);
                }
            }
            Marshal.Release(pIPStream);
            }
            iUseCount = Marshal.Release(pIUnk); // decrements the use count
        }
        }
        return (bRetc);
    }

     public bool CheckGUIDS(IPersistStreamInit pPersistStream, string strCtrlGUID)
     {
          bool retc = true;
          Guid ptrClsId = new Guid();
          pPersistStream.GetClassID(ref ptrClsId);
          string strStreamGUID = ptrClsId.ToString().ToUpper();

          if (strStreamGUID != strCtrlGUID.ToUpper())
          {
          _strErrorMsg = strStreamGUID + " " + strCtrlGUID + " mismatch";
          retc = false;
          }
          return (retc);
     }

     public string ErrorMsg
     {
          get {return _strErrorMsg;}
          set {_strErrorMsg = value;}
     }

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