HOWTO: Pass an Automation Object from VB to a C/C++ DLLID: Q181444
|
This article demonstrates how to pass an automation object from Microsoft Visual Basic to a C/C++ DLL.
The basic concept is to pass your automation object "ByVal As Object", expect an IUnknown pointer in the DLL and then call IUnknown::QueryInterface() for the IDispatch interface.
// Helper message function...
void ShowMsg(char *msg, HRESULT hr) {
char buf[1024];
if((long)hr) {
sprintf(buf, "%s, HRESULT = %08lx", msg, (long)hr);
}
else {
sprintf(buf, "%s", msg);
}
::MessageBox(NULL, buf, "C/C++ DLL message",
MB_SETFOREGROUND | MB_OK);
}
// The exported function, called from Microsoft Visual Basic...
void _stdcall talkToObject(IUnknown *pUnk) {
// QueryInterface for a IDispatch pointer...
IDispatch *pDisp;
HRESULT hr = pUnk->QueryInterface(IID_IDispatch,
(void **)&pDisp);
if(FAILED(hr)) {
ShowMsg("QueryInterface() failed", hr);
}
else {
ShowMsg("We got the dispatch pointer!!!", hr);
// Attach dispatch to a COleDispatchDriver class.
COleDispatchDriver disp(pDisp); // Uses constructor
// Set visible to FALSE...
static BYTE parms[] = VTS_BOOL;
disp.InvokeHelper(0x17, DISPATCH_PROPERTYPUT, VT_EMPTY,
NULL, parms, FALSE);
ShowMsg("Microsoft Word 97 shouldn't be visible now.", 0);
// Set visible to TRUE...
disp.InvokeHelper(0x17, DISPATCH_PROPERTYPUT, VT_EMPTY,
NULL, parms, TRUE);
ShowMsg("Microsoft Word 97 should now be visible again!",
0);
}
}
talkToObject
Private Declare Sub talkToObject Lib "vcvbdll.dll" ( _
ByVal pUnk As Object)
Private Sub Command1_Click()
Dim obj As Object
' Start automation to Microsoft Word 97.
Set obj = CreateObject("Word.Application")
' Make Microsoft Word 97 visible.
obj.Visible = True
MsgBox "Preparing to call into C/C++ dll..."
' Pass automation interface to C/C++ dll.
talkToObject obj
' Close Microsoft Word 97.
obj.Quit
End Sub
For more information about VC++ OLE Automation or COleDispatchDriver,
search the VC++ online help for "OLE Automation" or "COleDispatchDriver."
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Joe
Crump, Microsoft Corporation
© Microsoft Corporation 1998, All Rights Reserved.
Contributions by Joe Crump, Microsoft Corporation
Keywords : kbcode kbole kbAutomation kbMFC kbVC400 kbVC500 kbVC600
Version : WIN95:4.0; WINNT:4.0,5.0,6.0
Platform : Win95 winnt
Issue type : kbhowto
Last Reviewed: July 21, 1999