views:

92

answers:

6

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

+1  A: 

I think the simplest way is to try and create the component which lives in fooapi.dll. Wrap the creation code in a try/catch block and catch the exception which is generated if the DLL is not properly registered. This is the surest way to check for correct registration

JaredPar
A: 

Take a look here:

How to check if a dll is registered?

Use c# to check dll is registered

Leniel Macaferi
Points to the same page
ragu.pattabi
Very helpful...
SLaks
-1 that would've been funny, if the topic was recursion. :-) alas, it is not...
Franci Penov
+1  A: 

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

SLaks
+1  A: 

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

Franci Penov
+3  A: 

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.

Christian Hayter
The beauty with `Type.GetTypeFromCLSID` is that it always returns System.__ComObject regardless of whether the CLSID is valid
ragu.pattabi
+1  A: 

If you have the progID of the component in the DLL, you can try getting the Type:

System.Type.GetTypeFromProgID(string progID, bool throwOnError)

If you get System.Runtime.InteropServices.COMException, it means that the progID is not registered.

ragu.pattabi