如何在脚本语言里检查当前的CPU类型

有时候需要得到当前机器的CPU类型,比如是不是64位CPU, 可以用环境变量进行判断。

原文如下:

The general idea is to check the following environment variables:

  • PROCESSOR_ARCHITECTURE – reports the native processor architecture EXCEPT for WOW64, where it reports x86.
  • PROCESSOR_ARCHITEW6432 – not used EXCEPT for WOW64, where it reports the original native processor architecture.

Visually, it looks like:

Environment Variable \ Program Bitness 32bit Native 64bit Native WOW64
PROCESSOR_ARCHITECTURE x86 AMD64 x86
PROCESSOR_ARCHITEW6432 undefined undefined AMD64
  • WOW64 = 32bit Program on 64bit OS
  • Replace AMD64 with IA64 for Itaniums

Detection Logic

The logic that I use from a program to detect whether the OS is 32bit or 64bit looks like this:

IF PROCESSOR_ARCHITECTURE == amd64 OR    PROCESSOR_ARCHITEW6432 == amd64 THEN    // OS is 64bit ELSE    // OS is 32bit END IF

Another way to test for the same thing is:

IF PROCESSOR_ARCHITECTURE == x86 AND    PROCESSOR_ARCHITEW6432 NOT DEFINED THEN    // OS is 32bit ELSE    // OS is 64bit END IF

本文由IT Farmer的博客创作,欢迎转载并保留对本博的链接。

Leave a Reply