home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d6 / RX275D6.ZIP / Units / RXVERINF.PAS < prev    next >
Pascal/Delphi Source File  |  1999-10-12  |  12KB  |  430 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 1995, 1996 AO ROSNO             }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit RxVerInf;
  10.  
  11. { Working with VERSIONINFO resourse type }
  12.  
  13. {$I RX.INC}
  14.  
  15. {$IFDEF RX_D3}
  16.   {$WEAKPACKAGEUNIT}
  17. {$ENDIF}
  18.  
  19. interface
  20.  
  21. uses {$IFDEF WIN32} Windows {$ELSE} Ver {$ENDIF};
  22.  
  23. type
  24.   TVersionLanguage = (vlArabic, vlBulgarian, vlCatalan, vlTraditionalChinese,
  25.     vlCzech, vlDanish, vlGerman, vlGreek, vlUSEnglish, vlCastilianSpanish,
  26.     vlFinnish, vlFrench, vlHebrew, vlHungarian, vlIcelandic, vlItalian,
  27.     vlJapanese, vlKorean, vlDutch, vlNorwegianBokmel, vlPolish,
  28.     vlBrazilianPortuguese, vlRhaetoRomanic, vlRomanian, vlRussian,
  29.     vlCroatoSerbian, vlSlovak, vlAlbanian, vlSwedish, vlThai, vlTurkish,
  30.     vlUrdu, vlBahasa, vlSimplifiedChinese, vlSwissGerman, vlUKEnglish,
  31.     vlMexicanSpanish, vlBelgianFrench, vlSwissItalian, vlBelgianDutch,
  32.     vlNorwegianNynorsk, vlPortuguese, vlSerboCroatian, vlCanadianFrench,
  33.     vlSwissFrench, vlUnknown);
  34.  
  35.   TVersionCharSet = (vcsASCII, vcsJapan, vcsKorea, vcsTaiwan, vcsUnicode,
  36.     vcsEasternEuropean, vcsCyrillic, vcsMultilingual, vcsGreek, vcsTurkish,
  37.     vcsHebrew, vcsArabic, vcsUnknown);
  38.  
  39. {$IFNDEF WIN32}
  40.   PVSFixedFileInfo = Pvs_FixedFileInfo;
  41.   DWORD = Longint;
  42. {$ENDIF}
  43.  
  44.   TLongVersion = record
  45.     case Integer of
  46.     0: (All: array[1..4] of Word);
  47.     1: (MS, LS: LongInt);
  48.   end;
  49.  
  50. { TVersionInfo }
  51.  
  52.   TVersionInfo = class(TObject)
  53.   private
  54.     FFileName: PChar;
  55.     FValid: Boolean;
  56.     FSize: DWORD;
  57.     FBuffer: PChar;
  58.     FHandle: DWORD;
  59.     procedure ReadVersionInfo;
  60.     function GetFileName: string;
  61.     procedure SetFileName(const Value: string);
  62.     function GetTranslation: Pointer;
  63.     function GetFixedFileInfo: PVSFixedFileInfo;
  64.     function GetFileLongVersion: TLongVersion;
  65.     function GetProductLongVersion: TLongVersion;
  66.     function GetTranslationString: string;
  67.     function GetComments: string;
  68.     function GetCompanyName: string;
  69.     function GetFileDescription: string;
  70.     function GetFileVersion: string;
  71.     function GetVersionNum: Longint;
  72.     function GetInternalName: string;
  73.     function GetLegalCopyright: string;
  74.     function GetLegalTrademarks: string;
  75.     function GetOriginalFilename: string;
  76.     function GetProductVersion: string;
  77.     function GetProductName: string;
  78.     function GetSpecialBuild: string;
  79.     function GetPrivateBuild: string;
  80.     function GetVersionLanguage: TVersionLanguage;
  81.     function GetVersionCharSet: TVersionCharSet;
  82.     function GetVerFileDate: TDateTime;
  83.   public
  84.     constructor Create(const AFileName: string);
  85.     destructor Destroy; override;
  86.     function GetVerValue(const VerName: string): string;
  87.     property FileName: string read GetFileName write SetFileName;
  88.     property Valid: Boolean read FValid;
  89.     property FixedFileInfo: PVSFixedFileInfo read GetFixedFileInfo;
  90.     property FileLongVersion: TLongVersion read GetFileLongVersion;
  91.     property ProductLongVersion: TLongVersion read GetProductLongVersion;
  92.     property Translation: Pointer read GetTranslation;
  93.     property VersionLanguage: TVersionLanguage read GetVersionLanguage;
  94.     property VersionCharSet: TVersionCharSet read GetVersionCharSet;
  95.     property VersionNum: Longint read GetVersionNum;
  96.     property Comments: string read GetComments;
  97.     property CompanyName: string read GetCompanyName;
  98.     property FileDescription: string read GetFileDescription;
  99.     property FileVersion: string read GetFileVersion;
  100.     property InternalName: string read GetInternalName;
  101.     property LegalCopyright: string read GetLegalCopyright;
  102.     property LegalTrademarks: string read GetLegalTrademarks;
  103.     property OriginalFilename: string read GetOriginalFilename;
  104.     property ProductVersion: string read GetProductVersion;
  105.     property ProductName: string read GetProductName;
  106.     property SpecialBuild: string read GetSpecialBuild;
  107.     property PrivateBuild: string read GetPrivateBuild;
  108.     property Values[const Name: string]: string read GetVerValue;
  109.     property VerFileDate: TDateTime read GetVerFileDate;
  110.   end;
  111.  
  112. function LongVersionToString(const Version: TLongVersion): string;
  113. function StringToLongVersion(const Str: string): TLongVersion;
  114. function AppFileName: string;
  115. function AppVerInfo: TVersionInfo;
  116.  
  117. { Installation utility routine }
  118.  
  119. function OkToWriteModule(ModuleName: string; NewVer: Longint): Boolean;
  120.  
  121. implementation
  122.  
  123. {$IFDEF WIN32}
  124. uses SysUtils, FileUtil, DateUtil;
  125. {$ELSE}
  126. uses WinTypes, WinProcs, SysUtils, FileUtil, DateUtil, VclUtils;
  127. {$ENDIF}
  128.  
  129. function MemAlloc(Size: Longint): Pointer;
  130. {$IFDEF WIN32}
  131. begin
  132.   GetMem(Result, Size);
  133. end;
  134. {$ELSE}
  135. var
  136.   Handle: THandle;
  137. begin
  138.   if Size < 65535 then GetMem(Result, Size)
  139.   else begin
  140.     Handle := GlobalAlloc(HeapAllocFlags, Size);
  141.     Result := GlobalLock(Handle);
  142.   end;
  143. end;
  144. {$ENDIF WIN32}
  145.  
  146. const
  147.   LanguageValues: array[TVersionLanguage] of Word = ($0401, $0402, $0403,
  148.     $0404, $0405, $0406, $0407, $0408, $0409, $040A, $040B, $040C, $040D,
  149.     $040E, $040F, $0410, $0411, $0412, $0413, $0414, $0415, $0416, $0417,
  150.     $0418, $0419, $041A, $041B, $041C, $041D, $041E, $041F, $0420, $0421,
  151.     $0804, $0807, $0809, $080A, $080C, $0810, $0813, $0814, $0816, $081A,
  152.     $0C0C, $100C, $0000);
  153.  
  154. const
  155.   CharacterSetValues: array[TVersionCharSet] of Integer = (0, 932, 949, 950,
  156.     1200, 1250, 1251, 1252, 1253, 1254, 1255, 1256, -1);
  157.  
  158. { TVersionInfo }
  159.  
  160. constructor TVersionInfo.Create(const AFileName: string);
  161. begin
  162.   inherited Create;
  163.   FFileName := StrPCopy(StrAlloc(Length(AFileName) + 1), AFileName);
  164.   ReadVersionInfo;
  165. end;
  166.  
  167. destructor TVersionInfo.Destroy;
  168. begin
  169.   if FBuffer <> nil then FreeMem(FBuffer, FSize);
  170.   StrDispose(FFileName);
  171.   inherited Destroy;
  172. end;
  173.  
  174. procedure TVersionInfo.ReadVersionInfo;
  175. begin
  176.   FValid := False;
  177.   FSize := GetFileVersionInfoSize(FFileName, FHandle);
  178.   if FSize > 0 then
  179.     try
  180.       FBuffer := MemAlloc(FSize);
  181.       FValid := GetFileVersionInfo(FFileName, FHandle, FSize, FBuffer);
  182.     except
  183.       FValid := False;
  184.       raise;
  185.     end;
  186. end;
  187.  
  188. function TVersionInfo.GetFileName: string;
  189. begin
  190.   Result := StrPas(FFileName);
  191. end;
  192.  
  193. procedure TVersionInfo.SetFileName(const Value: string);
  194. begin
  195.   if FBuffer <> nil then FreeMem(FBuffer, FSize);
  196.   FBuffer := nil;
  197.   StrDispose(FFileName);
  198.   FFileName := StrPCopy(StrAlloc(Length(Value) + 1), Value);
  199.   ReadVersionInfo;
  200. end;
  201.  
  202. function TVersionInfo.GetTranslation: Pointer;
  203. var
  204. {$IFDEF WIN32}
  205.   Len: UINT;
  206. {$ELSE}
  207.   Len: Cardinal;
  208. {$ENDIF}
  209. begin
  210.   Result := nil;
  211.   if Valid then VerQueryValue(FBuffer, '\VarFileInfo\Translation', Result, Len)
  212.   else Result := nil;
  213. end;
  214.  
  215. function TVersionInfo.GetTranslationString: string;
  216. var
  217.   P: Pointer;
  218. begin
  219.   Result := '';
  220.   P := GetTranslation;
  221.   if P <> nil then
  222.     Result := IntToHex(MakeLong(HiWord(Longint(P^)), LoWord(Longint(P^))), 8);
  223. end;
  224.  
  225. function TVersionInfo.GetVersionLanguage: TVersionLanguage;
  226. var
  227.   P: Pointer;
  228. begin
  229.   P := GetTranslation;
  230.   for Result := vlArabic to vlUnknown do
  231.     if LoWord(Longint(P^)) = LanguageValues[Result] then Break;
  232. end;
  233.  
  234. function TVersionInfo.GetVersionCharSet: TVersionCharSet;
  235. var
  236.   P: Pointer;
  237. begin
  238.   P := GetTranslation;
  239.   for Result := vcsASCII to vcsUnknown do
  240.     if HiWord(Longint(P^)) = CharacterSetValues[Result] then Break;
  241. end;
  242.  
  243. function TVersionInfo.GetFixedFileInfo: PVSFixedFileInfo;
  244. var
  245. {$IFDEF WIN32}
  246.   Len: UINT;
  247. {$ELSE}
  248.   Len: Cardinal;
  249. {$ENDIF}
  250. begin
  251.   Result := nil;
  252.   if Valid then VerQueryValue(FBuffer, '\', Pointer(Result), Len)
  253.   else Result := nil;
  254. end;
  255.  
  256. function TVersionInfo.GetProductLongVersion: TLongVersion;
  257. begin
  258.   Result.MS := FixedFileInfo^.dwProductVersionMS;
  259.   Result.LS := FixedFileInfo^.dwProductVersionLS;
  260. end;
  261.  
  262. function TVersionInfo.GetFileLongVersion: TLongVersion;
  263. begin
  264.   Result.MS := FixedFileInfo^.dwFileVersionMS;
  265.   Result.LS := FixedFileInfo^.dwFileVersionLS;
  266. end;
  267.  
  268. function TVersionInfo.GetVersionNum: Longint;
  269. begin
  270.   if Valid then Result := FixedFileInfo^.dwFileVersionMS
  271.   else Result := 0;
  272. end;
  273.  
  274. function TVersionInfo.GetVerValue(const VerName: string): string;
  275. var
  276.   szName: array[0..255] of Char;
  277.   Value: Pointer;
  278. {$IFDEF WIN32}
  279.   Len: UINT;
  280. {$ELSE}
  281.   Len: Cardinal;
  282. {$ENDIF}
  283. begin
  284.   Result := '';
  285.   if Valid then begin
  286.     StrPCopy(szName, '\StringFileInfo\' + GetTranslationString + '\' + VerName);
  287.     if VerQueryValue(FBuffer, szName, Value, Len) then
  288.       Result := StrPas(PChar(Value));
  289.   end;
  290. end;
  291.  
  292. function TVersionInfo.GetComments: string;
  293. begin
  294.   Result := GetVerValue('Comments');
  295. end;
  296.  
  297. function TVersionInfo.GetCompanyName: string;
  298. begin
  299.   Result := GetVerValue('CompanyName');
  300. end;
  301.  
  302. function TVersionInfo.GetFileDescription: string;
  303. begin
  304.   Result := GetVerValue('FileDescription');
  305. end;
  306.  
  307. function TVersionInfo.GetFileVersion: string;
  308. begin
  309.   Result := GetVerValue('FileVersion');
  310.   if (Result = '') and Valid then
  311.     Result := LongVersionToString(FileLongVersion);
  312. end;
  313.  
  314. function TVersionInfo.GetInternalName: string;
  315. begin
  316.   Result := GetVerValue('InternalName');
  317. end;
  318.  
  319. function TVersionInfo.GetLegalCopyright: string;
  320. begin
  321.   Result := GetVerValue('LegalCopyright');
  322. end;
  323.  
  324. function TVersionInfo.GetLegalTrademarks: string;
  325. begin
  326.   Result := GetVerValue('LegalTrademarks');
  327. end;
  328.  
  329. function TVersionInfo.GetOriginalFilename: string;
  330. begin
  331.   Result := GetVerValue('OriginalFilename');
  332. end;
  333.  
  334. function TVersionInfo.GetProductVersion: string;
  335. begin
  336.   Result := GetVerValue('ProductVersion');
  337.   if (Result = '') and Valid then
  338.     Result := LongVersionToString(ProductLongVersion);
  339. end;
  340.  
  341. function TVersionInfo.GetProductName: string;
  342. begin
  343.   Result := GetVerValue('ProductName');
  344. end;
  345.  
  346. function TVersionInfo.GetSpecialBuild: string;
  347. begin
  348.   Result := GetVerValue('SpecialBuild');
  349. end;
  350.  
  351. function TVersionInfo.GetPrivateBuild: string;
  352. begin
  353.   Result := GetVerValue('PrivateBuild');
  354. end;
  355.  
  356. function TVersionInfo.GetVerFileDate: TDateTime;
  357. begin
  358.   if FileExists(FileName) then
  359.     Result := FileDateTime(FileName)
  360.   else Result := NullDate;
  361. end;
  362.  
  363. { Long version string routines }
  364.  
  365. function LongVersionToString(const Version: TLongVersion): string;
  366. begin
  367.   with Version do
  368.     Result := Format('%d.%d.%d.%d', [All[2], All[1], All[4], All[3]]);
  369. end;
  370.  
  371. function StringToLongVersion(const Str: string): TLongVersion;
  372. var
  373.   Sep: Integer;
  374.   Tmp, Fragment: string;
  375.   I: Byte;
  376. begin
  377.   Tmp := Str;
  378.   for I := 1 to 4 do begin
  379.     Sep := Pos('.', Tmp);
  380.     if Sep = 0 then Sep := Pos(',', Tmp);
  381.     if Sep = 0 then Fragment := Tmp
  382.     else begin
  383.       Fragment := Copy(Tmp, 1, Sep - 1);
  384.       Tmp := Copy(Tmp, Sep + 1, MaxInt);
  385.     end;
  386.     if Fragment = '' then Result.All[I] := 0
  387.     else Result.All[I] := StrToInt(Fragment);
  388.   end;
  389.   I := Result.All[1];
  390.   Result.All[1] := Result.All[2];
  391.   Result.All[2] := I;
  392.   I := Result.All[3];
  393.   Result.All[3] := Result.All[4];
  394.   Result.All[4] := I;
  395. end;
  396.  
  397. function AppFileName: string;
  398. var
  399.   FileName: array[0..255] of Char;
  400. begin
  401.   if IsLibrary then begin
  402.     GetModuleFileName(HInstance, FileName, SizeOf(FileName) - 1);
  403.     Result := StrPas(FileName);
  404.   end
  405.   else Result := ParamStr(0);
  406. end;
  407.  
  408. function AppVerInfo: TVersionInfo;
  409. begin
  410.   Result := TVersionInfo.Create(AppFileName);
  411. end;
  412.  
  413. { Installation utility routines }
  414.  
  415. function OkToWriteModule(ModuleName: string; NewVer: Longint): Boolean;
  416. { Return True if it's ok to overwrite ModuleName with NewVer }
  417. begin
  418.   {Assume we should overwrite}
  419.   OkToWriteModule := True;
  420.   with TVersionInfo.Create(ModuleName) do begin
  421.     try
  422.       if Valid then {Should we overwrite?}
  423.         OkToWriteModule := NewVer > VersionNum;
  424.     finally
  425.       Free;
  426.     end;
  427.   end;
  428. end;
  429.  
  430. end.