通过构建获取FileVersion [英] Get FileVersion with Build

查看:70
本文介绍了通过构建获取FileVersion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DelphiXe。

对于接收文件版本,我使用以下功能:

For reception of the version of a file I use function:

function FileVersion(AFileName: string): string;
var
  szName: array[0..255] of Char;
  P: Pointer;
  Value: Pointer;
  Len: UINT;
  GetTranslationString: string;
  FFileName: PChar;
  FValid: boolean;
  FSize: DWORD;
  FHandle: DWORD;
  FBuffer: PChar;
begin
  try
    FFileName := StrPCopy(StrAlloc(Length(AFileName) + 1), AFileName);
    FValid := False;
    FSize := GetFileVersionInfoSize(FFileName, FHandle);
    if FSize > 0 then
    try
      GetMem(FBuffer, FSize);
      FValid := GetFileVersionInfo(FFileName, FHandle, FSize, FBuffer);
    except
      FValid := False;
      raise;
    end;
    Result := '';
    if FValid then
      VerQueryValue(FBuffer, '\VarFileInfo\Translation', p, Len)
    else
      p := nil;
    if P <> nil then
      GetTranslationString := IntToHex(MakeLong(HiWord(Longint(P^)),
        LoWord(Longint(P^))), 8);
    if FValid then
    begin
      StrPCopy(szName, '\StringFileInfo\' + GetTranslationString +
        '\FileVersion');
      if VerQueryValue(FBuffer, szName, Value, Len) then
        Result := StrPas(PChar(Value));
    end;
  finally
    try
      if FBuffer <> nil then
        FreeMem(FBuffer, FSize);
    except
    end;
    try
      StrDispose(FFileName);
    except
    end;
  end;
end;

对于大多数已执行的文件和库,它返回正确的值。但是在某些文件中,该版本已被剪切掉,并且没有生成。
例如,这里的文件BASS.DLL(http://us.un4seen.com/files/bass24.zip)
在Windows资源管理器的文件属性中,我看到的是版本2.4.7.1,函数result = '2.4.7':(

For the majority of executed files and libraries it returns correct value. But at some files the version is cut off and shown without Build. Here for example file BASS.DLL (http://us.un4seen.com/files/bass24.zip) In Windows Explorer in properties of a file I see version 2.4.7.1, function result='2.4.7' :(

我通过Resourcehacker.exe(http://angusj.com/resourcehacker/)打开一个文件,我看到的是VersionInfo结构:

I open a file through Resourcehacker.exe (http://angusj.com/resourcehacker/), I look structure VersionInfo:

1 VERSIONINFO
FILEVERSION 2,4,7,1
PRODUCTVERSION 2,4,0,0
FILEOS 0x4
FILETYPE 0x2
{
BLOCK "StringFileInfo"
{
    BLOCK "000004b0"
    {
        VALUE "CompanyName", "Un4seen Developments"
        VALUE "FileDescription", "BASS"
        VALUE "FileVersion", "2.4.7"
        VALUE "LegalCopyright", "Copyright © 1999-2010"
    }
}

BLOCK "VarFileInfo"
{
    VALUE "Translation", 0x0000 0x04B0
}
}

问题:如何接收2.4.7.1,即完整版本?

Question: how to receive 2.4.7.1, i.e. the full version?

推荐答案

如果要使用r的文件版本oot块,然后忘记特定语言的翻译:

If you want the file version of the root block, then forget about the language specific translation:

function FileVersion(const FileName: TFileName): String;
var
  VerInfoSize: Cardinal;
  VerValueSize: Cardinal;
  Dummy: Cardinal;
  PVerInfo: Pointer;
  PVerValue: PVSFixedFileInfo;
begin
  Result := '';
  VerInfoSize := GetFileVersionInfoSize(PChar(FileName), Dummy);
  GetMem(PVerInfo, VerInfoSize);
  try
    if GetFileVersionInfo(PChar(FileName), 0, VerInfoSize, PVerInfo) then
      if VerQueryValue(PVerInfo, '\', Pointer(PVerValue), VerValueSize) then
        with PVerValue^ do
          Result := Format('v%d.%d.%d build %d', [
            HiWord(dwFileVersionMS), //Major
            LoWord(dwFileVersionMS), //Minor
            HiWord(dwFileVersionLS), //Release
            LoWord(dwFileVersionLS)]); //Build
  finally
    FreeMem(PVerInfo, VerInfoSize);
  end;
end;

这篇关于通过构建获取FileVersion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆