就目前情况而言,我可以将当前的进程列表拉入我的 Delphi 应用程序和图像名称中。我还需要查找并提取文件描述。例如,我可以这样做:
Image name Description myfile.exe
我似乎无法做到这一点:
Image name Description myfile.exe cool text about my file
我怎样才能提取描述?
最佳答案
下面的代码可能就是您想要的。它使用 GetFileVersionInfoSize 和 GetFileVersionInfo。它返回一个包含各种版本信息的 TStringList。您可能需要 FileDescription 条目。它基于一些代码 from the Delphi section of About.com .
const
// Version Info sections as stored in Exe
viCompanyName = 'CompanyName';
viFileDescription = 'FileDescription';
viFileVersion = 'FileVersion';
viInternalName = 'InternalName';
viLegalCopyRight = 'LegalCopyright';
viLegalTradeMarks = 'LegalTradeMarks';
viOriginalFilename = 'OriginalFilename';
viProductName = 'ProductName';
viProductVersion = 'ProductVersion';
viComments = 'Comments';
viAuthor = 'Author';
VersionInfoNum = 11;
VersionInfoStr : array [1..VersionInfoNum] of String =
(viCompanyName,
viFileDescription,
viFileVersion,
viInternalName,
viLegalCopyRight,
viLegalTradeMarks,
viOriginalFilename,
viProductName,
viProductVersion,
viComments,
viAuthor
);
function GetFileVersionInformation(FileName : string; ListOut : TStrings) : boolean;
// Code based on the following from About.com / Delphi:
// http://delphi.about.com/cs/adptips2001/a/bltip0701_4.htm
//
// Related: http://www.delphidabbler.com/articles?article=20&printable=1
var
n, Len : DWord;
j : Integer;
Buf : PChar;
Value : PChar;
begin
Result := false;
ListOut.Clear;
n := GetFileVersionInfoSize(PChar(FileName), n);
if n > 0 Then
begin
Buf := AllocMem(n);
try
ListOut.Add('Size='+IntToStr(n));
GetFileVersionInfo(PChar(FileName),0,n,Buf);
for j:=1 To VersionInfoNum Do
begin
// this was originally working out the Locale ID for United States ($0409)
// where as we want United Kingdom ($0809)
// See notes for Chapter 22, page 978 - http://www.marcocantu.com/md4/md4update.htm
//if VerQueryValue(Buf,PChar('StringFileInfo\040904E4\'+
// InfoStr[j]),Pointer(Value),Len) then
if VerQueryValue(Buf, PChar('StringFileInfo\080904E4\' + VersionInfoStr[j]), Pointer(Value), Len) then
begin
if Length(Value) > 0 Then
begin
ListOut.Add(VersionInfoStr[j] + '=' + Value);
end;
end;
end;
finally
FreeMem(Buf,n);
Result := True;
end;
end;
end;
只需将完整的文件名和 TStringList 传递给上面的函数,然后您可以执行以下操作来获取描述:
Result := ListOut.Values[viFileDescription];
编辑 - 喜欢主要示例中的代码格式,但不认为它喜欢\'。
关于delphi - 关于delphi引入进程信息的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1113645/