ersionInfoSize和GetFileVersionInf

ersionInfoSize和GetFileVersionInf

本文介绍了GetFileVersionInfoSize和GetFileVersionInfo不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetFileVersionInfoSize为0(零)而且GetFileVersionInfo什么也不返回

GetFileVersionInfoSize is 0 (zero)And GetFileVersionInfo return nothing

我正在使用它,像这样:

I am using it Like this:

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;

基于堆栈回复使用Build获取FileVersion

在大多数情况下都可以使用.

It work in most of the cases.

谢谢

推荐答案

您没有检查.如果文件名无效或没有可用的版本信息,它将返回零(false),并且文档说您可以查明为什么使用.当您使用Delphi 2007时,跨平台兼容性不是问题(至少到目前为止),并且您可以使用 SysErrorMessage(GetLastError)来获取失败原因的字符串描述.

You're not checking the return value of GetFileVersionInfoSize. If the file name is invalid or there's no version info available, it will return zero (false), and the documentation says you can find out why using GetLastError. As you're using Delphi 2007, cross-platform compatibility isn't an issue (at least for now), and you can use SysErrorMessage(GetLastError) to get a string description of the reason it failed.

我写了它,以在失败时返回错误消息.当然,在实际的应用程序中,您可能不想这样做.

I've written it to return the error message if something fails; in an actual application, you probably won't want to do that, of course.

这有效(在Delphi 7,Win7 64位上测试):

This works (tested on Delphi 7, Win7 64-bit):

function FileVersion(const FileName: TFileName): String;
var
  VerInfoSize: Cardinal;
  VerValueSize: Cardinal;
  Dummy: Cardinal;
  PVerInfo: Pointer;
  PVerValue: PVSFixedFileInfo;
  iLastError: DWord;
begin
  Result := '';
  VerInfoSize := GetFileVersionInfoSize(PChar(FileName), Dummy);
  if VerInfoSize > 0 then
  begin
    GetMem(PVerInfo, VerInfoSize);
    try
      if GetFileVersionInfo(PChar(FileName), 0, VerInfoSize, PVerInfo) then
      begin
        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
      end
      else
      begin
        iLastError := GetLastError;
        Result := Format('GetFileVersionInfo failed: (%d) %s',
                      [iLastError, SysErrorMessage(iLastError)]);
      end;
    finally
      FreeMem(PVerInfo, VerInfoSize);
    end;
  end
  else
  begin
    iLastError := GetLastError;
    Result := Format('GetFileVersionInfo failed: (%d) %s',
                     [iLastError, SysErrorMessage(iLastError)]);
  end;
end;

始终测试WinAPI调用的返回值.MSDN上的文档描述了返回值和失败原因(或告诉您可以从GetLastError获取它).

Always test the return value of WinAPI calls. The documentation at MSDN describes the return values and reasons for failure (or tells you that you can get it from GetLastError).

这篇关于GetFileVersionInfoSize和GetFileVersionInfo不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:30