delphi - 关于delphi引入进程信息的问题?

标签 delphi

就目前情况而言,我可以将当​​前的进程列表拉入我的 Delphi 应用程序和图像名称中。我还需要查找并提取文件描述。例如,我可以这样做:

Image name                           Description
myfile.exe              

我似乎无法做到这一点:

Image name                           Description
myfile.exe                           cool text about my file

我怎样才能提取描述?

最佳答案

下面的代码可能就是您想要的。它使用 GetFileVersionInfoSizeGetFileVersionInfo。它返回一个包含各种版本信息的 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/

相关文章:

rest - 德尔福 10.1 : How to get JSON string stored in the body parameter of a REST request (TCustomRESTRequest. 主体)?

json - 如何检查 SuperObject 中是否存在特定元素?

delphi - Delphi 中比较指针与函数值

delphi - 使用 Delphi 10 编译 SelectDirectory 第三次重载

delphi - 负属性默认值似乎不起作用

arrays - 通过带有起始和长度说明符的引用传递静态/动态数组的切片

delphi - Delphi 中的数学解析器

delphi - int64 上的 SHR 未返回预期结果

delphi - 如何使用任意字符串编码?

delphi - Indy 和 smtps : cannot connect