我正在寻找一种聪明的方式发送"参考"各种COM对象 - 例如,对Excel工作簿的引用,或稍后对范围,工作表等的引用 - 通过VBA到另一个进程。我目前正在探索此处建议的方式:Passing a reference to a COM object accross processes via IPC?。
简而言之,我试图调用CoMarshalInterface()来将Workbook引用封送到流中(稍后,我会将该流中的字节发送到另一个将再次解组的进程)。 / p>
然而,我似乎无法使其发挥作用; CoMarshalInterface()在下面的情况下返回E_INVALIDARGS。有人会知道这里出了什么问题吗?
Declare Function IIDFromString Lib "ole32" (ByVal lpsz As Long, ByRef lpiid As UUID) As Long
Declare Function CoMarshalInterface Lib "ole32" (_
ByRef pStream As Variant, _
riid As Variant, _
pUnknown As Variant, _
dwWestContext As Long, _
pvDestContextPtr As Long, _
mshflags As Long _
) As Long
Public Type UUID 'GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Const IID_WORKBOOK As String = "{000208DA-0000-0000-C000-000000000046}"
Const MSHCTX_LOCAL As Long = 0
Const MSHLFLAGS_TABLESTRONG = 1
Const S_OK = &H0
Const E_INVALIDARG = &H80070057
Public Sub test()
Dim iid As UUID
Call IIDFromString(StrPtr(IID_WORKBOOK), iid)
Dim oStream As Object
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1 ' Binary
Dim wb As IUnknown
Set wb = ThisWorkbook
Dim result As Long
result = CoMarshalInterface(oStream, VarPtr(iid), ObjPtr(wb), MSHCTX_LOCAL, 0, MSHLFLAGS_TABLESTRONG)
' Result is E_INVALIDARG
Debug.Print "result: 0x" & Hex(result)
oStream.Close
End Sub