获取系统位数

#include <ntddk.h>  



typedef struct _SYSTEM_PROCESSOR_INFORMATION {
	USHORT ProcessorArchitecture;
	USHORT ProcessorLevel;
	USHORT ProcessorRevision;
	USHORT Reserved;
	ULONG ProcessorFeatureBits;
} SYSTEM_PROCESSOR_INFORMATION, *PSYSTEM_PROCESSOR_INFORMATION;

typedef enum _SYSTEM_INFORMATION_CLASS {
	SystemBasicInformation,
	SystemProcessorInformation,             // obsolete...delete
	SystemPerformanceInformation,
	SystemTimeOfDayInformation,
	SystemPathInformation,
	SystemProcessInformation,
	SystemCallCountInformation,
	SystemDeviceInformation,
	SystemProcessorPerformanceInformation,
	SystemFlagsInformation,
	SystemCallTimeInformation,
	SystemModuleInformation,
	SystemLocksInformation,
	SystemStackTraceInformation,
	SystemPagedPoolInformation,
	SystemNonPagedPoolInformation,
	SystemHandleInformation,
	SystemObjectInformation,
	SystemPageFileInformation,
	SystemVdmInstemulInformation,
	SystemVdmBopInformation,
	SystemFileCacheInformation,
	SystemPoolTagInformation,
	SystemInterruptInformation,
	SystemDpcBehaviorInformation,
	SystemFullMemoryInformation,
	SystemLoadGdiDriverInformation,
	SystemUnloadGdiDriverInformation,
	SystemTimeAdjustmentInformation,
	SystemSummaryMemoryInformation,
	SystemMirrorMemoryInformation,
	SystemPerformanceTraceInformation,
	SystemObsolete0,
	SystemExceptionInformation,
	SystemCrashDumpStateInformation,
	SystemKernelDebuggerInformation,
	SystemContextSwitchInformation,
	SystemRegistryQuotaInformation,
	SystemExtendServiceTableInformation,
	SystemPrioritySeperation,
	SystemVerifierAddDriverInformation,
	SystemVerifierRemoveDriverInformation,
	SystemProcessorIdleInformation,
	SystemLegacyDriverInformation,
	SystemCurrentTimeZoneInformation,
	SystemLookasideInformation,
	SystemTimeSlipNotification,
	SystemSessionCreate,
	SystemSessionDetach,
	SystemSessionInformation,
	SystemRangeStartInformation,
	SystemVerifierInformation,
	SystemVerifierThunkExtend,
	SystemSessionProcessInformation,
	SystemLoadGdiDriverInSystemSpace,
	SystemNumaProcessorMap,
	SystemPrefetcherInformation,
	SystemExtendedProcessInformation,
	SystemRecommendedSharedDataAlignment,
	SystemComPlusPackage,
	SystemNumaAvailableMemory,
	SystemProcessorPowerInformation,
	SystemEmulationBasicInformation,
	SystemEmulationProcessorInformation,
	SystemExtendedHandleInformation,
	SystemLostDelayedWriteInformation,
	SystemBigPoolInformation,
	SystemSessionPoolTagInformation,
	SystemSessionMappedViewInformation,
	SystemHotpatchInformation,
	SystemObjectSecurityMode,
	SystemWatchdogTimerHandler,
	SystemWatchdogTimerInformation,
	SystemLogicalProcessorInformation,
	SystemWow64SharedInformation,
	SystemRegisterFirmwareTableInformationHandler,
	SystemFirmwareTableInformation,
	SystemModuleInformationEx,
	SystemVerifierTriageInformation,
	SystemSuperfetchInformation,
	SystemMemoryListInformation,
	SystemFileCacheInformationEx,
	MaxSystemInfoClass  // MaxSystemInfoClass should always be the last enum
} SYSTEM_INFORMATION_CLASS;


#define PROCESSOR_ARCHITECTURE_INTEL            0
#define PROCESSOR_ARCHITECTURE_MIPS             1
#define PROCESSOR_ARCHITECTURE_ALPHA            2
#define PROCESSOR_ARCHITECTURE_PPC              3
#define PROCESSOR_ARCHITECTURE_SHX              4
#define PROCESSOR_ARCHITECTURE_ARM              5
#define PROCESSOR_ARCHITECTURE_IA64             6
#define PROCESSOR_ARCHITECTURE_ALPHA64          7
#define PROCESSOR_ARCHITECTURE_MSIL             8
#define PROCESSOR_ARCHITECTURE_AMD64            9
#define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64    10


NTSTATUS ZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,PVOID SystemInformation,ULONG SystemInformationLength, PULONG ReturnLength);

VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
{
	return;
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{

	DriverObject->DriverUnload = DriverUnload;

	DbgBreakPoint();


	ULONG ReturnLength;
	SYSTEM_PROCESSOR_INFORMATION SysProcessorInfo = {0};
	NTSTATUS status = ZwQuerySystemInformation(SystemProcessorInformation, &SysProcessorInfo, sizeof(SYSTEM_PROCESSOR_INFORMATION), &ReturnLength);
	if (NT_SUCCESS(status))
	{
		if (SysProcessorInfo.ProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
		{
			KdPrint(("win64\n"));
		}
		else if (SysProcessorInfo.ProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
		{
			KdPrint(("win32\n"));
		}
		else
		{
			KdPrint(("no\n"));
		}
	}

	return STATUS_SUCCESS;
}




<think>好的,我现在需要解决用户的问题:如何在Java中获取系统位数而不依赖JVM的位数。用户之前可能已经知道通过System.getProperty("os.arch")会受到JVM的影响,所以现在想找不依赖JVM的方法。首先,回忆之前的引用内容,特别是引用[1]提到System.getProperty("os.arch")返回的是JVM的位数而非操作系统的实际架构。例如,在64位Linux上,32位JVM会返回x86,而64位JVM返回amd64。这说明直接使用系统属性不可行,必须另寻他法。接下来,我需要考虑Java中是否有其他方法可以获取系统信息而不受JVM影响。可能的思路包括调用本地命令或使用其他API。例如,在Windows系统中,可以通过系统环境变量或注册表信息;在Linux或MacOS中,可以执行uname-m命令来获取架构信息。然后,思考如何用Java实现这些操作。可以使用Runtime.getRuntime().exec()来执行系统命令,并读取输出结果。例如,执行uname-m可能会返回x86_64表示64位系统,i686表示32位。但需要注意不同操作系统的命令差异,比如Windows可能需要使用wmicosgetosarchitecture或者检查环境变量PROCESSOR_ARCHITECTURE。此外,还要处理命令执行可能出现的异常,比如权限问题或命令不存在的情况。需要捕获IOException和InterruptedException,确保程序健壮性。同时,解析命令输出的字符串,提取正确的系统位数信息。另外,可能需要分平台处理,因为不同系统的命令和输出格式不同。例如,在Windows中,可以通过检查环境变量中的“ProgramFiles(x86)”是否存在来判断是否为64位系统,但这种方法可能不够可靠。还要考虑Java的跨平台特性,确保代码在不同操作系统上都能正确运行。可能需要编写条件判断,根据当前操作系统选择相应的命令或检查方法。最后,总结这些方法,给出示例代码,并指出各方法的优缺点,比如依赖特定命令可能存在兼容性问题,或者环境变量可能被修改导致结果不准确。同时提醒用户这些方法虽然不直接依赖JVM属性,但仍有其他依赖,需要根据实际需求选择合适的方法。</think>在Java中获取操作系统位数(32位或64位)而不依赖JVM自身位数的方法,可以通过以下两种方式实现: ### 方法1:调用本地系统命令(跨平台) 通过执行操作系统命令直接获取硬件架构信息: ```java public static String getRealOSArchitecture() { String os = System.getProperty("os.name").toLowerCase(); try { Process process; if (os.contains("win")) { // Windows系统 process = Runtime.getRuntime().exec("cmd /c echo %PROCESSOR_ARCHITECTURE%"); } else { // Linux/Mac系统 process = Runtime.getRuntime().exec("uname -m"); } try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()))) { String result = reader.readLine().trim(); process.waitFor(); // 解析结果 if (result.contains("64")) return "64-bit"; else if (result.contains("86")) return "32-bit"; else return "Unknown: " + result; } } catch (IOException | InterruptedException e) { return "检测失败"; } } ``` ### 方法2:检查系统环境特征(Windows专用) 通过检查特定环境变量路径判断: ```java public static String checkWin64ByEnv() { // 检查是否存在64位程序目录 String programFilesX86 = System.getenv("ProgramFiles(x86)"); return (programFilesX86 != null) ? "64-bit" : "32-bit"; } ``` ### 技术细节说明 1. **命令执行方法**的优缺点: - ✅ 直接获取硬件架构信息 - ❗ 需处理命令执行权限和异常 - ❗ Windows输出可能为`AMD64`,Linux可能输出`x86_64`或`aarch64` 2. **环境变量方法**的限制: - ✅ Windows系统专用快速判断 - ❗ 依赖系统环境变量配置的完整性 - ❗ 不适用于Linux/Mac系统 ### 验证示例 当在64位Windows系统上运行: ```java System.out.println(getRealOSArchitecture()); // 输出"64-bit" System.out.println(checkWin64ByEnv()); // 输出"64-bit" ``` ### 注意事项 - 所有方法最终仍通过JVM执行,但检测目标已从JVM转向操作系统本身 - 建议组合使用两种方法以提高可靠性 - 需要处理不同操作系统的路径分隔符差异(Windows用`\`,Linux/Mac用`/`)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值