较旧的操作系统是32位还是64位

时间:2019-01-26 14:36:18

标签: c++ qt

从Qt Framework获取操作系统是32位还是64位的信息,以便在不同的OS上移植。

我可以从Qt信息中获取应用程序已构建为32位还是64位(QSysInfo :: buildCpuArchitecture())或CPU是32位还是64位(QSysInfo :: currentCpuArchitecture())的信息。操作系统名称(QSysInfo :: prettyProductName()),但是我还没有找到如何确定Qt操作系统是32位还是64位(应该是可移植的!)。 Qt中有任何功能可以获取该信息吗?

感谢您的时间。

qDebug() << "QSysInfo::buildCpuArchitecture():" << QSysInfo::buildCpuArchitecture();
qDebug() << "QSysInfo::currentCpuArchitecture():" << QSysInfo::currentCpuArchitecture();
qDebug() << "QSysInfo::buildAbi():" << QSysInfo::buildAbi();
qDebug() << "QSysInfo::prettyProductName():" << QSysInfo::prettyProductName();
// the result with MinGW 32-bit:
// QSysInfo::buildCpuArchitecture(): "i386"
// QSysInfo::currentCpuArchitecture(): "x86_64"
// QSysInfo::buildAbi(): "i386-little_endian-ilp32"
// QSysInfo::prettyProductName(): "Windows 10"
// the result with VC++ 64-bit:
// QSysInfo::buildCpuArchitecture(): "x86_64"
// QSysInfo::currentCpuArchitecture(): "x86_64"
// QSysInfo::buildAbi(): "x86_64-little_endian-llp64"
// QSysInfo::prettyProductName(): "Windows 10"

2 个答案:

答案 0 :(得分:0)

尽管名称另有说明,currentCpuArchitecture不会告诉您CPU是32位还是64位,但是会告诉您操作系统的位数。

  

QString QSysInfo :: currentCpuArchitecture()

     

返回应用程序正在运行的CPU的体系结构   开启,以文字格式显示。请注意,此功能取决于操作系统   如果操作系统将报告并且可能无法检测到实际的CPU体系结构   隐藏该信息或无法提供该信息。 例如,一个   在64位CPU上运行的32位OS通常无法确定   CPU实际上能够运行64位程序。

http://doc.qt.io/qt-5/qsysinfo.html#currentCpuArchitecture

答案 1 :(得分:0)

经过多次试验,我找到了一种解决方案,该解决方案还可以为32位应用程序和64位Windows提供正确的结果:

#if defined(Q_OS_WIN)
inline bool isWow64Process()
{
    // https://docs.microsoft.com/en-us/windows/desktop/api/wow64apiset/nf-wow64apiset-iswow64process

    typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
    BOOL bIsWow64 = FALSE;

    //IsWow64Process is not available on all supported versions of Windows.
    //Use GetModuleHandle to get a handle to the DLL that contains the function
    //and GetProcAddress to get a pointer to the function if available.
    LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
                GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if( NULL != fnIsWow64Process )
    {
        if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
        {
            // we couldn't get the pointer to the function,
            // we assume that app is not running as 
            // WOW64 process and return therefore FALSE
            bIsWow64 = FALSE;
        }
    }
    return bIsWow64;
}
#endif  // (_MSC_VER)

QString osInfo()
{
#if defined(Q_OS_WIN)
    QString osBitness( QSysInfo::buildAbi().contains("ilp32") && !isWow64Process()? "32-bit" : "64-bit" );
    return QString( QSysInfo::prettyProductName() + " - " + osBitness);
#else   // we do not know how to get OS bitness on Linux and OS-X (we do not mean processor, neither application, but OS!)
     return QSysInfo::prettyProductName();
#endif
}