testing if an application is intalled

  • Thread starter Thread starter thierry
  • Start date Start date
T

thierry

Hello,

I try tu use MapPoint but prior to begin to use it I would like to
test if it is present on the computer running my code. So After going
around a little I actually code like

MapPoint.ApplicationClass mpac = null;
try {
mpac = new MapPoint.ApplicationClass();
} catch ( System.Exception ex ) {
System.Windows.Forms.MessageBox.Show("Microsoft MapPoint ou un de
ses composants n'a pas été trouvé sur ce poste.\n" + ex.Message );
}

But I'm quite sure there is a more elegant way (without exception for
example)

thank you in advance

thierry
 
Hello,

I try tu use MapPoint but prior to begin to use it I would like to
test if it is present on the computer running my code. So After going
around a little I actually code like

MapPoint.ApplicationClass mpac = null;
try {
    mpac = new MapPoint.ApplicationClass();} catch ( System.Exception ex ) {

    System.Windows.Forms.MessageBox.Show("Microsoft MapPoint ou un de
ses composants n'a pas été trouvé sur ce poste.\n" + ex.Message );

}

But I'm quite sure there is a more elegant way (without exception for
example)

thank you in advance

thierry

You can probably try to use a more specific exception, to begin with.
Catching the System.Exception is not recommended in general.
 
You're probably better off relying on an exception in most cases. Testing if
a program is installed isn't standardized (it depends on the installation
technology) and even for mainstream technologies like MSI, there are a
varirety of issues associated with it I won't get into here (though you can
take a look at "MsiQueryProductState()" in the WinAPI if you're curious -
this doesn't discuss the issues I'm referring to though). In your case, if
"MapPoint" is COM-based as it appears to be (from the quick check I did),
then try catching "COMException" instead. If it is thrown, then test the
"ErrorCode" property. It may give you a usable error code (HRESULT actually)
but this isn't always guaranteed to be reliable however (for future versions
of "MapPoint" in particular). In practice though it usually will be.
 
hello again.

I finally found pretty good solution: use WMI

ManagementScope scope = new ManagementScope(@"\\.\root
\cimv2");
ObjectQuery oq = new ObjectQuery("select * from
win32_product where name like '%Microsoft MapPoint%'");
ManagementObjectSearcher mos = new ManagementObjectSearcher
(scope, oq);
ManagementObjectCollection moc = mos.Get();
if ( moc.Count > 0 ) {
//then mappoint is probably installed
try {
.........
} else {
//MapPoint is certainly not installed
}
 
I finally found pretty good solution: use WMI
ManagementScope scope = new ManagementScope(@"\\.\root
\cimv2");
ObjectQuery oq = new ObjectQuery("select * from
win32_product where name like '%Microsoft MapPoint%'");
ManagementObjectSearcher mos = new ManagementObjectSearcher
(scope, oq);
ManagementObjectCollection moc = mos.Get();
if ( moc.Count > 0 ) {
//then mappoint is probably installed
try {
.........
} else {
//MapPoint is certainly not installed
}

This just relies on the native MSI API (see example in my previous post) but
it's potentially unstable in your case. It amounts to a (heuristic) guessing
game which is bound to fail eventually. Microsoft could change the name or
you could have localization issues. It's also possible this particular
object may eventually be installed using other means (using another
installation technology for instance or it may even be installed to support
other MSFT products). Maybe you don't want to support that unless the
original product is also installed (even though your code would still work).
Nevertheless, it's safer and cleaner to just trap the exception and check
the error code as previously suggested. Underneath the hood
"CoCreateInstanceEx()" is likely being called (or some variant) and the
error code will likely be stable (REGDB_E_CLASSNOTREG from <winerror.h> if
it's not installed perhaps - you need to check). You can even check that the
COM server itself is installed but why bother. You need to trap the
exception anyway since it may fail even if MapPoint is installed.
 
I now better understand what your talking about in your posts.
And I agree with the fact that I must trap the exception.
I think I will use WMI to try to log the more pertinent message.

thank you
 
Back
Top