fileinfo.pp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {
  11. Based on getver by Bernd Juergens - Munich, Germany
  12. email :[email protected]
  13. Usage : Drop component on form. Set desired file name using
  14. FileVersionInfo.filename := 'c:\winnt\system32\comctl32.dll'
  15. or something like that.
  16. Read StringLists VersionStrings and VersionCategories.
  17. or check a single entry:
  18. FileVersionInfo1.fileName := 'd:\winnt\system32\comctl32.dll';
  19. showMessage(FileVersionInfo1.getVersionSetting('ProductVersion'));
  20. }
  21. unit fileinfo;
  22. {$mode objfpc}
  23. interface
  24. uses
  25. Windows, SysUtils, Classes;
  26. { Record to receive charset }
  27. type TTranslation = record
  28. langID : WORD;
  29. charset : WORD;
  30. end;
  31. type
  32. TFileVersionInfo = class(TComponent)
  33. private
  34. FFileName : String;
  35. FmyVersionStrings : TStringList;
  36. FmyVersionCategories : TStringList;
  37. procedure SetFileName (inp : string);
  38. procedure readVersionFromFile;
  39. protected
  40. public
  41. constructor Create(AOwner: TComponent); override;
  42. destructor Destroy; override;
  43. function getVersionSetting(inp : string): String;
  44. published
  45. property fileName : string read FFileName write SetFileName;
  46. property VersionStrings : TStringList read FmyVersionStrings;
  47. property VersionCategories : TStringList read FmyVersionCategories;
  48. end;
  49. implementation
  50. { initialize everything }
  51. constructor TFileVersionInfo.Create(AOwner: TComponent);
  52. begin
  53. inherited Create(AOwner);
  54. FmyVersionStrings := TStringList.Create;
  55. FmyVersionCategories := TStringList.Create;
  56. FFileName := '';
  57. end;
  58. destructor TFileVersionInfo.Destroy;
  59. begin
  60. FmyVersionCategories.Free;
  61. FmyVersionStrings.Free;
  62. inherited;
  63. end;
  64. { Get filename, check if file exists and read info from file }
  65. procedure TFileVersionInfo.SetFileName (inp : string);
  66. begin
  67. FmyVersionStrings.clear;
  68. FmyVersionCategories.clear;
  69. if fileexists(inp) then
  70. begin
  71. FFileName := inp;
  72. readVersionFromFile;
  73. end
  74. else
  75. begin
  76. FFileName := '';
  77. end;
  78. end;
  79. { read info from file }
  80. procedure TFileVersionInfo.readVersionFromFile;
  81. var struSize : Dword;
  82. dwBytes,someDummy : Dword;
  83. a,txt : array[0..256] of char;
  84. p : pchar;
  85. i : integer;
  86. pp : pointer;
  87. theFixedInfo : TVSFixedFileInfo;
  88. theTrans : TTranslation;
  89. s : string;
  90. ts : TStringList;
  91. begin
  92. ts := TStringList.Create;
  93. try
  94. ts.add('CompanyName');
  95. ts.add('FileDescription');
  96. ts.add('FileVersion');
  97. ts.add('InternalName');
  98. ts.add('LegalCopyright');
  99. ts.add('OriginalFilename');
  100. ts.add('ProductName');
  101. ts.add('ProductVersion');
  102. strPCopy(a,FFileName);
  103. { get size of data }
  104. struSize := GetFileVersionInfoSize(a,@someDummy);
  105. if struSize=0 then exit;
  106. p := NIL;
  107. try
  108. { get memory }
  109. GetMem(p,struSize+10);
  110. { get data }
  111. if not GetFileVersionInfo(a,0,struSize,p) then exit;
  112. { get root info }
  113. if not VerQueryValue(p,'\',pp,dwBytes) then exit;
  114. move(pp^,theFixedInfo,dwBytes);
  115. { get translation info }
  116. if not VerQueryValue(p,'\VarFileInfo\Translation',pp,dwBytes) then
  117. exit;
  118. move(pp^,theTrans,dwBytes);
  119. { iterate over defined items }
  120. for i:=0 to ts.count-1 do
  121. begin
  122. s := '\StringFileInfo\'+inttohex(theTrans.langID,4)+inttohex(theTrans.charset,4)+'\'+ts[i];
  123. StrPCopy(a,s);
  124. if not VerQueryValue(p,a,pp,dwBytes) then Continue;
  125. if dwBytes>0 then
  126. begin
  127. move(pp^,txt,dwBytes);
  128. FmyVersionCategories.add(ts[i]);
  129. FmyVersionStrings.add(StrPas(txt));
  130. end
  131. end;
  132. finally
  133. { release memory }
  134. FreeMem(p);
  135. end;
  136. finally ts.Free end;
  137. end;
  138. { get single version string }
  139. function TFileVersionInfo.getVersionSetting(inp : string): String;
  140. var i : integer;
  141. begin
  142. inp:=LowerCase(inp);
  143. for i:= 0 to FmyVersionCategories.Count -1 do
  144. if LowerCase(FmyVersionCategories[i])=inp then
  145. begin
  146. result := FmyVersionStrings[i];
  147. Exit;
  148. end;
  149. result := '';
  150. end;
  151. end.