QueryInterface -- How come this code doesn't work?
-
Here is the code:
// this line gets a valid ADO database connection object
_ConnectionPtr pConnection = m_AdoDatabase.GetActiveConnection();// Now I will get a pUknown pointing to the database connection
IUnknown* pUnk;// this is successful
HRESULT hr = pConnection->QueryInterface(IID_IDispatch, (void**) &pUnk);// Now I will attempt to get an ADO connection object from pUnknown
_ConnectionPtr pConnection2;// this line fails
hr = pConnection->QueryInterface(__uuidof(Connection), (void**) &pConnection2);if (FAILED(hr))
{
// this is where the failure is!
// hr is returned with a value E_NOINTERFACE
TRACE("Failed to get pointer the second time.\n");
TRACE("Error: %s\n\n", GetComError(hr));
}Shouldn't I be able to go from an ADO Connection object, to IUnknown, and then back again? How do I get back my ADO Connection from IUnkown?
-
Here is the code:
// this line gets a valid ADO database connection object
_ConnectionPtr pConnection = m_AdoDatabase.GetActiveConnection();// Now I will get a pUknown pointing to the database connection
IUnknown* pUnk;// this is successful
HRESULT hr = pConnection->QueryInterface(IID_IDispatch, (void**) &pUnk);// Now I will attempt to get an ADO connection object from pUnknown
_ConnectionPtr pConnection2;// this line fails
hr = pConnection->QueryInterface(__uuidof(Connection), (void**) &pConnection2);if (FAILED(hr))
{
// this is where the failure is!
// hr is returned with a value E_NOINTERFACE
TRACE("Failed to get pointer the second time.\n");
TRACE("Error: %s\n\n", GetComError(hr));
}Shouldn't I be able to go from an ADO Connection object, to IUnknown, and then back again? How do I get back my ADO Connection from IUnkown?
Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.
-
Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.
-
Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.
-
Haven't checked, but may be __uuidof(Connection) returns the CLSID of the Connection object, while what is expected in queryinterface() is the IID of the interface.
So how do I get the IID?
-
So how do I get the IID?
I believe either __uuidof(_Connection) or __uuidof(_ConnectionPtr) is better. Make sure to check that the return IID is non-NULL by the way.