Reputation: 10644
How can i check if COM Object exists (is registered / is available / can run without problems) before running actions using them?
My application should use other applications COM's (InteropServices) but before i start some action, i would like to check whether i can create COM objects.
It woudnt be a problem if COM's where in same directory, but they're not.
In excample. I would like to check if sth like this:
CDNBase.ApplicationClass App = new CDNBase.ApplicationClass();
will cause catchable exceptions or something. Than i could create nice MessageBox and block some events till it will be fixed. Any other solutions like checking if namespace exists or sth, are also ok (i guess :D )
I tried to use try / catch, but it fails, google didnt bring me anything special on this, so im asking for your help.
Thanks in advance
Upvotes: 4
Views: 4816
Reputation: 300549
Have you tried catching ComException
:
try
{
...
}
catch(System.Runtime.InteropServices.COMException ex)
{
// log error message
throw;
}
UPDATE: apologies, I wasn't suggesting to suppress the exception. Have updated.
Alternatively, you could search in the registry for the component's ClassID
:
HKEY_CLASSES_ROOT\CLSID\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\InprocServer32
The default value will contain the filesystem full path to the DLL.
Upvotes: 1
Reputation: 941465
Nobody likes to see an error message. But this is a special case, don't do this. COM activation has always been hard to trouble-shoot. But recent developments like 64-bit operating systems, registry virtualization, registry redirection and UAC has exponentially increased the number of ways it might not work.
The very last thing your customer's IT staff needs is another layer of code that silently changes the activation rules or suppresses diagnostic information. They'll shoot you some serious flak when it doesn't work and they can't find out why.
First ask yourself if your program is still useful when the interop doesn't work. If it isn't then do nothing, just let the exception terminate your program but do make sure the exception details are well visible.
If it is still useful then simply add a config option to your program that lets it bypass the interop without even trying.
Upvotes: 2