在Qt中获取运行时架构信息

3

使用Qt,我如何在运行时发现用户的系统是Win7-32还是Win7-64?

2个回答

2

据我所知,使用Qt无法独立完成此操作。以下是如何实现该操作:

#include <windows.h>
#include <tchar.h>
#include <QtCore/QSysInfo>

typedef enum { Win_64, Win_32, Error, Other } OsType;

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

OsType checkOS() {
#ifndef Q_OS_WIN32
    return Other;
#else
    // An application compiled for 64 bits can only run on a 64 bit os,
    // so no need to check any further.
    if (QSysInfo::WordSize == 64) return Win7_64;
    // A 32 bit application may be running on a 64 bit OS.
    BOOL is64 = FALSE;
    // IsWow64Process may not be available in kernel32 on all Windows versions, so we bind to it
    // at runtime.
    LPFN_ISWOW64PROCESS fnIsWow64Process;
    fnIsWow64Process = (LPFN_ISWOW64PROCESS)
            GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
    // No way it's a 64 bit OS if it doesn't have this API.
    if (fnIsWow64Process == NULL) return Win_32;
    // Note that GetCurrentProcess() can't fail.
    if (!IsWow64Process(GetCurrentProcess(), &is64)) return Error; // The check has failed.
    return is64 ? Win_64 : Win_32;
#endif
}

谢谢你的回复兄弟...我也想知道如何在Win XP上解决这个问题。你能否再提供一个Win XP的解决方案? - Viku
你想特别检查Win 7,所以我添加了一个检查。只需删除那行代码即可使其适用于任何64位版本。未来的提示:如果您想要更一般的答案,请提出更一般的问题。不要包含不必要的细节。在您的情况下,在问题中加入“Windows 7”是适得其反的。 - Kuba hasn't forgotten Monica

1
在Qt 5的情况下,您可以使用QSysInfo静态函数,例如prettyProductName()currentCpuArchitecture()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接