判断程序是否运行在 Windows x64 系统下

该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/post/2013/08/22/detect-if-x64-on-runtime/

以下功能代码判断是否运行在 Windows x64 下。本例使用 Windows API 函数 IsWow64Process,具体请参考MSDN文档:http://msdn.microsoft.com/en-us/library/ms684139(VS.85).aspx

 1/**
 2 *   This program test if this application is a x64 program or
 3 *   is a x86 program running under Windows x64.
 4 *
 5 * Author:   Angus Fenying
 6 * Date:     2013-08-22
 7 */
 8#include <windows.h>
 9#include <tchar.h>
10
11typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
12
13/**
14 * Don't use the function IsWow64Process as a static function,
15 * you should load it by function GetProcAddress, because
16 * it is not available on all version of Windows.
17 */
18LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;
19
20/**
21 * This function tells if your application is a x64 program.
22 */
23BOOL Isx64Application() {
24    return (sizeof(LPFN_ISWOW64PROCESS) == 8)? TRUE: FALSE;
25}
26
27/**
28 * This function tells if you're under Windows x64.
29 */
30BOOL IsWow64() {
31
32    BOOL bIsWow64 = FALSE;
33
34    if (!fnIsWow64Process)
35        fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
36
37    if(fnIsWow64Process)
38        if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
39            return FALSE;
40
41    return bIsWow64;
42}
43
44int main( void ) {
45
46    if (Isx64Application())
47        _tprintf(TEXT("The application is a x64 program.\n"));
48    else {
49        if (!IsWow64())
50            _tprintf(TEXT("The application is running under Windows x86.\n"));
51        else
52            _tprintf(TEXT("The application is a x86 program running under Windows x64.\n"));
53    }
54
55    return 0;
56}
comments powered by Disqus