Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check COM dll is registered or not with C#?

Tags:

c#

registry

I need to check if msdia100.dll is registered on a computer system that I'm running in order to register the dll with the command regsvr32.exe. How can I do that with C#?

like image 488
prosseek Avatar asked Feb 11 '11 07:02

prosseek


People also ask

How do you check COM DLL is registered or not?

Open regedit and search for your DLL filename. If it is registered, you will find filename under a key that is under the TypeLib. The key will look like: {9F3DBFEE-FD77-4774-868B-65F75E7DB7C2}

What is COM DLL in C#?

A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications.

Where are COM components registered?

Your component will be registered into the Windows Registry so you need to figure out which hive you want to look in. If your component is installed with regasm, chances are HKCU will be used, since it will be run from a user's command line.

Do DLL files need to be registered?

Short answer is that you don't need to register DLLs in order to use them. The only exception to this is COM and ActiveX DLLs which need to add certain keys to the registry. For a normal DLL (including . NET class libraries), all you need to know is the path to the DLL.


3 Answers

The registry approaches are okay and worth doing, but to be sure you might also consider instantiating something from within the COM object wrapped in a try {} catch (COMException) {}, then present something sensible to the user if a COMException got caught.

like image 78
tomfanning Avatar answered Oct 12 '22 14:10

tomfanning


Assuming you know the CLSID of the COM dll, you can just check if there's a key with that CLSID on HKEY_CLASSES_ROOT\CLSID\{CLSID-of-your-COM-component} or HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CLSID-of-your-COM-component} (Wow6432Node => 32-bit COM registered on a 64-bit machine)

e.g.

private bool IsAlreadyRegistered()
{
    using (var classesRootKey = Microsoft.Win32.RegistryKey.OpenBaseKey(
           Microsoft.Win32.RegistryHive.ClassesRoot, Microsoft.Win32.RegistryView.Default))
    {
        const string clsid = "{12345678-9012-3456-7890-123456789012}";

        var clsIdKey = classesRootKey.OpenSubKey(@"Wow6432Node\CLSID\" + clsid) ??
                        classesRootKey.OpenSubKey(@"CLSID\" + clsid);

        if (clsIdKey != null)
        {
            clsIdKey.Dispose();
            return true;
        }

        return false;
    }
}
like image 22
C. Augusto Proiete Avatar answered Oct 12 '22 13:10

C. Augusto Proiete


You can search through the registry for this. Assuming that you don't know the COM objects contained in the DLL you'll have to start looking for the DLL name first in HKEY_CLASSES_ROOT.

Then use the class name to find the CLSID in HKEY_CLASSES_ROOT\[ClassName]\CLSID and finally you should be able find it the CLSID as HKEY_CLASSES_ROOT\CLSID\[CLSID].

Please note, registry locations written from memory so might be a bit off.

Edit: Or if you know the class name you could just try to create an instance of it and see if it works or not.

like image 26
Hans Olsson Avatar answered Oct 12 '22 13:10

Hans Olsson