Find COM DLL path from Com Interop Assembly

孤街浪徒 提交于 2019-12-14 02:18:48

问题


I am trying to write a wrapper around a legacy COM object and install the wrapper into the GAC. The goal would be to automate the setup of specific configuration information the component requires, and make a common strongly typed interface for all of my applications to use.

My solution thus far is to keep an XML configuration file in the same directory as the original COM DLL, and load the configuration in the class constructor. Unfortunately, I have been unable to find the location of the registered COM dll...

How do I get the full file path of the COM dll referenced by a COM object interop dll?


回答1:


Once you've created an object from the respective COM server, its DLL must have been loaded. Assuming that the underlying COM server is implemented in "mycomserver.dll", you could use P/Invoke and call GetModuleHandle( "mycomserver.dll" ) -- that gives you the path of the DLL.




回答2:


Presumably you could get the GuidAttribute or CoClassAttribute values from the interop DLL that map to the CLSID and IID values of your COM DLL. Then you can look up the appropriate DLL path in the registry.




回答3:


If you know the CLSID of the COM dll, you can 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)

If the key is there, it means that the COM component is registered. Then look at the default value inside the sub-key InprocServer32

e.g.

  • HKEY_CLASSES_ROOT\CLSID\{12345678-9012-3456-7890-123456789012}\InprocServer32
  • HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{12345678-9012-3456-7890-123456789012}\InprocServer32

If helps, here is a reference example of how to open these keys using C# (you'd just have to check for the value in InprocServer32): How to check COM dll is registered or not with C#?




回答4:


Just reflect the AddIn class.

var t = typeof(ThisAddIn);
var path = t.Assembly.CodeBase;


来源:https://stackoverflow.com/questions/849701/find-com-dll-path-from-com-interop-assembly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!