我需要检查在我运行的计算机系统上是否已经注册了msdia100.dll,以便使用命令regsvr32.exe
注册该dll。如何使用C#实现?
HKEY_CLASSES_ROOT\CLSID\{CLSID-of-your-COM-component}
或者 HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CLSID-of-your-COM-component}
(Wow6432Node => 在64位机器上注册的32位COM)。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;
}
}
注册表方法是可以的,值得尝试。但为确保安全,您还可以在COM对象中实例化try {} catch (COMException) {},如果捕获到COMException,则向用户呈现合理的内容。
HKEY_CLASSES_ROOT
中查找DLL名称。HKEY_CLASSES_ROOT\[ClassName]\CLSID
中查找CLSID,最后你应该能够在 HKEY_CLASSES_ROOT\CLSID\[CLSID]
中找到它。使用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;
查看 HKEY_CLASSES_ROOT\CLSID\\InprocServer 的注册表。如果您有该记录,则 DLL 应已注册。
这是正确的做法。它确实涉及到 PInvoke,但那只是因为他们没有在 .NET 直接提供这个功能。
[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);
}