获得所在系统的处理器的体系结构

GetSystemInfo provides the basic system information and processor architecture of the underlying platform. This API can be used successfully in both x64 and x86 platform. But, under 64-bit WIndows, we can run 32 bit Applications( WOW64). If a WOW64 process call GetSystemInfo API, it will return the processor architecture as x86. Of course it should be the way, this API act otherwise there could compatibility problems may arise and application could act weird and show undefined behavior.

If the WOW64 process want to know the original platform it’s running, it must call GetNativeSystemInfo.When we’ve to use this? I’ve a realworld example. When we spawn process explorer (procexp.exe, it realize the underlying platform and create another exe procexp64.exe (64bit version) to iterate all process information in the system. Note that the GetNativeSystemInfo need to be called only if you 32bit application wants to run under 64 bit platform and need to care about the true underlying platform. In all other cases, call GetSystemInfo, which works across platforms uniquely.

#include "pch.h"
#include <windows.h>
#include <stdio.h>

void displayPrcessorInfo(SYSTEM_INFO &stInfo)
{
    switch (stInfo.wProcessorArchitecture)
    {
    case PROCESSOR_ARCHITECTURE_INTEL:
        printf("Processor Architecture: Intel x86\n");
        break;
    case PROCESSOR_ARCHITECTURE_IA64:
        printf("Processor Type: Intel x64\n");
        break;
    case PROCESSOR_ARCHITECTURE_AMD64:
        printf("Processor Type: AMD 64\n");
        break;
    default:
        printf("Unknown processor architecture\n");
    }
}

int main()
{
    SYSTEM_INFO stInfo;
    GetSystemInfo(&stInfo);
    displayPrcessorInfo(stInfo);

    GetNativeSystemInfo(&stInfo);
    displayPrcessorInfo(stInfo);
    return 0;
}

在 x64 平台上。
编译成 x86 运行结果:

Processor Architecture: Intel x86
Processor Type: AMD 64

编译成 x64 运行结果:

Processor Type: AMD 64
Processor Type: AMD 64
发布了7 篇原创文章 · 获赞 0 · 访问量 34

猜你喜欢

转载自blog.csdn.net/songbei6/article/details/105233855