fileinfo.pp 4.6 KB

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