tags:

views:

286

answers:

2

Hello. 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

A: 

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.

Mitch Wheat
Yes, but couldnt catch exception in same method. It worked other way:In buttonClickMethod (or anywhere else where i start this code) i have:this.runSynch();in runSynch() at the beginning i declare COM's i need to check, so i have:runSynch(..){CDNBase... App = new CDNBase...//(rest of code...)}And in buttonClickMethod i have:try{ runSynch }catch(FileNotFoundException ex){ msgBox.. }So it works how i want it to work ;)Cheers
cichy
This approach is itself error prone: you will need to handle 32/64bit differences (e.g. 32bit COM object installed, but this is a 64bit process). How about out of process implementation? And just because the key exists doesn't mean the implementation file exists, or the process has access to it.
Richard
Yes, don't do this.
Hans Passant
Damn, because of you i see how much i have to learn before i even start to think about writing decent application. ;)
cichy
+2  A: 

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.

Hans Passant
Well, i need to integrate one app (www) with other exe. So i wrote soap-webservice and almost finished small integrating application that uses this .exe's COMs to perform some actions ;)In short: its essential to have working COMs. Without them i could just shut down my app :DBut thank you for your answer, because i dont have much experience in developing bigger aps.
cichy
Right, just let it crash, don't fix the problem.
Hans Passant