DelphiFeeds.com

  • Dashboard
  • Popular Stories
  • Trending Stories
  • Feeds
  • Login
Trending now

Easily Learn How To Use Python List Comprehensions In A Delphi Windows GUI App

Powerful Native Open Source Hard Disk, Folder and Storage Analyzer Built In Delphi

Incredible Database Development And Visualization Tool Is Built In Delphi

User Defined Functions with InterBase

Learn How To Use C++ Extending Sizeof For Windows Development

Learn How Easy It Is To Apply Updates With TFDQuery.OnUpdateRecord In Delphi

4 Ways to Generate Quick Test Ideas

Measure Heart Rate Using Delphi And C++ Builder With The Power Of ThingConnect IoT Device ...

Learn How To Use Python Dictionaries In A Delphi Windows GUI App

Create Enterprise-Grade WebSockets Based Apps With Powerful Components For Delphi

If you use an implementation of TNonRefCountInterfacedObject, then document in the descendants how lifetime management ...

High DPI Support, Improved TIFF and New Performance Demo in ImageEn Imaging Library v9.3.0

Easy-to-use Orbit Simulator Built In Delphi FireMonkey For Learning Gravitational Physics

Incredibly Powerful Enhanced Terminal And Network Toolkit For Windows Is Built In Delphi

Learn How To Use C++ Explicit Virtual Overrides In Windows Development

Reading Version Information from your EXE

1
Francesca Gaillard Francesca Gaillard 8 years ago in Delphi, Resources, windows 0

Very often, you have to check some information contained in the VERSIONINFO section of your application and most of the examples you can find on the InterWebz are doing it by using this WinAPI routine: GetFileVersionInfo.

It certainly works and can be used to get the VersionInfo from any file you want, much like in Windows Explorer, because the routine takes a FileName as its main parameter and reads it from its header.

But if you need it from inside the running application, why would you go and read the file on disk (or at least ask the OS for it) when you have the information directly available as a resource.

The main difference is that instead of calling GetFileVersionInfo, you have to find, load and lock the resource. Then you can query the value all the same. (Warning: see Arthur’s comment below and read Raymond Chen’s blog post he references)

procedure GetResourceVersionNumbers(out AMajor, AMinor, ARelease, ABuild: Integer);
{ Get the module version information from the embedded resource.
Called upon Initialization }
var
  HResource: TResourceHandle;
  HResData: THandle;
  PRes: Pointer;
  InfoSize: DWORD;
  FileInfo: PVSFixedFileInfo;
  FileInfoSize: DWORD;
begin
  HResource := FindResource(HInstance, MakeIntResource(VS_VERSION_INFO), RT_VERSION);
  if HResource <> 0 then begin
    HResData:=LoadResource(HInstance, HResource);
    if HResData <> 0 then begin
      PRes:=LockResource(HResData);
      if Assigned(PRes) then begin
        InfoSize := SizeofResource(HInstance, HResource);
        if InfoSize = 0 then
          raise Exception.Create('Can''t get version information.');
        // Query the information for the version
        VerQueryValue(PRes, '\', Pointer(FileInfo), FileInfoSize);
        // Now fill in the version information
        AMajor := FileInfo.dwFileVersionMS shr 16;
        AMinor := FileInfo.dwFileVersionMS and $FFFF;
        ARelease := FileInfo.dwFileVersionLS shr 16;
        ABuild := FileInfo.dwFileVersionLS and $FFFF;
      end;
//      UnlockResource(HResData); // unnecessary per MSDN
//      FreeResource(HResData);   // unnecessary per MSDN
    end;
  end
  else
    RaiseLastOSError;
end;

procedure GetFileVersionNumbers(out AMajor, AMinor, ARelease, ABuild: Integer);
{ Get the file version information. Called upon Initialization }
var
  PInfo: Pointer;
  InfoSize: DWORD;
  FileInfo: PVSFixedFileInfo;
  FileInfoSize: DWORD;
  FileName: string;
  Tmp: DWORD;
begin
  FileName := ParamStr(0);
  // 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(PInfo, InfoSize);
  try
    // Get the information
    GetFileVersionInfo(PChar(FileName), 0, InfoSize, PInfo);
    // Query the information for the version
    VerQueryValue(PInfo, '\', Pointer(FileInfo), FileInfoSize);
    // Now fill in the version information
    AMajor := FileInfo.dwFileVersionMS shr 16;
    AMinor := FileInfo.dwFileVersionMS and $FFFF;
    ARelease := FileInfo.dwFileVersionLS shr 16;
    ABuild := FileInfo.dwFileVersionLS and $FFFF;
  finally
    FreeMem(PInfo);
  end;
end;

var
  gMajorVersion: Integer = 0;
  gMinorVersion: Integer = 0;
  gRelease: Integer = 0;
  gBuild: Integer = 0;

initialization
  GetResourceVersionNumbers(gMajorVersion, gMinorVersion, gRelease, gBuild);
//  GetFileVersionNumbers(gMajorVersion, gMinorVersion, gRelease, gBuild);
  ShowMessage(Format('major: %d, minor: %d, release: %d, build: %d',
    [gMajorVersion, gMinorVersion, gRelease, gBuild]));
end.

Trending Stories

  • Easily Learn How To Use Python List Comprehensions In A...

  • Powerful Native Open Source Hard Disk, Folder and Storage Analyzer...

  • Incredible Database Development And Visualization Tool Is Built In Delphi

  • User Defined Functions with InterBase

  • Learn How To Use C++ Extending Sizeof For Windows Development

Embarcadero GetIt

  • Brook Framework

    Microframework which helps to develop web Pascal applications.

  • Trial - TMS Scripter

    Add the ultimate flexibility and power into your apps with native Pascal or Basic scripting and […]

  • Trial - TMS VCL Chart

    DB-aware and non DB-aware feature-rich charting components for business, statistical, financial […]

  • Trial - TMS VCL Cloud Pack

    TMS VCL Cloud Pack is a Delphi and C++Builder component library to seamlessly use all major cloud […]

  • Trial - TMS VCL UI Pack

    Create modern-looking & feature-rich Windows applications faster with well over 600 components […]

  • Learn Delphi Programming
  • Learn C++
  • Embarcadero Blogs
  • BeginEnd.net
  • Python GUI
  • Firebird News
  • Torry’s Delphi Pages
Copyright DelphiFeeds.com 2021. All Rights Reserved
Embarcadero
Login Register

Login

Lost Password

Register

Lost Password