Data kompilacji pliku

0

Witam,
Czy Delphi zapisuje datę kompilacji w pliku exe?
Jeżeli tak, to jak można ją odczytać?

3

Znalezione gdzieś kiedyś w sieci:

uses
 DateUtils;

function GetImageLinkTimeStamp(const FileName: string): DWORD;
const
  INVALID_SET_FILE_POINTER = DWORD(-1);
  BorlandMagicTimeStamp = $2A425E19; // Delphi 4-6 (and above?)
  FileTime1970: TFileTime = (dwLowDateTime:$D53E8000; dwHighDateTime:$019DB1DE);
type
  PImageSectionHeaders = ^TImageSectionHeaders;
  TImageSectionHeaders = array [Word] of TImageSectionHeader;
type
  PImageResourceDirectory = ^TImageResourceDirectory;
  TImageResourceDirectory = packed record
    Characteristics: DWORD;
    TimeDateStamp: DWORD;
    MajorVersion: Word;
    MinorVersion: Word;
    NumberOfNamedEntries: Word;
    NumberOfIdEntries: Word;
  end;
var
  FileHandle: THandle;
  BytesRead: DWORD;
  ImageDosHeader: TImageDosHeader;
  ImageNtHeaders: TImageNtHeaders;
  SectionHeaders: PImageSectionHeaders;
  Section: Word;
  ResDirRVA: DWORD;
  ResDirSize: DWORD;
  ResDirRaw: DWORD;
  ResDirTable: TImageResourceDirectory;
  FileTime: TFileTime;
begin
  Result := 0;
  // Open file for read access
  FileHandle := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil,
    OPEN_EXISTING, 0, 0);
  if (FileHandle <> INVALID_HANDLE_VALUE) then
  try
    // Read MS-DOS header to get the offset of the PE32 header
    // (not required on WinNT based systems - but mostly available)
    if not ReadFile(FileHandle, ImageDosHeader, SizeOf(TImageDosHeader),
      BytesRead, nil) or (BytesRead <> SizeOf(TImageDosHeader)) or
      (ImageDosHeader.e_magic <> IMAGE_DOS_SIGNATURE) then
    begin
      ImageDosHeader._lfanew := 0;
    end;
    // Read PE32 header (including optional header
    if (SetFilePointer(FileHandle, ImageDosHeader._lfanew, nil, FILE_BEGIN) =
      INVALID_SET_FILE_POINTER) then
    begin
      Exit;
    end;
    if not(ReadFile(FileHandle, ImageNtHeaders, SizeOf(TImageNtHeaders),
      BytesRead, nil) and (BytesRead = SizeOf(TImageNtHeaders))) then
    begin
      Exit;
    end;
    // Validate PE32 image header
    if (ImageNtHeaders.Signature <> IMAGE_NT_SIGNATURE) then
    begin
      Exit;
    end;
    // Seconds since 1970 (UTC)
    Result := ImageNtHeaders.FileHeader.TimeDateStamp;

    // Check for Borland's magic value for the link time stamp
    // (we take the time stamp from the resource directory table)
    if (ImageNtHeaders.FileHeader.TimeDateStamp = BorlandMagicTimeStamp) then
    with ImageNtHeaders, FileHeader, OptionalHeader do
    begin
      // Validate Optional header
      if (SizeOfOptionalHeader < IMAGE_SIZEOF_NT_OPTIONAL_HEADER) or
        (Magic <> IMAGE_NT_OPTIONAL_HDR_MAGIC) then
      begin
        Exit;
      end;
      // Read section headers
      SectionHeaders :=
        GetMemory(NumberOfSections * SizeOf(TImageSectionHeader));
      if Assigned(SectionHeaders) then
      try
        if (SetFilePointer(FileHandle,
          SizeOfOptionalHeader - IMAGE_SIZEOF_NT_OPTIONAL_HEADER, nil,
          FILE_CURRENT) = INVALID_SET_FILE_POINTER) then
        begin
          Exit;
        end;
        if not(ReadFile(FileHandle, SectionHeaders^, NumberOfSections *
          SizeOf(TImageSectionHeader), BytesRead, nil) and (BytesRead =
          NumberOfSections * SizeOf(TImageSectionHeader))) then
        begin
          Exit;
        end;
        // Get RVA and size of the resource directory
        with DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] do
        begin
          ResDirRVA := VirtualAddress;
          ResDirSize := Size;
        end;
        // Search for section which contains the resource directory
        ResDirRaw := 0;
        for Section := 0 to NumberOfSections - 1 do
        with SectionHeaders[Section] do
          if (VirtualAddress <= ResDirRVA) and
            (VirtualAddress + SizeOfRawData >= ResDirRVA + ResDirSize) then
          begin
            ResDirRaw := PointerToRawData - (VirtualAddress - ResDirRVA);
            Break;
          end;
        // Resource directory table found?
        if (ResDirRaw = 0) then
        begin
          Exit;
        end;
        // Read resource directory table
        if (SetFilePointer(FileHandle, ResDirRaw, nil, FILE_BEGIN) =
          INVALID_SET_FILE_POINTER) then
        begin
          Exit;
        end;
        if not(ReadFile(FileHandle, ResDirTable,
          SizeOf(TImageResourceDirectory), BytesRead, nil) and
          (BytesRead = SizeOf(TImageResourceDirectory))) then
        begin
          Exit;
        end;
        // Convert from DosDateTime to SecondsSince1970
        if DosDateTimeToFileTime(HiWord(ResDirTable.TimeDateStamp),
          LoWord(ResDirTable.TimeDateStamp), FileTime) then
        begin
          // FIXME: Borland's linker uses the local system time
          // of the user who linked the executable image file.
          // (is that information anywhere?)
          Result := (ULARGE_INTEGER(FileTime).QuadPart -
            ULARGE_INTEGER(FileTime1970).QuadPart) div 10000000;
        end;
      finally
        FreeMemory(SectionHeaders);
      end;
    end;
  finally
    CloseHandle(FileHandle);
  end;
end; 

Przykładowe użycie (samo-sprawdzenie programu):

Label1.Caption:= DateTimeToStr(UnixToDateTime(GetImageLinkTimeStamp(Application.ExeName))); 
0

@marogo
Dzięki. Pod Delphi7 to działa :)

Ludzie piszą:

The problem was (at least with older versions) that Delphi did not update PE header timestamp correctly.

Jest też szczepionka http://cc.embarcadero.com/Item/19823

Będę to obserwował.

Znalazłem też
http://stackoverflow.com/questions/8437300/how-can-i-automate-getting-the-date-of-build-into-a-constant-visible-to-my-code

0

Pozowliłem sobie stworzyć moduł działający w WinAPI, bo przyda mi się to w jednym z projektów. Podejrzewałem zapis takich danych w pliku wynikowym, ponieważ co kompilacje nawet niezmienionego kodu pliki wykonikowe od poprzednich znacznie się różnią. Oczywiście zamiast na sztywno zrobionej funkcji FormatC, aby mozolnie nie przenośic całego SysUtils z kodu VCL wpisałem na sztywno - taki "polski format", ale nie problem tak przerobić funkcję lub zrobić z niej procedurę aby zwracała jako parametry poprzedzone var konkretne składowe daty i czasu kompilacji. Oczywiście pod WinAPI jeżeli nic nie zaciemnia naszego kodu podajemy jako parametr na przykład ParamStr(0) lub wyniki funkcji do zwracania parametrów opisanych na MSDNie. Chociaż widziałem efekt takiego cryptera do malware, pisany bodajże w .net, który zaciemniał ParamStr(0), a kosztował ponoć 70 dolarów, także shit bo znany nam ex user forum, stwierdził po analizie cryptniętego exeka, że autor cryptera jest idiotą, ten kto kupuje za tyle takie napisane źle rozwiązanie również jest idiotą, a wszystko da się napisać inaczej i o wiele lepiej, tak aby chociaż nie przekłamywało działania cryptniętego kodu w tej kwestii. Całe szczęście, że nie kupowałem tego, ale ja nie piszę malware :)). Anyway mój moduł wrzuciłem na: http://4programmers.net/Pastebin/2360 - testowane jak to u mnie bywa pod Delphi 7 i wszystko jest raczej ok z tego, co widzę. Również dziękuję @marogo za podanie tutaj tego rozwiązania. Sam kod podanej funkcji został przeze mnie przeformatowany, dodane po ludzku begin wraz end "do pary", jak wolę oraz usunięte wszelkie Exity - przerobione na sprawdzanie poprawności. Róbcie z tym co chcecie :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1