F
fontain
Unregistered / Unconfirmed
GUEST, unregistred user!
起因是想写一个对话框,忽然发现获取当前文件的版本信息很困难,曾经在网上找了很长时间相应的代码,几乎一样的原因:LangID被设置成040904e4,不同用,可是这个LangID还挺难找出来,今天终于找到了一个通用的解决法子,写了个类,大家愿意用就用用看,我不保留版权哦:),对了,如果那位仁兄做出了更完善的版本,记得给小弟发一份,不胜感谢,我这里就算抛砖引玉吧
unit APP;
interface
uses Classes, Dialogs, SysUtils,forms,types,windows;
Type
TAppObj = class
private
protected
BufChar;
public
InfoNum:integer;
constructor Create;
destructor Destroy; override;
Function Path:string;
Function Title:string;
Function Version:string;
Function FileName:string;
Function LangID:string;
Function VersionInfo(Key:string):string;overload;
Function VersionInfo(Index:Integer):string;overload;
end;
implementation
constructor TAppObj.Create;
var nWord;
S:string;
begin
S:=Application.ExeName ;
n := GetFileVersionInfoSize(PChar(S),n);
if (n>0) then begin
Buf := AllocMem;
GetFileVersionInfo(PChar(S),0,n,Buf);
end
else begin
buf:=nil;
end;
InfoNum:=11;
end;
destructor TAppObj.Destroy;
begin
if not (buf=nil) then dispose(buf);
inherited;
end;
function TAppObj.FileName: string;
begin
result:=ExtractFileNAme(Application.ExeName);
end;
function TAppObj.LangID: string;
var lenword;
l :^DWord;
j :array [1..2] of word;
begin
VerQueryValue(Buf, '/VarFileInfo/Translation',pointer(l),len);
move(l^,j,4);
Result:=format('%.4x%.4x',[j[1],j[2]]);
end;
function TAppObj.Path: string;
begin
Result:=ExtractFilePath(Application.ExeName);
end;
function TAppObj.Title: string;
begin
result:=Application.Title;
end;
function TAppObj.Version: string;
begin
Result:=VersionInfo('FileVersion');
end;
function TAppObj.VersionInfo(Key: string): string;
var KeyValueChar;
LenWord;
Sect:String;
begin
if Buf=nil then begin
Result:='';
end
else begin
Sect:=format('StringFileInfo/%s/%s',[LangID,Key]);
If VerQueryValue(Buf,PChar(sect),Pointer(KeyValue),Len) Then begin
KeyValue:= PChar(Trim(KeyValue));
result:=KeyValue;
end
else begin
result:=''
end;
end;
end;
function TAppObj.VersionInfo(Index: Integer): string;
const
InfoStr : array [1..11] of String =
('CompanyName', 'FileDescription', 'FileVersion',
'InternalName', 'LegalCopyright', 'LegalTradeMarks',
'OriginalFilename', 'ProductName', 'ProductVersion',
'Comments', 'Author');
LabelStr : array [1..11] of String =
('Company Name', 'Description', 'File Version',
'Internal Name', 'Copyright', 'TradeMarks',
'Original File Name', 'Product Name',
'Product Version', 'Comments', 'Author');
begin
Result:=format('%s=%s',[LabelStr[index],VersionInfo(InfoStr[index])]);
end;
End.
unit APP;
interface
uses Classes, Dialogs, SysUtils,forms,types,windows;
Type
TAppObj = class
private
protected
BufChar;
public
InfoNum:integer;
constructor Create;
destructor Destroy; override;
Function Path:string;
Function Title:string;
Function Version:string;
Function FileName:string;
Function LangID:string;
Function VersionInfo(Key:string):string;overload;
Function VersionInfo(Index:Integer):string;overload;
end;
implementation
constructor TAppObj.Create;
var nWord;
S:string;
begin
S:=Application.ExeName ;
n := GetFileVersionInfoSize(PChar(S),n);
if (n>0) then begin
Buf := AllocMem;
GetFileVersionInfo(PChar(S),0,n,Buf);
end
else begin
buf:=nil;
end;
InfoNum:=11;
end;
destructor TAppObj.Destroy;
begin
if not (buf=nil) then dispose(buf);
inherited;
end;
function TAppObj.FileName: string;
begin
result:=ExtractFileNAme(Application.ExeName);
end;
function TAppObj.LangID: string;
var lenword;
l :^DWord;
j :array [1..2] of word;
begin
VerQueryValue(Buf, '/VarFileInfo/Translation',pointer(l),len);
move(l^,j,4);
Result:=format('%.4x%.4x',[j[1],j[2]]);
end;
function TAppObj.Path: string;
begin
Result:=ExtractFilePath(Application.ExeName);
end;
function TAppObj.Title: string;
begin
result:=Application.Title;
end;
function TAppObj.Version: string;
begin
Result:=VersionInfo('FileVersion');
end;
function TAppObj.VersionInfo(Key: string): string;
var KeyValueChar;
LenWord;
Sect:String;
begin
if Buf=nil then begin
Result:='';
end
else begin
Sect:=format('StringFileInfo/%s/%s',[LangID,Key]);
If VerQueryValue(Buf,PChar(sect),Pointer(KeyValue),Len) Then begin
KeyValue:= PChar(Trim(KeyValue));
result:=KeyValue;
end
else begin
result:=''
end;
end;
end;
function TAppObj.VersionInfo(Index: Integer): string;
const
InfoStr : array [1..11] of String =
('CompanyName', 'FileDescription', 'FileVersion',
'InternalName', 'LegalCopyright', 'LegalTradeMarks',
'OriginalFilename', 'ProductName', 'ProductVersion',
'Comments', 'Author');
LabelStr : array [1..11] of String =
('Company Name', 'Description', 'File Version',
'Internal Name', 'Copyright', 'TradeMarks',
'Original File Name', 'Product Name',
'Product Version', 'Comments', 'Author');
begin
Result:=format('%s=%s',[LabelStr[index],VersionInfo(InfoStr[index])]);
end;
End.