4

I'm using this code to capture compilation date of a executable (.exe). I'm searching a solution to achieve the same to a .dll file. When this code is inserted in a dll project and injected/or using exported function in some executable (.exe), the compilation date retrieved is of executable and not of dll.

function GetExeBuildTime: TDateTime;
var
  LI: TLoadedImage;
{$IF CompilerVersion >= 26.0}
  m: TMarshaller;
{$IFEND}
  timeStamp: Cardinal;
  utcTime: TDateTime;
begin
{$IF CompilerVersion >= 26.0}
  Win32Check(MapAndLoad(PAnsiChar(m.AsAnsi(ParamStr(0)).ToPointer), nil, @LI,
    False, True));
{$ELSE}
  Win32Check(MapAndLoad(PAnsiChar(AnsiString(ParamStr(0))), nil, @LI,
    False, True));
{$IFEND}
  timeStamp := LI.FileHeader.FileHeader.TimeDateStamp;
  UnMapAndLoad(@LI);

  utcTime := UnixToDateTime(timeStamp);
  Result := TTimeZone.local.ToLocalTime(utcTime);
end;

Usage:

FormatDateTime('dd/mm/yyyy', GetExeBuildTime);

1 Answers1

3

Your code example is using ParamStr(0) to retrieve path to the executable file of your application that is the passed to MapANdLoad function to load PE Image of the executable into memory.

You need to replace the ParamStr(0) with the file location of your DLL file in order to retrieve this information from your DLL file.

Here is your function modified so that it accepts path to the executable or DLL as input parameter.

function GetExeBuildTime(ExecutablePath: String): TDateTime;
var
  LI: TLoadedImage;
{$IF CompilerVersion >= 26.0}
  m: TMarshaller;
{$IFEND}
  timeStamp: Cardinal;
  utcTime: TDateTime;
begin
{$IF CompilerVersion >= 26.0}
  Win32Check(MapAndLoad(PAnsiChar(m.AsAnsi(ExecutablePath).ToPointer), nil, @LI,
    False, True));
{$ELSE}
  Win32Check(MapAndLoad(PAnsiChar(AnsiString(ExecutablePath)), nil, @LI,
    False, True));
{$IFEND}
  timeStamp := LI.FileHeader.FileHeader.TimeDateStamp;
  UnMapAndLoad(@LI);

  utcTime := UnixToDateTime(timeStamp);
  Result := TTimeZone.local.ToLocalTime(utcTime);
end;
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • Hint: this is unbound to the platform - you can also check a 64 bit DLL in your 32 bit EXE with this. OS/2 and 16 bit DLLs/EXEs don't have such timestamp at all. – AmigoJack Jul 30 '21 at 10:42