Last active
November 22, 2024 19:44
-
-
Save jeremyd2019/bb17f602da12e0f42aa4ff76848279e8 to your computer and use it in GitHub Desktop.
prototype code for identifying wow64 in cygwin uname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
static BOOL | |
IsWow64Process2 (HANDLE hProcess, USHORT *pProcessMachine, USHORT *pNativeMachine) | |
{ | |
typedef BOOL (WINAPI * IsWow64Process2_t) (HANDLE, USHORT *, USHORT *); | |
static IsWow64Process2_t pIsWow64Process2 = NULL; | |
static bool bIsWow64Process2Inited = false; | |
if (!bIsWow64Process2Inited) | |
{ | |
pIsWow64Process2 = (IsWow64Process2_t) GetProcAddress ( | |
GetModuleHandle("KERNEL32"), | |
"IsWow64Process2"); | |
bIsWow64Process2Inited = true; | |
} | |
if (pIsWow64Process2) | |
return pIsWow64Process2 (hProcess, pProcessMachine, pNativeMachine); | |
SetLastError (ERROR_PROC_NOT_FOUND); | |
return FALSE; | |
} | |
extern const IMAGE_DOS_HEADER | |
dosheader __asm__ ("__image_base__"); | |
static USHORT | |
current_module_machine (void) | |
{ | |
PIMAGE_NT_HEADERS ntheader = (PIMAGE_NT_HEADERS)((PBYTE) &dosheader | |
+ dosheader.e_lfanew); | |
return ntheader->FileHeader.Machine; | |
} | |
int | |
main (void) | |
{ | |
USHORT emulated, hosted; | |
if (!IsWow64Process2 (GetCurrentProcess (), &emulated, &hosted)) | |
{ | |
#if defined (__x86_64__) | |
hosted = IMAGE_FILE_MACHINE_AMD64; | |
#elif defined (__i386__) | |
BOOL wow64 = FALSE; | |
IsWow64Process (GetCurrentProcess (), &wow64); | |
hosted = wow64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_MACHINE_I386; | |
#else | |
/* this should not happen */ | |
hosted = IMAGE_FILE_MACHINE_UNKNOWN; | |
#endif | |
} | |
if (hosted != current_module_machine ()) | |
{ | |
switch (hosted) | |
{ | |
case IMAGE_FILE_MACHINE_AMD64: | |
/* special case for backwards compatibility */ | |
if (current_module_machine () == IMAGE_FILE_MACHINE_I386) | |
printf ("-WOW64"); | |
else | |
printf ("-x64"); | |
break; | |
case IMAGE_FILE_MACHINE_I386: | |
printf ("-x86"); | |
break; | |
case IMAGE_FILE_MACHINE_ARMNT: | |
printf ("-ARM"); | |
break; | |
case IMAGE_FILE_MACHINE_ARM64: | |
printf ("-ARM64"); | |
break; | |
default: | |
printf ("-%#06x", hosted); | |
break; | |
} | |
} | |
printf ("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment