-1

I'm using the function below to get version info from the current EXE file.

Problem is, after applying Delphi 11's November Patch , the function started to crash the application.

My code is listed below. The line that crashes is this :

VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);

And here is the error, after this the app closes. This same funcion worked without problems before the patch. Maybe a bugged update ?

enter image description here

  function TForm1.version : string;
  var
    VerInfoSize: DWord;
    VerInfo: Pointer;
    VerValueSize: DWord;
    VerValue: PVSFixedFileInfo;
    Dummy: DWord;
    sfilename: string;
  begin
    sfilename := paramstr(0);
    VerInfoSize := GetFileVersionInfoSize(pchar(sfilename), Dummy);
    GetMem(VerInfo, VerInfoSize);
    GetFileVersionInfo(pchar(sfilename), 0, VerInfoSize, VerInfo);
    VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
    with VerValue^ do
    begin
      Result := inttostr(dwFileVersionMS shr 16);
      Result := Result + '.' + inttostr(dwFileVersionMS and $FFFF);
      Result := Result + '.' + inttostr(dwFileVersionLS shr 16);
      Result := Result + '.' + inttostr(dwFileVersionLS and $FFFF);
    end;
    FreeMem(VerInfo, VerInfoSize);
  end;
delphirules
  • 6,443
  • 17
  • 59
  • 108
  • 4
    You don't do any error checking. So perhaps these API calls fail. Check the return values of the API functions for error. The documentation tells you how errors are indicated. – David Heffernan Nov 29 '21 at 11:50
  • It's also odd that you pass a size to `FreeMem`. You should not do that. – David Heffernan Nov 29 '21 at 11:53
  • 2
    @DavidHeffernan is right and probable cause is lack of error checking. Besides that your exact code works for me with patched Delphi 11. – Dalija Prasnikar Nov 29 '21 at 12:01
  • 2
    Another question.. why do you have that function as part of the `TForm1` class. It could be standalone function. – Dalija Prasnikar Nov 29 '21 at 12:05
  • 1
    Question with an answer that does error checking while retrieving version information: https://stackoverflow.com/questions/17279394/getfileversioninfosize-and-getfileversioninfo-return-nothing – Brian Nov 29 '21 at 12:54

1 Answers1

-2

Actually the problem was the EXE file did not have any version info, and an exception was generated and i was not handling it.

The code on the answer below works :

GetFileVersionInfoSize And GetFileVersionInfo return nothing

delphirules
  • 6,443
  • 17
  • 59
  • 108
  • 3
    That's not accurate. An error was reported that your code ignored, and that in turn led to a AV – David Heffernan Nov 29 '21 at 13:55
  • 1
    You link to the Q, which has the bad code. You should link to [the A with the correct code](https://stackoverflow.com/a/17286050/4299358). You still have not understood that the exception happens because of bad code and not because a VERSIONINFO is missing. – AmigoJack Nov 29 '21 at 16:50
  • @AmigoJack Ok, edited. – delphirules Nov 29 '21 at 16:59
  • Edit still gets it wrong. The exception is because your code was bad, not because the version info was missing. The problem is your code didn't check for errors and made the false assumption that errors could not occur. – David Heffernan Nov 29 '21 at 17:17