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

前端 未结 6 1061
情歌与酒
情歌与酒 2020-12-06 17:59

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 th

相关标签:
6条回答
  • 2020-12-06 18:28

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 18:32

    Look at the rgistry at HKEY_CLASSES_ROOT\CLSID\\InprocServer. If you have that record, then the DLL should be registered.

    0 讨论(0)
  • 2020-12-06 18:39

    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.

    0 讨论(0)
  • 2020-12-06 18:41

    This is the proper way to do it. It does involve PInvoke but that' only because they haven't provided this capability in .NET directly.

    [DllImport("kernel32")]    
    public extern static int LoadLibrary(string lpLibFileName);
    
    [DllImport("kernel32")]    
    public extern static bool FreeLibrary(int hLibModule);        
    
    public bool IsDllRegistered(string DllName)    
    {    
      int libId = LoadLibrary(DllName);
      if (libId>0) FreeLibrary(libId);
      return (libId>0);    
    }
    
    0 讨论(0)
  • 2020-12-06 18:44

    Verify if key exists using Microsoft.Win32.RegistryKey

    var key = Microsoft.Win32.RegistryKey.OpenBaseKey(
                Microsoft.Win32.RegistryHive.ClassesRoot,
                Microsoft.Win32.RegistryView.Default)
                .OpenSubKey("Interface")
                   //replace with your COM object GUID
                .OpenSubKey("{a3560000-0000-0000-c63b3-000000cbadf0000}");
    
            return key != null;
    
    0 讨论(0)
  • 2020-12-06 18:48

    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.

    0 讨论(0)
提交回复
热议问题