Matt Murrell
Matt Murrell

Reputation: 2351

Find COM DLL path from Com Interop Assembly

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?

Upvotes: 5

Views: 5892

Answers (4)

ReR
ReR

Reputation: 1

Just reflect the AddIn class.

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

Upvotes: -1

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27818

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#?

Upvotes: 3

Johannes Passing
Johannes Passing

Reputation: 2805

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.

Upvotes: 2

Jeff Yates
Jeff Yates

Reputation: 62357

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.

Upvotes: 3

Related Questions