How to check version of an installed COM DLL

J

jwexqm

I'm using a COM DLL from a C# application via an interop wrapper
generated by 'tlbimp.exe'. That all works fine, I can fire off methods
and things happen as they should.

My issue is that I want my application to check both that the COM DLL
is registered on the system and its version is acceptable. I envisage
a bit of code at the beginning of the application's runtime that
performs these checks.

I've got a 'soup' of ideas about how to tackle this. I get the
impression that I can't get the information I'm looking for from the
COM object if it's been instantiated via the interop. I tried this...

MyCOMLib.MyInterface MyInstance = new MyCOMLib.MyInterface();
Assembly myAssembly = Assembly.GetAssembly(MyInstance.GetType());
Console.WriteLine("name=" + SampleAssembly.CodeBase);

But it just gave me the pathname to the interop wrapper whereas I want
the pathname to the DLL that the interop uses.

So then I tried this to get rid of the interop haze in the middle...

Type myType = Type.GetTypeFromProgID("MyCOMLib.MyInterface");
Object myObject = Activator.CreateInstance(myType);

This felt just like instantiating a COM object in C++ with
CreateInstance(). So now I want to do something like a call to the
Platform SDK's GetModuleFileName(), passing in the object reference,
but looking around the framework I can only find the
'FullyQualifiedName' property of the Module class in
System.Reflection. Maybe I can use this somehow?

Armed with this fully qualified filename I was hoping to be able to
feed it into some kind of embedded resource-parsing function in the
framework that would give me the version directly from the file (like
GetFileVersionInfo() does in version.lib). Maybe there's a way of the
framework getting me the version without having to resolve the DLL's
pathname first though?

As for checking whether the COM object is registered at all on the
system at all, I imagine putting an exception check round an
instantiation statement would suffice?

Any comments welcome!
James
 
W

Wiktor Zychla

As for checking whether the COM object is registered at all on the
system at all, I imagine putting an exception check round an
instantiation statement would suffice?

yes, this would be sufficient.

as for the version - I know two possibilities:
1. implement the Version property in the COM object and check it. several
existing COM objects implement such property (for example Office objects)
2. get the guid of the COM type and look into the registry under
HKClassesToor\CLSID\. after you find the proper key, query its
"InprocServer32" subkey which will tell you the name of the object source.

if there's a simpler way, I would like to know it too.

regards,
Wiktor
 
J

jwexqm

Wiktor said:
as for the version - I know two possibilities:
1. implement the Version property in the COM object and check it. several
existing COM objects implement such property (for example Office objects)
2. get the guid of the COM type and look into the registry under
HKClassesToor\CLSID\. after you find the proper key, query its
"InprocServer32" subkey which will tell you the name of the object source.

if there's a simpler way, I would like to know it too.

Wiktor, thanks for your suggestion, I have attempted to implement it
in the following function below. Seems to do everything I wanted -
thank you very much. In all it was a lot less work than I was
expecting. If anyone knows of any other shortcuts / improvements then
please post!

here's the code.


// need these...
using System.Diagnostics; // for 'FileVersionInfo'
using Microsoft.Win32; // for 'RegistryKey'


// then somewhere in your main application...
string retval;
if( checkComServerExists("{blahblah-blah-blah-blah-blahblahblah}",
out retval) == false )
{
Console.WriteLine("didn't exist, reason: " + retval);
return;
}
Console.WriteLine("existed - Ver." + retval);


// checks to see if a COM server is registered and exists on the
system
public static bool checkComServerExists(string CLSID,
out string retval)
{
RegistryKey myRegKey = Registry.LocalMachine;
Object val;

try {
// get the pathname to the COM server DLL if the key exists
myRegKey = myRegKey.OpenSubKey(@"SOFTWARE\Classes\CLSID\" +
CLSID + @"\InprocServer32");
val = myRegKey.GetValue(null); // the null gets default
// value from key
}
catch {
retval = "not registered";
return false;
}

FileVersionInfo myFileVersionInfo = null;
try {
// parse out the version number embedded in the resource
// in the DLL
myFileVersionInfo =
FileVersionInfo.GetVersionInfo(val.ToString());
}
catch {
retval = ".dll not found";
return false;
}

retval = myFileVersionInfo.FileVersion;
return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top