Get compilation date and time from .exe

I have the full name and path of the executable, for example. C:\IW4\BIN\iw32.exe and want to extract the compilation date and time of this executable file.

How can i do this? I did not find a suitable solution.

My C ++ program should compile under Windows using Borland C ++ Builder, if this information is of value.

EDIT: I found a sample code and it works, thanks for all your pointers and tips!

Code:

 #include <stdio.h> #include <windows.h> int main (int argc, char** argv) { WIN32_FILE_ATTRIBUTE_DATA attr; SYSTEMTIME creation; if (argc < 2) return 1; GetFileAttributesEx(argv[1], GetFileExInfoStandard, &attr); FileTimeToSystemTime(&attr.ftLastWriteTime, &creation); printf("Created: %04d-%02d-%02d %02d:%02d:%02d\n" "Size: %d bytes\n", creation.wYear, creation.wMonth, creation.wDay, creation.wHour, creation.wMinute, creation.wSecond, attr.nFileSizeLow); return 0; } 

Which provides the following conclusion:

 Created: 2013-06-20 12:37:14 Size: 15098368 
+4
source share
4 answers

The information you are looking for can be found in the IMAGE_FILE_HEADER of the executable image. You can get this information by indicating the field by its offset in the image (hard path). Another option would be to use the Image Help Library

+2
source

You can do this using MapAndLoad with the full path to the executable file name or use GetModuleFileName to extract it for the current process. It initializes the structure of LOADED_IMAGE.

In this structure you need to look for the FileHeader element, this is the IMAGE_NT_HEADERS structure. From this you should look for FileHeader, this is the IMAGE_FILE_HEADER structure. In this you can read TimeDateStamp.

The TimeDateStamp from IMAGE_FILE_HEADER is the timestamp when the image was created by the linker. It is expressed in seconds from January 1, 1970 at 00:00:00 UTC. From here you can simply convert it to time_t and call them, for example, localtime, to get the tm structure with all the necessary information.

References

MapAndLoad https://msdn.microsoft.com/en-us/library/windows/desktop/ms680353(v=vs.85).aspx

MapAndUnload https://msdn.microsoft.com/en-us/library/windows/desktop/ms680353(v=vs.85).aspx

GetModuleFileName https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx

LOADED_IMAGE https://msdn.microsoft.com/en-us/library/windows/desktop/ms680349(v=vs.85).aspx

IMAGE_NT_HEADERS https://msdn.microsoft.com/en-us/library/windows/desktop/ms680336(v=vs.85).aspx

IMAGE_FILE_HEADER https://msdn.microsoft.com/en-us/library/windows/desktop/ms680313(v=vs.85).aspx

+1
source

You can rely on the metadata of the files of the created operating system date (right-click on the executable file and select Properties ), but this will not work if the file was created differently (for example, it was copied).

Another thing you could try: The C ++ preprocessor has several macros that you can use: __DATE__ and __TIME__ . As a warning, this will only show you when the preprocessor was started, and not when the executable was fully compiled. You can save them in the program, and then display them when you run the executable file.

Indeed, the simplest solution would probably be to save the current date / time in a separate file whenever assembly is done.

0
source

If you want only one date or time (simple code)

 const char *buildDateString = "" __DATE__ "";//for date purpose const char *buildTimeString ="" __TIME__ "";//for Time purpose 
0
source

Source: https://habr.com/ru/post/1487276/


All Articles