How to add version informations to another exes in Delphi?

只愿长相守 提交于 2019-12-04 17:54:11

Here is some working code to add or replace the version numbers:

type
 VERSIONHEADER = packed record
   wLength: word;
   wValueLength: word;
   wType: word;
   Key: array[0..16] of WideChar;   // 'VS_VERSION_INFO'
   Version: VS_FIXEDFILEINFO;
 end;

  (...)
  var ToolPath: TFileName;    // = exe containing a reference version resource
      ExeFullPath: TFileName; // = destination exe
      Maj, Min: cardinal; // expected UPDATED Version number
      VersionHandle, VersionRes: THandle;
      VersionSize: DWORD;
      Version: array of AnsiChar;
      Ver: ^VERSIONHEADER;
  (...)
  VersionSize := GetFileVersionInfoSize(pointer(ToolPath),VersionHandle);
  if (VersionSize<>0) and (Maj<>0) then
  begin
    SetLength(Version,VersionSize);
    Ver := pointer(Version);
    GetFileVersionInfo(pointer(ToolPath),0,VersionSize,Ver);
    if Ver^.Version.dwSignature=$feef04bd then
    begin
      Ver^.Version.dwFileVersionMS := MAKELONG(Min,Maj);
      Ver^.Version.dwProductVersionMS := Ver^.Version.dwFileVersionMS;
      VersionRes := BeginUpdateResource(Pointer(ExeFullPath),False);
      UpdateResource(VersionRes,RT_VERSION,MAKEINTRESOURCE(VS_VERSION_INFO),
        1033,Ver,VersionSize);
      EndUpdateResource(VersionRes,false);
    end;
  end;

It will add or update the numeric version numbers of an existing executable (ExeFullPath), replacing it with a supplied executable resource (ToolPath - may be paramstr(0) to copy some existing generic version information, or even ExeFullPath to update the version numbers).

RT_VERSION resource is not just eight bytes long. It's VERSIONINFO instead, with fixed size and variable strings. See VERSIONINFO resource - MSDN for details.

CodeProject has some sample code for you: Updating version information at run-time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!