c++dllcomsql-server-cesqlcedatareader

DllGetClassObject return "No such interface supported" while CoCreateInstance can find it successful


I want to use the library "sqlceoledb35.dll" to process .sdf db file without register. I know this dll is a COM dll and used in ADO.
But I can't get the target interface, it returns error "No such interface supported".
Here is the code:

    CoInitialize(nullptr); 
    HMODULE hmod = CoLoadLibrary((L"sqlceoledb35.dll"), true);
    DllGetClassObject_t pDllGetClassObject =(DllGetClassObject_t)GetProcAddress(hmod,        "DllGetClassObject");
    HRESULT hr=NOERROR;
    IDBInitialize *pIDBInitialize1=NULL;
    IDBInitialize *pIDBInitialize2=NULL;
    hr = pDllGetClassObject(CLSID_SQLSERVERCE_3_5, __uuidof(IUnknown), (void**)&pIDBInitialize1);
    hr = pDllGetClassObject(CLSID_SQLSERVERCE_3_5, IID_IDBInitialize, (void**)&pIDBInitialize2);

But in this code snippet, _uuidof(IUnknow)can return a interface success, but IID_IDBInitialize will fail(this IID can work in CoCreateInstance, you will see later.

This is another code which can work correctly in the same machine with the same interface IID:

    CoInitialize(nullptr); 
    hr = CoCreateInstance(  CLSID_SQLSERVERCE_3_5, 
                        0, 
                        CLSCTX_INPROC_SERVER, 
                        IID_IDBInitialize, 
                        (void**)&pIDBInitialize);

So anyone can help, So that the 1st code snippet can work?
So did the method CoCreateInstance do more work which is a key?


Solution

  • CoCreateInstance (for in-proc servers) works in two stages. First, it loads the DLL and calls DllGetClassObject with the CLSID you pass, asking for IClassFactory interface. Second, it calls IClassFactory::CreateInstance on the pointer thus obtained, with the IID you pass.

    The object that DllGetClassObject knows how to create - the class factory - does not normally itself implement any interfaces other than IClassFactory and, of course, IUnknown.