Beranda > Delphi > Getting version information from a file

Getting version information from a file

GetAllFileVersionInfo() is great for your About boxes in your Delphi projects.  To use it, make sure you have the necessary info built into the EXE by selecting (in the Delphi IDE) Project | Project Options, Version Info tab, input your EXE info, and build your project.  Simply get this info during run-time by calling GetAllFileVersionInfo(Application.ExeName) and read the results in the TFileVersionInfo record.  You can do the same for other EXEs by calling GetAllFileVersionInfo(‘EXENAME.EXE’). If you’re having trouble getting version info from a file and you know the info exists in the file, try GetAppVersionInfo() below.type
  TFileVersionInfo = record
    fCompanyName,
    fFileDescription,
    fFileVersion,
    fInternalName,
    fLegalCopyRight,
    fLegalTradeMark,
    fOriginalFileName,
    fProductName,
    fProductVersion,
    fComments: string;
  end;

var
  FileVersionInfo: TFileVersionInfo;

procedure GetAllFileVersionInfo(FileName: string);
var
  Buf      : PChar;
  fInfoSize: DWord;

  procedure InitVersion;
  var
    FileNamePtr: PChar;
  begin
    with FileVersionInfo do
      begin
        FileNamePtr := PChar(FileName);
        fInfoSize := GetFileVersionInfoSize(FileNamePtr, fInfoSize);
        if fInfoSize > 0 then
          begin
            ReAllocMem(Buf, fInfoSize);
            GetFileVersionInfo(FileNamePtr, 0, fInfoSize, Buf);
          end;
      end;
  end;

  function GetVersion(What: string): string;
  var
    tmpVersion: string;
    Len   : Dword;
    Value : PChar;
  begin
    Result := ‘Not defined’;

    if fInfoSize > 0 then
      begin
        SetLength(tmpVersion, 200);
        Value := @tmpVersion;
        { If you are not using an English OS, then replace the language & }
{ code-page identifier with the correct one. English (U.S.) is    }
{ 0409 (language) & 04E4 (code-page). See Code-Page Identifiers & }
{ Language Identifiers in the Win32 help file for info.           }

if
VerQueryValue(Buf, PChar(‘StringFileInfo40904E4\’ + What),

                         Pointer(Value), Len) then
          Result := Value;
     end;
  end;

begin
  Buf := nil;

  with FileVersionInfo do
    begin
      InitVersion;
      fCompanyName      := GetVersion(‘CompanyName’);
      fFileDescription  := GetVersion(‘FileDescription’);
      fFileVersion      := GetVersion(‘FileVersion’);
      fInternalName     := GetVersion(‘InternalName’);
      fLegalCopyRight   := GetVersion(‘LegalCopyRight’);
      fLegalTradeMark   := GetVersion(‘LegalTradeMark’);
      fOriginalFileName := GetVersion(‘OriginalFileName’);
      fProductName      := GetVersion(‘ProductName’);
      fProductVersion   := GetVersion(‘ProductVersion’);
      fComments         := GetVersion(‘Comments’);
    end;

  if Buf <> nil then
    FreeMem(Buf);
end;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: