BOOL isWin32Exe (char *FileName)
{
BYTE *buffer;
HANDLE hFile;
DWORD peLocation, d, FileSize;
BOOL retval = 0; // PRESUME ERREUR
hFile = CreateFile(FileName, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if(hFile == INVALID_HANDLE_VALUE) goto exeEXIT;
FileSize = GetFileSize(hFile, 0);
if(GetLastError()) goto closeEXE;
if(FileSize <= 0x3F) goto closeEXE;
buffer = (BYTE*) VirtualAlloc(0, FileSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if(!buffer) goto closeEXE;
d = 0; ReadFile(hFile, buffer, FileSize, &d, 0);
if(d != FileSize) goto relMEM; // ERREUR LECTURE
if(*((WORD*) buffer) != 0x5A4D) goto relMEM; // MZ au début
peLocation = *((DWORD*) (buffer + 60)); // pointeur vers l'offset PE
if(peLocation >= FileSize) goto relMEM; // pointeur pointerait hors buffer alors on quitte
if(*((WORD*) (buffer + peLocation)) == 0x4550) retval = TRUE; // signature PE, module Win32
relMEM: VirtualFree(buffer, 0, MEM_RELEASE);
closeEXE: CloseHandle(hFile);
exeEXIT: return retval;
}