...obtain DLL-specific version information?

Author: Thomas Stutz

Category: System

{***************************
** DllGetVersion Function **

***************************}

{
  Implemented by many of the Microsoft® Windows® Shell dynamic-link libraries (DLLs) to
  allow applications to obtain DLL-specific version information.
}


{
  Using DllGetVersion to Determine the Version Number
  Starting with version 4.71, the Shell and common controls DLLs, among others,
  began exporting DllGetVersion.
  This function can be called by an application to determine which DLL
  version is present on the system. It returns a structure that contains version information.

  Note  DLLs do not necessarily export DllGetVersion. Always test for it
  before attempting to use it.
  For systems earlier than Windows 2000, DllGetVersion returns a DLLVERSIONINFO structure
  that contains the major and minor version numbers, the build number,
  and a platform identifier (ID).
  For Windows 2000 and later systems, DllGetVersion might instead return a
  DLLVERSIONINFO2 structure. This structure contains the QFE number that identifies
  the service pack and provides a more robust way to compare version numbers than DLLVERSIONINFO.
  Because the first member of DLLVERSIONINFO2 is a DLLVERSIONINFO structure,
  the new structure is backward-compatible.
}

// DLLVERSIONINFO structure
type
  
PDLLVerInfo=^TDLLVersionInfo;
  TDLLVersionInfo=Record
    
cbSize,   // Size of the structure, in bytes.
    
dwMajorVersion, // Major version of the DLL
    
dwMinorVersion, // Minor version of the DLL
    
dwBuildNumber, // Build number of the DLL
    
dwPlatformID: DWord; // Identifies the platform for which the DLL was built
end;

var
  
DllGetVersion: function(dvi: PDLLVerInfo): PDLLVerInfo; stdcall;

function GetDllVersion(DllName: stringvar DLLVersionInfo: TDLLVersionInfo): Boolean;
var
  
hInstDll: THandle;
  p: pDLLVerInfo;
begin
  
Result := False;
  // Get a handle to the DLL module.
  // das Handle zum DLL Modul ermitteln.
  
hInstDll := LoadLibrary(PChar(DllName));
  if (hInstDll = 0) then Exit;
  // Return the address of the specified exported (DLL) function.
  // Adresse der Dll-Funktion ermitteln
  
@DllGetVersion := GetProcAddress(hInstDll, 'DllGetVersion');
  // If the handle is not valid, clean up an exit.
  // Wenn das Handle ungültig ist, wird die Funktion verlassen
  
if (@DllGetVersion) = nil then
  begin
    
FreeLibrary(hInstDll);
    Exit;
  end;

  new(p);
  try
    
ZeroMemory(p, SizeOf(p^));
    p^.cbSize := SizeOf(p^);

    // Call the DllGetVersion function
    // Die DllGetVersion() Funktion aufrufen
    
DllGetVersion(p);
    DLLVersionInfo.dwMajorVersion := p^.dwMajorVersion;
    DLLVersionInfo.dwMinorVersion := p^.dwMinorVersion;

    @DllGetVersion := nil;
    Result := True;
  finally
    
dispose(P);
  end;
 // Free the DLL module.
 // Dll wieder freigeben.
 
FreeLibrary(hInstDll);
end;

// Example to get version info from comctl32.dll
// Beispiel, um von comctl32 Versions/Informationen zu erhalten

procedure TForm1.Button1Click(Sender: TObject);
var
  
DLLVersionInfo: TDLLVersionInfo;
begin
 if not 
GetDllVersion('comctl32.dll',DLLVersionInfo) then
 begin
   
DLLVersionInfo.dwMajorVersion := 4;
   DLLVersionInfo.dwMinorVersion := 0;
 end;
 with DLLVersionInfo do
   
ShowMessage(Format('ComCtl Version: %d.%d / Build: %d',[dwMajorVersion, dwMinorVersion, dwBuildNumber]))
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base