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

前端 未结 6 1067
情歌与酒
情歌与酒 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;
        }
    }
    

提交回复
热议问题