Tuesday, September 16, 2008

Which Version of a DLL Is Loaded?

While playing with VistaBridge (about which, more soon), I needed to verify that my .NET application was using version 6 of comctl32.dll, which is the first version that supports task dialogs. I found a number of posts about getting version information for unmanaged DLLs, but they all involved getting file version information from the physical file on disk. This isn't a good solution due to side-by-side considerations: Windows might not be using the version of comctl32.dll that lives in the system folder.

I got the answer from the brand new (and very promising) Stack Overflow programming Q&A site: System.Diagnostics.Process.GetCurrentProcess returns a Process object, which has a Modules collection. You can iterate the collection, find the module you're looking for, and check its FileVersionInfo property. This will work, of course, for any module, not just comctl32.

Here's a function that returns the FileVersionInfo for any loaded module:
using System.Diagnostics;
internal static FileVersionInfo GetLoadedModuleVersion(string name)
{
Process process = Process.GetCurrentProcess();
foreach (ProcessModule module in process.Modules)
{
if (module.ModuleName.ToLower() == name)
return module.FileVersionInfo;
}
return null;
}
So if I want to verify that I have comctl version 6:
internal static bool HaveComctl6()
{
FileVersionInfo verInfo = GetLoadedModuleVersion("comctl32.dll");
return verInfo != null && verInfo.FileMajorPart >= 6;
}

No comments: