C # check if COM object is accessible before doing any action on them

How to check if a COM object exists (registered / accessible / can work without problems) before starting actions with them?

My application should use other COM applications (InteropServices), but before I start any action, I want to check if I can create COM objects.

This is not a problem if COM is where in the same directory, but it is not.

In the example. I would like to check if this is the case:

CDNBase.ApplicationClass App = new CDNBase.ApplicationClass(); 

will throw exciting exceptions or something else. How could I create a good MessageBox and block some events until it is fixed. Any other solutions, such as checking for a namespace or sth, are also fine (I think: D)

I tried using try / catch, but it fails, Google did not bring me anything special about this, so I ask you for help.

Thanks in advance

+4
source share
2 answers

You tried to catch a ComException :

 try { ... } catch(System.Runtime.InteropServices.COMException ex) { // log error message throw; } 

UPDATE : apologies, I did not propose to exclude the exception. Updated.

Alternatively, you can search the registry for the ClassID component:

 HKEY_CLASSES_ROOT\CLSID\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\InprocServer32 

The default value will contain the full path of the file system to the DLL.

+1
source

No one likes to see an error message. But this is a special case, do not do this. Activating COM has always been a daunting task. But recent events, such as 64-bit operating systems, registry virtualization, registry redirection, and UAC, have exponentially increased the number of ways it cannot work.

The last thing that your client’s IT staff needs is another level of code that silently modifies activation rules or suppresses diagnostic information. They will shoot you if they do not work, and they cannot understand why.

Ask yourself first, your program is still useful when the interaction is not working. If it’s not, do nothing, just let the exception terminate your program, but make sure that the details of the exception are clearly visible.

If this is still useful, just add a configuration option to your program that allows you to get around this process without even trying.

+2
source

Source: https://habr.com/ru/post/1299813/


All Articles