Главная страница | назад





Article #19658: How do I get the version information for a file?

 Question and Answer Database

     FAQ: FAQ4658D - How do I get the version information for a file?
Category: Windows API
Platform: All-32Bit
 Product: All32Bit,   

Question:

How do I get the version information for a file?


Answer:

In order to get the file version information for a file you 
have to make several API calls. Here is an example
that checks your version of shell32.dll and returns True if
it is greater than or equal to 4.71.

function TForm1.CheckShell32Version: Boolean;

  procedure GetFileVersion(FileName: string; var Major1, Major2,
    Minor1, Minor2: Integer);
  { Helper function to get the actual file version information }
  var
    Info: Pointer;
    InfoSize: DWORD;
    FileInfo: PVSFixedFileInfo;
    FileInfoSize: DWORD;
    Tmp: DWORD;
  begin
    // Get the size of the FileVersionInformatioin
    InfoSize := GetFileVersionInfoSize(PChar(FileName), Tmp);
    // If InfoSize = 0, then the file may not exist, or
    // it may not have file version information in it.
    if InfoSize = 0 then
      raise Exception.Create('Can''t get file version information for '
        + FileName);
    // Allocate memory for the file version information
    GetMem(Info, InfoSize);
    try
      // Get the information
      GetFileVersionInfo(PChar(FileName), 0, InfoSize, Info);
      // Query the information for the version
      VerQueryValue(Info, '\', Pointer(FileInfo), FileInfoSize);
      // Now fill in the version information
      Major1 := FileInfo.dwFileVersionMS shr 16;
      Major2 := FileInfo.dwFileVersionMS and $FFFF;
      Minor1 := FileInfo.dwFileVersionLS shr 16;
      Minor2 := FileInfo.dwFileVersionLS and $FFFF;
    finally
      FreeMem(Info, FileInfoSize);
    end;
  end;

var
  tmpBuffer: PChar;
  Shell32Path: string;
  VersionMajor: Integer;
  VersionMinor: Integer;
  Blank: Integer;
begin
  tmpBuffer := AllocMem(MAX_PATH);
  // Get the shell32.dll path
  try
    GetSystemDirectory(tmpBuffer, MAX_PATH);
    Shell32Path := tmpBuffer + '\shell32.dll';
  finally
    FreeMem(tmpBuffer);
  end;

  // Check to see if it exists
  if FileExists(Shell32Path) then
  begin
    // Get the file version
    GetFileVersion(Shell32Path, VersionMajor, VersionMinor, Blank, Blank);
    // Do something, such as require a certain version
    // (such as greater than 4.71)
    if (VersionMajor >= 4) and (VersionMinor >= 71) then
      Result := True
    else
      Result := False;
  end
  else
    Result := False;
end;


6/21/99 2:08:09 PM
 

Last Modified: 01-SEP-99