Shell and common control versions

by Mark Wiseman

In previous issues I talked about how to detect the version of Windows your program is running on. You learned that you need to know the Windows version in order to use certain API functions and registry settings. But knowing the version of Windows is sometimes not enough.

There are three DLLs that contain the APIs for the Windows’ shell and common controls. These DLLs are COMCTL32.DLL, SHELL32.DLL, and SHLWAPI.DLL.

As you might expect, the APIs contained in these DLLs vary based on the version of Windows. But they also vary depending on the installed version of Internet Explorer.

What version is it?

Listings A and B are the source code for two functions that you can use to determine the versions of these DLLs.

The GetDLLVersion() function takes the name of the DLL (COMCTL32.DLL, SHELL32.DLL, or SHLWAPI.DLL) and returns a DWORD that contains the DLL’s version. The major version number is in the HIWORD and the minor version number is in the LOWORD.

We can easily convert the version information to a string using this code:

String version = 
  String(HIWORD(version)) + 
  "." + String(LOWORD(version));

The example program uses this technique to display the versions of the three DLLs in a dialog box.

The inline function PackDLLVersion() takes the major and minor version numbers and packs them into a DWORD. We can use PackDLLVersion() to make testing the DLLs’ versions easier:

if (GetDLLVersion("Shell32.dll") 
    >= PackDLLVersion(4,71))
  // Shell32.dll is atleast version 4.71
else
  // Shell32.dll is older

What’s next?

In the next issue, you will see how to manipulate the Windows’ Recycle Bin with code. To do so, you will need to know which version of the Shell32.dll is on the system.

Listing A: DLLVersion.h

#ifndef DLLVersionH
#define DLLVersionH

inline DWORD 
PackDLLVersion(DWORD major, DWORD minor)
{
  return((major << 16) + minor);
}

DWORD GetDLLVersion(String dllName);

#endif    // DLLVersionH

Listing B: DLLVersion.cpp

#include <vcl.h>
#pragma hdrstop

#include "DLLVersion.h"

#include <shlwapi.h>

DWORD GetDLLVersion(String dllName)
{
  DWORD version = 0;

  HINSTANCE hInst = LoadLibrary(dllName.c_str());

  if (hInst)
  {
    DLLGETVERSIONPROC getVersion =  
      (DLLGETVERSIONPROC)GetProcAddress(
      hInst, "DllGetVersion");

      if (getVersion)
      {
        DLLVERSIONINFO info;
        ZeroMemory(&info, sizeof(info));
        info.cbSize = sizeof(info);

        HRESULT result = (*getVersion)(&info);
        if (SUCCEEDED(result))
          version = PackDLLVersion(
            info.dwMajorVersion, 
            info.dwMinorVersion);
      }

      FreeLibrary(hInst);
  }

  return(version);
}