Fenying

Angus’ Home.


22 Aug 2013

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

该文章迁移自作者的旧博客站点。
源地址:http://fenying.blog.163.com/blog/static/10205599320137224339263/

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

/**
 *   This program test if this application is a x64 program or
 *   is a x86 program running under Windows x64.
 *
 * Author:   Angus Fenying
 * Date:     2013-08-22
 */
#include <windows.h>
#include <tchar.h>

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

/**
 * Don't use the function IsWow64Process as a static function,
 * you should load it by function GetProcAddress, because
 * it is not available on all version of Windows.
 */
LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;

/**
 * This function tells if your application is a x64 program.
 */
BOOL Isx64Application() {
    return (sizeof(LPFN_ISWOW64PROCESS) == 8)? TRUE: FALSE;
}

/**
 * This function tells if you're under Windows x64.
 */
BOOL IsWow64() {

    BOOL bIsWow64 = FALSE;

    if (!fnIsWow64Process)
        fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if(fnIsWow64Process)
        if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
            return FALSE;

    return bIsWow64;
}

int main( void ) {

    if (Isx64Application())
        _tprintf(TEXT("The application is a x64 program.\n"));
    else {
        if (!IsWow64())
            _tprintf(TEXT("The application is running under Windows x86.\n"));
        else
            _tprintf(TEXT("The application is a x86 program running under Windows x64.\n"));
    }

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

comments powered by Disqus