delphi 获取Exe或DLL的文件版本信息

unit VersionInfo;
{* 获取应用程序版本类单元 }

interface

uses Windows,Types,SysUtils,Dialogs;

type
  TExeVersionInfo = class(TObject)
  private
    FHasVerInfo: Boolean;
    FProductName: string;
    FProductVersion: string;
    FFileDescription: string;
    FLegalCopyright: string;
    FFileVersion: string;
    FCompanyName: string;
    FLegalTradeMarks: string;
    FInternalName: string;
    FOriginalFileName: string;
    FFullName: string;
    procedure SetExeOrLibFile(const Value: string);
  protected
    procedure GetVersionInfo;
  public
    property ExeOrLibFile: string read FFullName write SetExeOrLibFile;
    property HasVerInfo: Boolean read FHasVerInfo;
    property ProductName: string read FProductName;
    property ProductVersion: string read FProductVersion;
    property FileDescription: string read FFileDescription;
    property LegalCopyright: string read FLegalCopyright;
    property FileVersion: string read FFileVersion;
    property CompanyName: string read FCompanyName;
    property LegalTradeMarks: string read FLegalTradeMarks;
    property InternalName: string read FInternalName;
    property OriginalFileName: string read FOriginalFileName;
  end;

implementation


procedure TExeVersionInfo.GetVersionInfo;
const
  InfoNum = 9;
  InfoStr: array[1..InfoNum] of string = (
      'ProductName',
      'ProductVersion',
      'FileDescription',
      'LegalCopyright',
      'FileVersion',
      'CompanyName',
      'LegalTradeMarks',
      'InternalName',
      'OriginalFileName');
var
  S, Find: string;
  BufSize, Len, LangCode: DWORD;
  Buf: PChar;
  Value: PChar;
  PLang: Pointer;
begin
  FHasVerInfo := True;
  S := FFullName;
  BufSize := GetFileVersionInfoSize(PChar(S), BufSize);
  if BufSize > 0 then
  begin
    Buf := AllocMem(BufSize);
    GetFileVersionInfo(PChar(S), 0, BufSize, Buf);
    //取语言代码
    VerQueryValue(Buf, PChar('\VarFileInfo\Translation'), PLang, Len);
    LangCode := PDword(PLang)^;
    //生成查找串
    Find := Format('StringFileInfo\%.4x%.4x\', [Word(LangCode), HiWord(LangCode)]);
    //依次查找不同的信息内容
    if VerQueryValue(Buf, PChar(Find + InfoStr[1]), Pointer(Value), Len) then
       FProductName := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[2]), Pointer(Value), Len) then
       FProductVersion := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[3]), Pointer(Value), Len) then
       FFileDescription := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[4]), Pointer(Value), Len) then
       FLegalCopyright := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[5]), Pointer(Value), Len) then
       FFileVersion := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[6]), Pointer(Value), Len) then
       FCompanyName := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[7]), Pointer(Value), Len) then
       FLegalTrademarks := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[8]), Pointer(Value), Len) then
       FInternalName := Value;
    if VerQueryValue(Buf, PChar(Find + InfoStr[9]), Pointer(Value), Len) then
       FOriginalFilename := Value;
    FreeMem(Buf, BufSize);
  end else FHasVerInfo := False;
end;

procedure TExeVersionInfo.SetExeOrLibFile(const Value: string);
begin
  FFullName := Value;
  GetVersionInfo;
end;

end.



调用示例:


private
    FInfo: TExeVersionInfo;
    { Private declarations }

procedure TForm1.FormCreate(Sender: TObject);
begin
  FInfo := TExeVersionInfo.Create;
end;

procedure TForm1.btnSelectClick(Sender: TObject);
begin
  if dlgOpen.Execute then
  begin
    FInfo.ExeOrLibFile := dlgOpen.FileName;
    Caption := FInfo.FileDescription;
  end;
end;



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种编程语言,而DLL(Dynamic-Link Library)是一种模块化的文件格式,用于存储代码和数据,可以被多个应用程序共享。DLL注入是一种技术,它允许将DLL文件加载到正在运行的进程中,并使得该进程能够调用DLL中的函数和使用其中的数据。 在Delphi中实现DLL注入的方法有很多种。一种常见的方法是使用Windows API函数LoadLibrary和GetProcAddress。通过调用LoadLibrary函数,将DLL文件加载到进程的虚拟地址空间中。然后使用GetProcAddress函数获取DLL中导出函数的地址,并将其传递给需要调用的函数。通过这种方式,可以在运行时将DLL注入到目标进程中,并且通过调用DLL中的函数来扩展进程的功能。 DLL注入在实际应用中有多种用途。例如,可以使用DLL注入来为某个程序添加额外的功能或修改程序的行为。DLL注入还可以用于实现一些调试和监控的功能。通过注入DLL,可以截获程序的输入和输出,或者在程序执行某些指定的操作时进行额外的处理。 在Delphi中实现DLL注入需要一定的编程知识和技巧。需要考虑目标进程的架构和权限限制,以及如何管理注入的DLL的生命周期和资源管理。同时,还需要处理一些安全性和稳定性方面的问题,以确保注入过程不会对目标进程造成损害或崩溃。 总之,Delphi可以通过调用Windows API函数来实现DLL注入,从而扩展和修改进程的功能。但在实际应用中,需要考虑各种方面的问题,并且遵守相关的法律和规定,以确保注入操作的安全性和合法性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值