Programmatically registering and un-registering a COM DLL
Avishkar Autar · Jul 23 2008 · Win32 Platform
Playing around with WebKit a bit and stumbled across the headaches of working with a COM DLL. WebKit uses COM interfaces, so an application using a WebKit DLL must register the DLL. COM DLL registration is system-wide (i.e. all calls will be directed to a single DLL file), and this creates a problem if an application wants to use a DLL of a different version or a custom one (assuming renaming all the interfaces is not a viable option).
A simple solution to this seems to be to register and unregister the DLL programmatically. By doing so, any existing (or non-existent) registration is ignored and the exact DLL file the application needs is registered and used. The registration is still system-wide, but if other applications follow the same policy there doesn’t seem to be a problem. For applications that don’t follow this policy, it seems their calls will go to the last DLL registered.
(This is just from my own investigation and I really don’t know a whole lot about COM stuff. If anything mentioned is incorrect, please correct me.)
How to register and unregister COM DLLs can be found here. This is a bit convoluted. I actually just followed the steps listed in the second post of this thread on the MSDN forums to write my code.
To register:
HMODULE webKitMod = ::LoadLibrary(L"WebKit.dll");
FARPROC regFunc = ::GetProcAddress(webKitMod, "DllRegisterServer");
if(regFunc != NULL)
{
HRESULT regRet = regFunc();
if(regRet != S_OK)
{
// report error
}
}
else
{
// report error
}
::FreeLibrary(webKitMod);
To UnRegister:
HMODULE webKitMod = ::LoadLibrary(L"WebKit.dll");
FARPROC unRegFunc = ::GetProcAddress(webKitMod, "DllUnregisterServer");
if(unRegFunc != NULL)
{
HRESULT unRegRet = unRegFunc();
if(unRegRet != S_OK)
{
// report error
}
}
else
{
// report error
}
::FreeLibrary(webKitMod);