Skip to content

Instantly share code, notes, and snippets.

@kyle-go
Last active January 9, 2019 01:37
Show Gist options
  • Save kyle-go/1d2c1b0c7a97c43bb4432e8bafbf2910 to your computer and use it in GitHub Desktop.
Save kyle-go/1d2c1b0c7a97c43bb4432e8bafbf2910 to your computer and use it in GitHub Desktop.
Check Windows is x64 or not
//
// ref:https://stackoverflow.com/questions/7011071/detect-32-bit-or-64-bit-of-windows
//
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
bool is_x64_system() {
if (sizeof(void*) == 8) {
return true;
}
SYSTEM_INFO si = { 0 };
GetNativeSystemInfo(&si);
return ((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)
|| (si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64))
? true : false;
}
bool is_x64_system2() {
if (sizeof(void*) == 8) {
return true;
}
BOOL isWow64 = FALSE;
IsWow64Process(GetCurrentProcess(), &isWow64);
return isWow64 ? true : false;
}
bool is_x64_system3() {
if (sizeof(void*) == 8) {
return true;
}
char szDir[MAX_PATH] = { 0 };
auto ret = GetSystemWow64DirectoryA(szDir, MAX_PATH);
if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
return false;
}
return true;
}
bool is_x64_system4() {
if (sizeof(void*) == 8) {
return true;
}
FILE *file;
if ((file = _popen("systeminfo", "r")) != NULL) {
char cmd[1024] = { 0 };
while (fgets(cmd, 1024, file) != NULL) {
// printf("%s\n", cmd);
if (StrStrIA(cmd, "x64-based PC")) {
_pclose(file);
return true;
}
if (StrStrIA(cmd, "x86-based PC")) {
_pclose(file);
return false;
}
}
_pclose(file);
}
return false;
}
int main() {
printf("1) is_x64=%s\n", is_x64_system() ? "YES" : "NO");
printf("2) is_x64=%s\n", is_x64_system2() ? "YES" : "NO");
printf("3) is_x64=%s\n", is_x64_system3() ? "YES" : "NO");
printf("4) is_x64=%s\n", is_x64_system4() ? "YES" : "NO");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment