Recent

Author Topic: Include project version info. in source code?  (Read 10191 times)

amiso

  • New Member
  • *
  • Posts: 13
    • http://alainmichaud.net
Include project version info. in source code?
« on: July 13, 2011, 05:52:41 pm »
Hi,  the Lazarus IDE keeps track of a project Version Info:

    "Version", "subversion", "revision", "build_number"...

This information can (apparently) be included in the executable, but:

Is there a way that I can import this information in my FPC code through the use of a 

                 {$INCLUDE %XXX%}

declaration, or anything else?

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: Include project version info. in source code?
« Reply #1 on: July 13, 2011, 07:34:11 pm »
Here is what I do to get that information....
I'm only grabbing the program version info, but this should be enough to get you started.

Code: [Select]
Var
  Info:     TVersionInfo;
Begin
  // grab the build number .... save it to BuildNum
  Info := TVersionInfo.Create;
  Info.Load(HINSTANCE);
  BuildNum := IntToStr(Info.FixedInfo.FileVersion[3]);
  Info.Free;

TheBlackSheep

  • Jr. Member
  • **
  • Posts: 93
Re: Include project version info. in source code?
« Reply #2 on: July 13, 2011, 10:42:56 pm »
For anyone that's interested in the other properties you can access the productname for example as in;

Info.StringFileInfo[0].Values['ProductName']

assuming you'd set this field in the Version Info properties on the relevant Project Options page.

So a ProductName + Major/Minor displayed on a label caption would look like;

Code: [Select]
 
procedure TForm1.FormCreate(Sender: TObject);
var Info: TVersionInfo;     

  function ProductVersionToString(PV: TFileProductVersion): String;
  begin
     Result := Format('%d.%d', [PV[0],PV[1]])
  end;

begin
   Info := TVersionInfo.Create;
   Info.Load(HINSTANCE);
   lblVersion.Caption := Info.StringFileInfo[0].Values['ProductName']+' '
                         +ProductVersionToString(Info.FixedInfo.FileVersion);
   Info.Free;   
...
end;
Does anyone know if this "vinfo" unit from Paul Ishenin is going to be included in the Lazarus/FPC codebase?


amiso

  • New Member
  • *
  • Posts: 13
    • http://alainmichaud.net
Re: Include project version info. in source code?
« Reply #3 on: July 15, 2011, 02:42:19 am »
Hi,
 
   unfortunately, I can not reach to the TVersionInfo. So sad...

I understand that one useful unit could be: "ProjectInfo" or "w32VersionInfo" (which one?) but the "use xxxtinfo" clause failed!

Finally, when I included some links, I got a message about the missing file Laz_XMLCfg.  I took a bad track! Obviously, I am missing something stupid...

My platform is i386-linux-gtk2, FPC 2.4.2, lazarus 0.9.30.

Thanks.
« Last Edit: July 15, 2011, 02:51:42 am by amiso »

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: Include project version info. in source code?
« Reply #4 on: July 15, 2011, 03:42:30 am »
Did you add "vinfo" to the Uses clause at the beginning of your program?
Have you downloaded "vinfo" yet?

See this thread...
http://www.lazarus.freepascal.org/index.php?topic=12435.0

amiso

  • New Member
  • *
  • Posts: 13
    • http://alainmichaud.net
Re: Include project version info. in source code?
« Reply #5 on: July 15, 2011, 04:31:06 am »
GREAT! The vinfo unit is a key element in this. thank you so much to everyone.
----------------------
unit vinfo;

{$mode objfpc}

interface

uses
  Classes, SysUtils, resource, versiontypes, versionresource;

type
  { TVersionInfo }

  TVersionInfo = class
  private
    FVersResource: TVersionResource;
    function GetFixedInfo: TVersionFixedInfo;
    function GetStringFileInfo: TVersionStringFileInfo;
    function GetVarFileInfo: TVersionVarFileInfo;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Load(Instance: THandle);
    property FixedInfo: TVersionFixedInfo read GetFixedInfo;
    property StringFileInfo: TVersionStringFileInfo read GetStringFileInfo;
    property VarFileInfo: TVersionVarFileInfo read GetVarFileInfo;
  end;

implementation

{ TVersionInfo }

function TVersionInfo.GetFixedInfo: TVersionFixedInfo;
begin
  Result := FVersResource.FixedInfo;
end;

function TVersionInfo.GetStringFileInfo: TVersionStringFileInfo;
begin
  Result := FVersResource.StringFileInfo;
end;

function TVersionInfo.GetVarFileInfo: TVersionVarFileInfo;
begin
  Result := FVersResource.VarFileInfo;
end;

constructor TVersionInfo.Create;
begin
  inherited Create;
  FVersResource := TVersionResource.Create;
end;

destructor TVersionInfo.Destroy;
begin
  FVersResource.Free;
  inherited Destroy;
end;

procedure TVersionInfo.Load(Instance: THandle);
var
  Stream: TResourceStream;
begin
  Stream := TResourceStream.CreateFromID(Instance, 1, PChar(RT_VERSION));
  try
    FVersResource.SetCustomRawDataStream(Stream);
    // access some property to load from the stream
    FVersResource.FixedInfo;
    // clear the stream
    FVersResource.SetCustomRawDataStream(nil);
  finally
    Stream.Free;
  end;
end;

end.           
« Last Edit: July 15, 2011, 04:33:49 am by amiso »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1271
Re: Include project version info. in source code?
« Reply #6 on: July 24, 2011, 06:51:18 am »
G'day,

As is vinfo.pas will raise an exception if this is called on a file that has no build information contained in it.  If you're using this in generic (shared) code, then see below for update with defensive code...

Code: [Select]
Procedure TVersionInfo.Load(Instance: THandle);
Var
  Stream: TResourceStream;
  ResID: Integer;
  Res: TFPResourceHandle;
Begin
  ResID := 1;

  // Defensive code to prevent failure if no resource available...
  Res := FindResource(Instance, PChar(PtrInt(ResID)), PChar(RT_VERSION));
  If Res = 0 Then
    Exit;

  Stream := TResourceStream.CreateFromID(Instance, ResID, PChar(RT_VERSION));
  Try
    FVersResource.SetCustomRawDataStream(Stream);

    // access some property to load from the stream
    FVersResource.FixedInfo;

    // clear the stream
    FVersResource.SetCustomRawDataStream(nil);
  Finally
    Stream.Free;
  End;
End;

Cheers

Mike Thompson
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

 

TinyPortal © 2005-2018