INFO: Passing ADO Objects Between Visual Basic and Visual C++ID: Q190935
|
A common customer scenario is wanting to pass a Component Object Model (COM) object between a Visual C++ COM Server and a Visual Basic client. One example of this is passing an ActiveX Data Objects (ADO) recordset opened inside a C++-based Active Template Library (ATL) server to a Visual Basic client. Incorrect syntax within the Visual Basic client could return errors, such as the following:
0x80040002 (-2147467262) "No Such Interface Supported."
There are basically two kinds of procedures in Visual Basic, Function(s)
and Sub(programs). Either type of procedure, with arguments, can be called
with one of the following examples:
STDMETHODIMP CComDispatch::SetInterfacePtr(LPUNKNOWN* lpDisp)
{
_RecordsetPtr pRS;
IDispatch* pStream = NULL;
LPDISPATCH tempPtr;
HRESULT hr = (*lpDisp)->QueryInterface(IID_IDispatch,
(void**)& pStream);
hr = pStream->;QueryInterface(__uuidof(_RecordsetPtr), (void** &pRS);
}
Now use the following Visual Basic code to create an ADO Recordset and pass
it to the ATL Server:
Dim cmdByRoyalty As ADODB.Recordset
Set cmdByRoyalty = New ADODB.Recordset
Dim MyObj As ATLADOSERVERLib.ComDispatch
Set MyObj = New ATLADOSERVERLib.ComDispatch
MyObj.SetInterfacePtr (cmdByRoyalty) ' By value or reference?
STDMETHODIMP CComDispatch::SetInterfacePtr(LPUNKNOWN* lpDisp) is expecting a pointer and in order to pass a pointer with Visual Basic you need to pass the parameter by reference.
because the object passed to SetInterfacePtr is not an actual Recordset object. Instead the object is an instance of the ADO Fields Collection, which does not support the Recordset interface. The following code displays an example:0x80040002 Interface Not Supported
call MyObj.SetInterfacePtr (cmdByRoyalty)
MyObj.SetInterfacePtr cmdByRoyalty
The KB sample Q186387 "Ado2atl.exe Returns ADO Interfaces from COM" has a complete working sample of passing and ADO interface from a ATL COM object to VB.
Additional query words:
Keywords : kbDSupport
Version : WINDOWS:1.0,1.5,2.0
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: May 24, 1999