Показать сообщение отдельно
  #7  
Старый 01.07.2011, 17:30
Zorkov Igor Zorkov Igor вне форума
Новичок
 
Регистрация: 28.07.2009
Сообщения: 85
Репутация: 50
По умолчанию

Код:
type
  TVersionInfo = record
    CompanyName: WideString;
    FileDescription: WideString;
    FileVersion: WideString;
    InternalName: WideString;
    LegalCopyright: WideString;
    LegalTradeMarks: WideString;
    OriginalFilename: WideString;
    ProductName: WideString;
    ProductVersion: WideString;
    Comments: WideString;
    Language: WideString;
    Translation: WideString;

    FileVersionMajor: Word;
    FileVersionMinor: Word;
    FileVersionRelease: Word;
    FileVersionBuild: Word;
    ProductVersionMajor: Word;
    ProductVersionMinor: Word;
    ProductVersionRelease: Word;
    ProductVersionBuild: Word;
  end;

function GetFileVersionInfo(FileName: WideString; var VersionInfo: TVersionInfo): Boolean;
var
  Handle, Len, Size: Cardinal;
  Translation: WideString;
  Data: PWideChar;
  Buffer: Pointer;
  FixedFileInfo: PVSFixedFileInfo;
begin
  Result := False;

  Size := GetFileVersionInfoSizeW(PWideChar(FileName), Handle);
  if Size > 0 then
  begin
    try
      GetMem(Data, Size);
    except
      Exit;
    end;
    try
      if GetFileVersionInfow(PWideChar(FileName), Handle, Size, Data) then
      begin
        if VerQueryValue(Data, '\', Pointer(FixedFileInfo), Len) then
        begin

          VersionInfo.FileVersionMajor := HiWord(FixedFileInfo^.dwFileVersionMS);
          VersionInfo.FileVersionMinor := LoWord(FixedFileInfo^.dwFileVersionMS);
          VersionInfo.FileVersionRelease := HiWord(FixedFileInfo^.dwFileVersionLS);
          VersionInfo.FileVersionBuild := LoWord(FixedFileInfo^.dwFileVersionLS);
          VersionInfo.ProductVersionMajor := HiWord(FixedFileInfo^.dwProductVersionMS);
          VersionInfo.ProductVersionMinor := LoWord(FixedFileInfo^.dwProductVersionMS);
          VersionInfo.ProductVersionRelease := HiWord(FixedFileInfo^.dwProductVersionLS);
          VersionInfo.ProductVersionBuild := LoWord(FixedFileInfo^.dwProductVersionLS);

          VersionInfo.FileVersion := IntToStr(HiWord(FixedFileInfo^.dwFileVersionMS)) + '.' + IntToStr(LoWord(FixedFileInfo^.dwFileVersionMS)) + '.' +
            IntToStr(HiWord(FixedFileInfo^.dwFileVersionLS)) + '.' + IntToStr(LoWord(FixedFileInfo^.dwFileVersionLS))
        end;

        if VerQueryValueW(Data, '\VarFileInfo\Translation', Buffer, Len) then
        begin
          Translation := IntToHex(PDWORD(Buffer)^, 8);
          Translation := Copy(Translation, 5, 4) + Copy(Translation, 1, 4);
          VersionInfo.Translation := '$' + Copy(Translation, 1, 4);

          SetLength(VersionInfo.Language, 64);
          SetLength(VersionInfo.Language, VerLanguageNameW(StrToIntDef('$' + Copy(Translation, 1, 4), $0409), PWideChar(VersionInfo.Language), 64));
        end;
        

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\CompanyName'), Buffer, Len) then
          VersionInfo.CompanyName := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\FileDescription'), Buffer, Len) then
          VersionInfo.FileDescription := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\FileVersion'), Buffer, Len) then
          VersionInfo.FileVersion := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\InternalName'), Buffer, Len) then
          VersionInfo.InternalName := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\LegalCopyright'), Buffer, Len) then
          VersionInfo.LegalCopyright := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\LegalTradeMarks'), Buffer, Len) then
          VersionInfo.LegalTradeMarks := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\OriginalFilename'), Buffer, Len) then
          VersionInfo.OriginalFilename := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\ProductName'), Buffer, Len) then
          VersionInfo.ProductName := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\ProductVersion'), Buffer, Len) then
          VersionInfo.ProductVersion := PWideChar(Buffer);

        if VerQueryValueW(Data, PWideChar('\StringFileInfo\' + Translation + '\Comments'), Buffer, Len) then
          VersionInfo.Comments := PWideChar(Buffer);
        Result := True;
      end;
    finally
      FreeMem(Data);
    end;
  end;
end;

function AdjustProcessPrivilege(PrivilegeName: WideString): Boolean;
var
  TokenHandle: Cardinal;
  TokenPrivileges: TTokenPrivileges;
  ReturnLength: Cardinal;
begin
  Result := False;
  try
    if Windows.OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TokenHandle) then
    begin
      try
        LookupPrivilegeValueW(nil, PWideChar(PrivilegeName), TokenPrivileges.Privileges[0].Luid);
        TokenPrivileges.PrivilegeCount := 1;
        TokenPrivileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        if AdjustTokenPrivileges(TokenHandle, False, TokenPrivileges, 0, nil, ReturnLength) then
          Result := True;
      finally
        CloseHandle(TokenHandle);
      end;
    end;
  except
    Exit;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  VersionInfo: TVersionInfo;
begin
  AdjustProcessPrivilege('SeDebugPrivilege');
  GetFileVersionInfo('C:\Windows\Explorer.EXE', VersionInfo);
  Form1.Caption:= VersionInfo.CompanyName;
end;

1) Использовать GetFileVersionInfo
2) Получить DebugPrivilege для своего процесса
Ответить с цитированием