Обновлено: 13 июня, 2018

Как получить информацию о версии файла в Delphi?

Для этого необходимо вызвать несколько функций API. В приведенном ниже примере проверяется версия shell32.dll. Функция возвращает значение True — если версия DLL больше или равна 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; 
1 Звезда2 Звезды3 Звезды4 Звезды5 Звезд (11 оценок, среднее: 5,00 из 5)
Загрузка...
Добавить комментарий