I have the full name & path of an executable, e.g. C:\IW4\BIN\iw32.exe
, and want to extract the compilation date and time of that executable.
How can I do this? I couldn't find any suitable solution.
My C++ program must compile under Windows using Borland C++ Builder, if that info is of any worth.
EDIT: I found some sample code and it works, thanks for all your pointers and hints!
The Code is :
#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 delivers following output :
Created: 2013-06-20 12:37:14
Size: 15098368
The information your're looking for is to be found in the IMAGE_FILE_HEADER of the executable image. You can retrieve this information either by pointing the field by its offset in the image (the hard way). Another option would be to use the Image Help Library
You could rely on the operating system's 'created date' file metadata (right click on the executable and select
Properties
), but this won't work if the file was created in a different way (e.g. it was copied).Another thing you could try: The C++ preprocessor has some macros that you can use:
__DATE__
and__TIME__
. As a caveat, this will only show you when the preprocessor was run, not when the executable was fully compiled. You could save these into the program and then display them when the executable is run.Really, the easiest solution would probably be to save the current date/time in a separate file whenever a build is done.
If you want only one either date or time.(simple code)
You can do it by using MapAndLoad with the full path to the executable filename or use GetModuleFileName to retrive it for the current process. It will initialize a LOADED_IMAGE structure.
In this structure, you have to look for the FileHeader member, it is an IMAGE_NT_HEADERS structure. From this one, you have to look for the FileHeader, that is an IMAGE_FILE_HEADER structure. In this one, you can read the TimeDateStamp.
The TimeDateStamp from IMAGE_FILE_HEADER represents the timestamp when the image was created by the linker. It is expressed in seconds since January, 1, 1970 at 00:00:00 UTC. From here, you can simple convert it to a time_t and them call, for example, localtime to get a struct tm with all the information you need.
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