如何确定进程是32位还是64位?
How can I determine whether a process is 32 or 64 bit?
给定一个Windows进程句柄,我如何确定,使用c++代码,该进程是32位还是64位?
如果您有进程句柄,请使用IsWow64Process()
如果IsWow64Process()
报告为true,表示进程是32位运行在64位操作系统上。
如果IsWow64Process()
报告false(或者kernel32.dll
中不存在),则该进程在32位操作系统上运行为32位进程,或者在64位操作系统上运行为64位进程。要知道操作系统本身是32位还是64位,请使用GetNativeSystemInfo()
(如果kernel32.dll
中没有GetNativeSystemInfo()
,则使用GetSystemInfo()
)。
BOOL IsWow64(HANDLE process)
{
BOOL bIsWow64 = FALSE;
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
if (NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(process, &bIsWow64))
{
//handle error
}
}
return bIsWow64;
}
bool IsX86Process(HANDLE process)
{
SYSTEM_INFO systemInfo = { 0 };
GetNativeSystemInfo(&systemInfo);
// x86 environment
if (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
return true;
// Check if the process is an x86 process that is running on x64 environment.
// IsWow64 returns true if the process is an x86 process
return IsWow64(process);
}
如果你有模块句柄,那么你可以这样做:
IMAGE_NT_HEADERS * headers = ImageNtHeader(handle);
if ( headers->FileHeader.Machine == IMAGE_FILE_MACHINE_I386 )
{
//module is x86
}
else if ( headers->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64 )
{
//module is x64
}
我从自己的回答中得到了帮助。
Try
#include <Windows.h>
enum class process_architecture
{
nun,
x32,
x64
};
enum class windows_architecture
{
x32,
x64
};
windows_architecture process::get_windows_architecture()
{
#ifdef _WIN64
return windows_architecture::x64;
#else
return windows_architecture::x32;
#endif
}
process_architecture get_process_architecture(DWORD id)
{
BOOL is_wow_64 = FALSE;
HANDLE h_process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, id);
if (!h_process) return process_architecture::nun;
bool result = IsWow64Process(h_process, &is_wow_64);
CloseHandle(h_process);
if (!result) return process_architecture::nun;
if (is_wow_64) return process_architecture::x32;
else if (get_windows_architecture() == windows_architecture::x32) return process_architecture::x32;
else return process_architecture::x64;
}
如果您不想使用windows API,请尝试:
int main()
{
const int* pInt = nullptr;
if (sizeof(pInt) == 8)
{
std::cout << "64 bit process";
}
else if(sizeof(pInt) == 4)
{
std::cout << "32 bit process";
}
return 0;
}
相关文章:
- 将应用程序从32位移植到64位时出现问题
- qmake:检测目标位宽(32 位或 64 位)
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 浮点数为 32 位和 64 位二进制表示形式
- 如何在 64 位平台上计算 32 位哈希C++?
- 将代码从 32 位迁移到 64 位时出现问题 Visual Studio 2010
- 我把我的编译器从32位转换为64位,但我仍然不能使用超过2GB:(为什么
- 在node-gip binding.gyp文件中,如何根据系统结构(32位、64位)包含不同的库文件
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 是否可以使用 Wojciech Mula 算法将 __m256i 个 32 位字而不是 4 个 64 位字存储为弹出计数和结果?
- 将 VS 32 位项目转换为 64
- EDSDK 3.6 macOS - 32 位与 64 位和链接错误
- 如何将C++项目从 32 位迁移到 64 位以支持 MAC OS 10.14(莫哈韦)?
- 将 64 位整数中的每个其他位与 32 位整数进行比较
- 32位到64位:LNK2019错误,如何检索相关信息
- 在QtMultimedia的QAudioFormat中为24位,32位和64位音频选择哪种样本类型(而不是大小)?
- C/C++ 在 32 位和 64 位计算机上可执行
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 用于 64 位/32 位 IPC 的 POSIX 信号量的替代方案?
- 在 VS 移位中,UINT64 上的左移(64 >移位> 32)位仅 32 位