Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a COM dll is registered in C#

I created a Office Add-In in VS 2008, C#, .NET 3.5, and VSTO. It is deployed via ClickOnce. A run-time configuration form executes regsvr32 to register "fooapi.dll" included with the project that can not be registered during instal due to ClickOnce limitations. Is there any prefered way to check and see if "fooapi.dll" is registered during run-time in C#?

like image 529
Jay Avatar asked Jul 09 '10 16:07

Jay


3 Answers

Try the Type.GetTypeFromCLSID or Type.GetTypeFromProgID methods to quickly check for a COM interface's existence.

Alternatively, just instantiate the object and trap the exception, e.g.

catch(COMException ex) {
    if(ex.ErrorCode == -2147221164) {
        // Retrieving the COM class factory for component with CLSID XXXX failed
    }
}

UPDATE:

This overload appears to be the only one that actually returns null if the COM object cannot be instantiated.

like image 69
Christian Hayter Avatar answered Nov 16 '22 20:11

Christian Hayter


If you know the DLLs GUID, you could check the existance of the registry key in HKCU\SOFTWARE\Classes.

like image 44
SLaks Avatar answered Nov 16 '22 20:11

SLaks


Check the presence of HKEY_CLASSES_ROOT\CLSID\{your_CLSID} and the proper values under it. You could probably get away with looking for InprocServer32 and Codebase values only, but you might also opt-in for more extensive check.

You can also just create an instance of the component. However, if both the component and the client are C# and you use new, the CLR might be able to figure out the proper assembly and load it with going through COM. (Yes, it can be smart like that sometimes :-)). You should explicitly p/invoke to CoCreateInstance

like image 2
Franci Penov Avatar answered Nov 16 '22 20:11

Franci Penov