ATL and ADO Problem
-
I am writing an ATL component to do some data access using ATO. I want to pass a _RecordsetPtr pointer or a _ConnectionPtr to a ATL function. I use code like this:
STDMETHODIMP CMyAtlObject::ReadData(LPDISPATCH pAdoRecordset, BOOL *pbSuccess) { _RecordsetPtr pRecordsetPtr; if (FAILED(pAdoRecordset->QueryInterface(IID_IDispatch, (void**) &pRecordsetPtr))) { return S_FALSE; } // do somethign here with the recordset return S_OK; }
But this doesn't work. The recordset pointer gets closed during the function call. If I pass an _ConnectionPtr as LPDISPATCH, the pointer becomes invalid. So how can I pass valid ADO _RecordsetPtr's and _ConnectionPtr's to an ATL Object? -
I am writing an ATL component to do some data access using ATO. I want to pass a _RecordsetPtr pointer or a _ConnectionPtr to a ATL function. I use code like this:
STDMETHODIMP CMyAtlObject::ReadData(LPDISPATCH pAdoRecordset, BOOL *pbSuccess) { _RecordsetPtr pRecordsetPtr; if (FAILED(pAdoRecordset->QueryInterface(IID_IDispatch, (void**) &pRecordsetPtr))) { return S_FALSE; } // do somethign here with the recordset return S_OK; }
But this doesn't work. The recordset pointer gets closed during the function call. If I pass an _ConnectionPtr as LPDISPATCH, the pointer becomes invalid. So how can I pass valid ADO _RecordsetPtr's and _ConnectionPtr's to an ATL Object?Although I use OLE DB directly rather than ADO, unless the function explicitly requires a smart pointer, I'd be inclined to use a regular interface pointer instead, to save an AddRef and Release on construction/destruction of the temporary object. Steve S [This signature space available for rent]
-
Although I use OLE DB directly rather than ADO, unless the function explicitly requires a smart pointer, I'd be inclined to use a regular interface pointer instead, to save an AddRef and Release on construction/destruction of the temporary object. Steve S [This signature space available for rent]
So how do I do that?
-
So how do I do that?
Where does your LPDISPATCH come from in the first place? That's an IDispatch, so using QueryInterface(IID_IDISPATCH, ...) is a no-brainer, it should just AddRef(). Are you using #import to pull in the ADO stuff? To get the recordset ptr, you should use __uuidof(_Recordset) in place of IID_IDISPATCH. This will either fail or fill in your _RecordsetPtr properly. Probably need more context to help more. Steve S [This signature space available for rent]