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

Jay picture Jay · Jul 9, 2010 · Viewed 8.3k times · Source

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#?

Answer

Christian Hayter picture Christian Hayter · Jul 9, 2010

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.