What is the best way to detect if a COM component

2019-05-27 00:02发布

问题:

I have a COM interop assembly, and I would like to check from a .NET application whether the component I'm about to create is installed on the machine.

I would like to provide a nice error message if it is not installed.

Put the instantiation into try-catch is not a good solution for me, as I would like to distingwish between the missing installation and the other errors that may occur.

My idea is to check whether the node with the COM class id exists in the registry under the HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface path. But is there a better approach?

回答1:

I think the best way, unfortunately, is to try and instantiate it. Checking in the registry does not guarantee that the component is actually there at all (e.g. the file may have been deleted or moved post registration).



回答2:

In addition to the problem that Eyvind mentions, there's a few other things to consider when you're performing your registry check:

  • It's possible (not common, but possible) to have (parts of) the COM registration in HKCU\Software\Classes, so you would have to check that as well.
  • There's also Registration-free COM, where you won't find anything in the registry, but still would be able to instantiate the COM object...

Neither of the above are common, but good to be aware of nonetheless.

Eyvind's scenario is probably something you'd run into more often: someone deleting your COM server from disk without unregistering it first.



回答3:

Type.GetTypeFromProgID("MyProgID") != null



回答4:

You might also instantiate an object of the com class you want to check in a try/catch block so as to warn your user in the catch block. Blunt solution, I agree.

Checking the registry seems the most reasonable approach to me, as an unregistered com component is pretty useless.



回答5:

You could use iTripoli Type Library Viewer - free. Using the above, open the EXE/DLL in question. Then, you'll discover the GUID. Open regedit, go to KEY: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID. Search/find using the above GUID.



标签: .net com