Windows判断是否为64位程序(C++)

本文介绍了一种检测当前运行环境是否为64位系统及程序的方法,通过使用C++编程语言,调用Windows API函数,实现了对系统架构及进程架构的判断。代码包括获取系统信息、判断系统架构、检测进程架构等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:

1.先判断是否为64位系统

2.判断是否为64位程序

代码:

// ProcessType.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//判断运行程序exe是否为64位程序

#include <iostream>
#include <Windows.h>

using namespace std;

//判断是否为64位系统
bool is64BitOS()
{
	SYSTEM_INFO cur_system_info;
	GetNativeSystemInfo(&cur_system_info);
	WORD system_str= cur_system_info.wProcessorArchitecture;
	//判断是否为64位系统
	if (system_str == PROCESSOR_ARCHITECTURE_IA64 || system_str == PROCESSOR_ARCHITECTURE_AMD64)
	{
		return true;
	}
	return false;
}

//判断是否为64位进程
//@param:进程id
/*
Parameters
hProcess
A handle to the process. The handle must have the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right. For more information, see Process Security and Access Rights.
Windows Server 2003 and Windows XP:  The handle must have the PROCESS_QUERY_INFORMATION access right.
Wow64Process
A pointer to a value that is set to TRUE if the process is running under WOW64 on an Intel64 or x64 processor. If the process is running under 32-bit Windows, the value is set to FALSE. If the process is a 32-bit application running under 64-bit Windows 10 on ARM, the value is set to FALSE. If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE.
*/
bool is64BitProcess(DWORD dwPid)
{
if (!is64BitOS())
{
	cout << "is 32 Bit OS";
	return false;
}

HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPid);
if (hProcess)
{
	typedef BOOL(WINAPI * LPEN_ISWOW64PROCESS)(HANDLE, PBOOL);
	LPEN_ISWOW64PROCESS fnlsWow64Process = (LPEN_ISWOW64PROCESS)GetProcAddress(GetModuleHandleW(L"kernel32"), "IsWow64Process");
	if (NULL!=fnlsWow64Process)
	{
		BOOL bIsWow64 = FALSE;
		fnlsWow64Process(hProcess, &bIsWow64);
		CloseHandle(hProcess);
		return !bIsWow64;
	}
}
return false;
}


int main()
{
	DWORD dwPid = GetCurrentProcessId();
	bool flag_bit = is64BitProcess(dwPid);
	cout << flag_bit << endl;
	//is64BitOS();
    //std::cout << "Hello World!\n"; 
}


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIGC布道师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值