[DllImport("ole32.dll")]
static extern int CreateStreamOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease,
out UCOMIStream ppstm);
Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As IntPtr, ByVal fDeleteOnRelease As Boolean, ByRef ppstm As UCOMIStream) As Long
None.
None.
Please add some!
Please add some!
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");
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;}
}