[Lazarus] getting version info of active project from within a design time package

Bernd prof7bit at gmail.com
Sat Sep 29 22:05:18 CEST 2012


2012/9/28 Bernd <prof7bit at gmail.com>:
> I need to read the version number of the currently active project in
> my design time package. For this I have written the following code:
>
> function TSettings.GetVersion: String;
> var
>   Res: TAbstractProjectResources;
>   ResVer: TProjectVersionInfo;
> begin
>   Res := LazarusIDE.ActiveProject.Resources as TAbstractProjectResources;
>   ResVer := Res.Resource[TProjectVersionInfo] as TProjectVersionInfo;
>   Result := Format('%d.%d.%d.%d',  [ResVer.MajorVersionNr,
>                                     ResVer.MinorVersionNr,
>                                     ResVer.RevisionNr,
>                                     ResVer.BuildNr]);
> end;
>
> It needs the unit W32VersionInfo which is in the package LCL. Now I
> have noticed that when adding LCL as a depedency to my package I
> cannot compile the IDE from a clean directory, obviously when using
> IDE while IDE is not yet compiled seems to be not such a good idea. Is
> there a better way to get the version number? I could not find any
> other place to access it.
>
> Bernd



type

  { TMyAbstractProjectResources }
  TMyAbstractProjectResources = class(TAbstractProjectResources)
    // need this to call a protected class function
    class function GetList: TList;
  end;

  TFileProductVersion = array[0..3] of word;

  {This should have the same memory layout as the original one}
  TSameLayoutAsTProjectVersionInfo = class(TAbstractProjectResource)
    FAutoIncrementBuild: boolean;
    FHexCharSet: string;
    FHexLang: string;
    FStringTable: TObject;
    FUseVersionInfo: boolean;
    FVersion: TFileProductVersion;
    // more fields follow but we are not
    // interested anymore, only need FVersion
  end;



function TSettings.GetVersion: String;
var
  ResList: TAbstractProjectResources;
  Resource: TAbstractProjectResource;
  ResClass: TAbstractProjectResourceClass;
  ResClassList: TList;
  P: Pointer;
  VerInfo: TSameLayoutAsTProjectVersionInfo;

begin
  ResList := LazarusIDE.ActiveProject.Resources as TAbstractProjectResources;
  ResClassList := TMyAbstractProjectResources.GetList;
  for P in ResClassList do begin
    ResClass := TAbstractProjectResourceClass(P);
    Resource := ResList.Resource[ResClass];
    if Resource.ClassName = 'TProjectVersionInfo' then begin
      VerInfo := TSameLayoutAsTProjectVersionInfo(Resource);
      Result := Format('%d.%d.%d.%d', [VerInfo.FVersion[0],
                                       VerInfo.FVersion[1],
                                       VerInfo.FVersion[2],
                                       VerInfo.FVersion[3]]);
      break;
    end;
  end;
end;

As you can see I am really desperate. The problem with the above code
is that it will break as soon as there are changes made to the layout
of the first 6 fields of the TProjectVersionInfo class. I have also
tried to find any way to read from the currently opened projectinfo
file but I cannot find anything other other than CustomData (and I
don't want to read it directly from disk because there could be
unsaved changes to the version number). Is there really no "normal"
way for me to read the version number?




More information about the Lazarus mailing list