Delphi自动获取软件版本信息的类
来源:互联网 发布:伙伴云表格mac版下载 编辑:程序博客网 时间:2024/06/04 20:11
- unit untVersion;
- interface
- uses
- Windows, Forms, SysUtils;
- type
- TVersion = class(TObject)
- private
- FCompanyName: string;
- FFileDescription: string;
- FFileVersion: string;
- FInternalName: string;
- FLegalCopyright: string;
- FLegalTradeMarks: string;
- FOriginalFileName: string;
- FProductName: string;
- FProductVersion: string;
- procedure GetCodeVersion;
- public
- constructor Create;
- property CompanyName: string read FCompanyName;
- property FileDescription: string read FFileDescription;
- property FileVersion: string read FFileVersion;
- property InternalName: string read FInternalName;
- property LegalCopyright: string read FLegalCopyright;
- property LegalTradeMarks: string read FLegalTradeMarks;
- property OriginalFileName: string read FOriginalFileName;
- property ProductName: string read FProductName;
- property ProductVersion: string read FProductVersion;
- end;
- implementation
- constructor TVersion.Create;
- begin
- GetCodeVersion;
- inherited;
- end;
- procedure TVersion.GetCodeVersion;
- const
- InfoNum = 9;
- InfoStr: array[1..InfoNum] of string = (
- 'ProductName',
- 'ProductVersion',
- 'FileDescription',
- 'LegalCopyright',
- 'FileVersion',
- 'CompanyName',
- 'LegalTradeMarks',
- 'InternalName',
- 'OriginalFileName'
- );
- var
- S: string;
- BufSize, Len: DWORD;
- Buf: PChar;
- Value: PChar;
- begin
- S := Application.ExeName;
- BufSize := GetFileVersionInfoSize(PChar(S), BufSize);
- if BufSize > 0 then begin
- Buf := AllocMem(BufSize);
- GetFileVersionInfo(PChar(S), 0, BufSize, Buf);
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[1]), Pointer(Value), Len) then
- FProductName := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[2]), Pointer(Value), Len) then
- FProductVersion := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[3]), Pointer(Value), Len) then
- FFileDescription := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[4]), Pointer(Value), Len) then
- FLegalCopyright := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[5]), Pointer(Value), Len) then
- FFileVersion := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[6]), Pointer(Value), Len) then
- FCompanyName := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[7]), Pointer(Value), Len) then
- FLegalTrademarks := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[8]), Pointer(Value), Len) then
- FInternalName := Value;
-
- if VerQueryValue(Buf, PChar('StringFileInfo/080403A8/' + InfoStr[9]), Pointer(Value), Len) then
- FOriginalFilename := Value;
- FreeMem(Buf, BufSize);
-
-
- end
- else
- begin
- Application.MessageBox('获取产品信息时遇到致命错误,请尝试重新启动软件。'+ #13 + '若仍未能解决问题,请联系产品服务人员。','错误',MB_OK + MB_ICONSTOP);
- end;
- end;