Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Last active January 8, 2021 02:25
Show Gist options
  • Save kumatti1/1d9d34068ef92fd65ca6 to your computer and use it in GitHub Desktop.
Save kumatti1/1d9d34068ef92fd65ca6 to your computer and use it in GitHub Desktop.
hoge.idl
[
uuid(0C87DFCC-B6EF-40B6-B0F8-3A23663664D1)
]
library test
{
typedef struct
{
long signature;
BYTE padding[48];
short pid;
} COGETSERVERPID_OBJREFHDR;
typedef struct {
long Data1;
short Data2;
short Data3;
BYTE Data4[8];
} UUID;
[
dllname("Ole32.dll")
]
module ole32
{
[entry("CreateStreamOnHGlobal")]
long _stdcall CreateStreamOnHGlobal(
[in] long hGlobal,
[in] long fDeleteOnRelease,
[in, out] IUnknown **ppstm);
[entry("CoMarshalInterface")]
long _stdcall CoMarshalInterface(
[in] IUnknown *pStm,
[in] UUID *riid,
[in] IUnknown *pUnk,
[in] long dwDestContext,
[in] long pvDestContext,
[in] long mshlflags
);
[entry("GetHGlobalFromStream")]
long _stdcall GetHGlobalFromStream(
[in] IUnknown *pstm,
[in, out] long *phglobal
);
[entry("IIDFromString")]
long _stdcall IIDFromString(
[in] LPWSTR lpsz,
[in, out] UUID *lpiid
);
[entry("CoReleaseMarshalData")]
long _stdcall CoReleaseMarshalData(
[in] IUnknown *pStm
);
}
[
dllname("Kernel32.dll")
]
module Kernel32
{
[entry("GlobalLock")]
long _stdcall GlobalLock(
[in] long hMem
);
[entry("GlobalUnlock")]
long _stdcall GlobalUnlock(
[in] long hMem
);
[entry("RtlMoveMemory")]
void _stdcall MoveMemory(
[in, out] void *Destination,
[in, out] void *Source,
[in] long Length
);
}
[
dllname("Shlwapi.dll")
]
module Shlwapi
{
[entry("IStream_Size")]
long _stdcall IStream_Size(
[in] IUnknown *pstm,
[in, out] long *pui
);
}
[dllname("Constants")]
module Constants
{
const LPSTR IID_IUnknown = "{00000000-0000-0000-C000-000000000046}";
};
};
Option Explicit
Private Declare PtrSafe _
Function AtlFreeMarshalStream Lib "atl.dll" ( _
ByVal pStream As IUnknown _
) As Long
Sub hoge()
Dim hr&, iid As UUID
Dim stm As IUnknown
Dim unk As IUnknown
Dim st As COGETSERVERPID_OBJREFHDR
Dim p&, hg&
Set unk = Application
hr = IIDFromString(IID_IUnknown, iid)
hr = CreateStreamOnHGlobal(0, 1, stm)
hr = CoMarshalInterface(stm, iid, unk, 3, 0, 0)
hr = GetHGlobalFromStream(stm, hg)
p = GlobalLock(hg)
MoveMemory st, ByVal p, LenB(st)
If st.Signature = &H574F454D Then
MsgBox st.pid
End If
GlobalUnlock hg
hr = AtlFreeMarshalStream(stm)
Debug.Print Hex(hr)
End Sub
Option Explicit
Private Declare PtrSafe _
Function IStream_Reset Lib "Shlwapi.dll" ( _
ByVal pstm As IUnknown _
) As Long
Sub hoge2()
Dim hr&, iid As UUID
Dim stm As IUnknown
Dim unk As IUnknown
Dim st As COGETSERVERPID_OBJREFHDR
Dim p&, hg&
Set unk = Application
hr = IIDFromString(IID_IUnknown, iid)
hr = CreateStreamOnHGlobal(0, 1, stm)
hr = CoMarshalInterface(stm, iid, unk, 3, 0, 0)
hr = GetHGlobalFromStream(stm, hg)
p = GlobalLock(hg)
MoveMemory st, ByVal p, LenB(st)
If st.Signature = &H574F454D Then
MsgBox st.pid
End If
GlobalUnlock hg
hr = IStream_Reset(stm)
Debug.Print Hex(hr)
hr = CoReleaseMarshalData(stm)
Debug.Print Hex(hr)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment