Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than ole32, prefix the name with the module name and a period.
CreateStreamOnHGlobal (ole32)
.
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!
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");
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 string ErrorMsg
{
get {return _strErrorMsg;}
set {_strErrorMsg = value;}
}
The CreateStreamOnHGlobal API
3/12/2010 7:58:14 AM - -151.204.185.153
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).