Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
CreateStreamOnHGlobal (ole32)

C# Signature:

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:




Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

K. Lazar - C# -

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");

        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.";
        {   // 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
                 fClearDirty = true;
                 pPersistStream.Save(comStream, fClearDirty);
            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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version