C# 如何检查 COM 组件(EXE/DLL 文件)是否已注册(使用 .NET)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1491909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a COM component (EXE/DLL file) is registered or not (using .NET)?
提问by devnull
How do I check if a COM component (EXE/DLL file) is registered or not using .NET?
如何检查 COM 组件(EXE/DLL 文件)是否已使用 .NET 注册?
采纳答案by Arthur
Just make a lookup in the registry. HKEY_CLASSES_ROOT\yourcom.component.
只需在注册表中查找即可。HKEY_CLASSES_ROOT\yourcom.component。
回答by DB Tech
It depends. Your component will be registered into the Windows Registry so you need to figure out which hive you want to look in.
这取决于。您的组件将注册到 Windows 注册表中,因此您需要确定要查看的配置单元。
If your component is installed with regasm, chances are HKCU will be used, since it will be run from a user's command line. If, however, you use an MSI, the MSI may not use regasm and may place the entries directly into HKLM if you run the MSI in PER MACHINE mode (ALLUSERS = "1") or as an admin. On the other hand, if you run the MSI as PER USER (ALLUSERS = "") or as an unprivileged account, it will use HKCU.
如果您的组件安装了 regasm,则可能会使用 HKCU,因为它将从用户的命令行运行。但是,如果您使用 MSI,如果您在 PER MACHINE 模式(ALLUSERS = "1")或以管理员身份运行 MSI,则 MSI 可能不会使用 regasm,并且可能会将条目直接放入 HKLM。另一方面,如果您以 PER USER (ALLUSERS = "") 或非特权帐户运行 MSI,它将使用 HKCU。
HKCR is merged view of HKLM and HKCU, so you can't tell which hive was actually used, and it might not give you what you want to know. MSDN HKEY_CLASSES_ROOT
HKCR 是 HKLM 和 HKCU 的合并视图,因此您无法分辨实际使用的是哪个 hive,并且它可能无法提供您想知道的信息。 MSDN HKEY_CLASSES_ROOT
If your COM component is registered PER USER, it might fail depending on which user ran the install. So if you want to check whether or not it was installed CORRECTLY, you need to figure out which key you actually want to use, or if HKCR is acceptable. For end user testing, HKCR might be the safest way to test as it will be accessible by everyone and (in .NET) will not throw security exceptions.
如果您的 COM 组件是按用户注册的,它可能会失败,具体取决于运行安装的用户。所以如果你想检查它是否被正确安装,你需要弄清楚你真正想要使用哪个密钥,或者HKCR是否可以接受。对于最终用户测试,HKCR 可能是最安全的测试方式,因为每个人都可以访问它并且(在 .NET 中)不会抛出安全异常。
Also see this post: regasm and HKCU
另请参阅此帖子:regasm 和 HKCU
回答by Shane
From Visual Studio you can do this from the Reference Manager.
在 Visual Studio 中,您可以从 Reference Manager 执行此操作。
- Go to Solution Explorer > References
- Right click on References
- Choose Add Reference...
- Choose the COM item from the menu
- 转到解决方案资源管理器 > 参考
- 右键单击引用
- 选择添加参考...
- 从菜单中选择 COM 项
The nice thing about this tool is you can search for the objects by keyword a bit more elegantly than navigating through the registry. Note you can see the non-COM objects registered in the GAC as well.
这个工具的好处是你可以通过关键字来搜索对象,而不是在注册表中导航。请注意,您还可以看到在 GAC 中注册的非 COM 对象。