Skip to content

Instantly share code, notes, and snippets.

@prodeveloper0
Last active February 7, 2019 08:20
Show Gist options
  • Select an option

  • Save prodeveloper0/a00602d8ed1a86385399d78616be0745 to your computer and use it in GitHub Desktop.

Select an option

Save prodeveloper0/a00602d8ed1a86385399d78616be0745 to your computer and use it in GitHub Desktop.
Get compiled time and date from current EXE program for Win32
#include <stdio.h>
#include <time.h>
#include <Windows.h>
#include <tchar.h>
DWORD GetCompiltedTime()
{
TCHAR szFilename[MAX_PATH];
IMAGE_DOS_HEADER idh;
IMAGE_NT_HEADERS inh;
DWORD dwTimeStamp = 0;
ZeroMemory(&idh, sizeof(idh));
ZeroMemory(&inh, sizeof(inh));
GetModuleFileName(NULL, szFilename, MAX_PATH);
FILE* pf = _tfopen(szFilename, TEXT("rb"));
if(pf == NULL)
goto done;
if(fread(&idh, sizeof(idh), 1, pf) != 1)
goto done;
fseek(pf, idh.e_lfanew, 0);
if(fread(&inh, sizeof(inh), 1, pf) != 1)
goto done;
dwTimeStamp = inh.FileHeader.TimeDateStamp;
done:
if(pf != NULL)
fclose(pf);
return dwTimeStamp;
}
int main(int argc, char** argv)
{
DWORD dwCompiledTime = GetCompiltedTime();
time_t timeCompiledTime = (time_t)dwCompiledTime;
tm tmCompiledTime;
char strTimeStamp[128];
localtime_s(&tmCompiledTime, &timeCompiledTime);
strftime(strTimeStamp, 128, "%Y/%m/%d %H:%M:%S", &tmCompiledTime);
printf("Current program is compiled at %s\n", strTimeStamp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment