1
votes

I have hooked the cocreateinstance() function. When it's called with a specific CLSID, I want to use my dll instead the dll system.

So here is my code :

HOOK_CoCreateInstance(rclsid,pUnkOuter,dwClsContext,riid,*ppv){
    ...
    if(myCLSID){
    module = LoadLibrary(mydll);
    dllGetClassObject = (FUNC)GetProcAddress(module,"DllGetClassObject");
    hr = dllGetClassObject(rclsid, IID_IClassFactory, &pClassFactory);
    hr = pClassFactory->CreateInstance(NULL,IID_IUnknown, (void**)&data_source);
    return hr;
    }
    else{
        hr = CoCreateInstanceReal(rclsid,pUnkOuter,dwClsContext,riid,ppv);
        return hr;
    }
}

But it's not working.

I think the problem is in pClassFactory::CreateInstance(), with the second parameter : I don't know how to retrieve automatically the REFIID of my dll. And if I use riid it's not working either.

So if anyone has an idea, Thanks !

1
What is not working exactly if you use riid? - sharptooth
If I use riid, my dll is loaded but the system dll too. - Chicago
At what point is system dll loaded? - sharptooth

1 Answers

0
votes

If you want to follow proper COM conventions, you'll need to handle the CoCreateInstance parameters correct (as documented here).

The __in REFIID riid parameter is the GUID of the interface you want to use, not the DLL itself. The CLSID parameter is the class of the object, which you should know ahead of time. Because you want to return the expected interface, you really only need to know the CLSID of your new implementation (coclass) and call using that.

A simpler, but not quite COM-spec, method would be to export a factory from your DLL:

__declspec(dllexport) MyObject * CreateObject() 
{
    return new MyObject();
}

and call that from your wrapper:

HOOK_CoCreateInstance(rclsid,pUnkOuter,dwClsContext,riid,*ppv)
{
    if(myCLSID)
    {
        module = LoadLibrary(mydll);
        dllCreate = (FUNC)GetProcAddress(module,"CreateObject");
        *ppv = dllCreate();
        return S_OK;
    } else {
        hr = CoCreateInstanceReal(rclsid,pUnkOuter,dwClsContext,riid,ppv);
        return hr;
    }
}