获取Windows体系结构(32/64位版本)

时间:2010-07-05 13:38:36

标签: windows architecture kernel32

我遇到了一个问题:

为了获得操作系统的体系结构,问题是我的编程语言不支持这样的功能。因此我需要从windows dll(如kernel32.dll)中读取此信息 我确实尝试使用函数GetNativeSystemInfo/GetVersionEx/GetSystemInfo来获取信息 不幸的是我无法获得架构:/

是否还有其他一些函数可以在任何windows dll中读取架构? (它需要是kernel32,它可以是任何dll但它必须存在于win xp +中)

作为信息:即时通讯使用Gupta(SQLWindows / Team devoloper)

编辑1:

typedef struct _SYSTEM_INFO {
  union {
    DWORD  dwOemId;
    struct {
      WORD wProcessorArchitecture;
      WORD wReserved;
    } ;
  } ;
  DWORD     dwPageSize;
  LPVOID    lpMinimumApplicationAddress;
  LPVOID    lpMaximumApplicationAddress;
  DWORD_PTR dwActiveProcessorMask;
  DWORD     dwNumberOfProcessors;
  DWORD     dwProcessorType;
  DWORD     dwAllocationGranularity;
  WORD      wProcessorLevel;
  WORD      wProcessorRevision;
} SYSTEM_INFO;

那是来自MSDN的信息,我试图用10和12参数调用此函数 (Gupta dosnt支持结构)。
在32Bit我得到:
alt text http://img714.imageshack.us/img714/1954/32bit.gif

在64Bit我得到:
alt text http://img691.imageshack.us/img691/8978/64bit.gif

每次32位0 OemID时我都会得到吗?或更好的是64位版本的Windows上填写的OemID每个人?

求助!

电贺
AURO

2 个答案:

答案 0 :(得分:3)

GetNativeSystemInfo绝对是使用的功能。如果您的应用是原生64位应用,则GetNativeSystemInfoGetSystemInfo相同;否则,如果它在WOW64下运行,它将返回真正的系统属性,即使它是在模拟的32位环境中运行。

GetNativeSystemInfo填充SYSTEM_INFO结构,wProcessorArchitecture成员告诉您系统是32位(可能是PROCESSOR_ARCHITECTURE_INTEL)还是64位(可能是{ {1}})。

如果您的语言没有此Win API函数的包装器,要使用它,您可以像往常一样使用PROCESSOR_ARCHITECTURE_AMD64LoadLibrary,并且需要定义GetProcAddress结构当然。

更新

我会定义

SYSTEM_INFO

然后{(1}}在(公共)32位系统上,typedef struct _SYSTEM_INFO { WORD wProcessorArchitecture; WORD wReserved; DWORD dwPageSize; LPVOID lpMinimumApplicationAddress; LPVOID lpMaximumApplicationAddress; DWORD_PTR dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel; WORD wProcessorRevision; } SYSTEM_INFO; 在(公共)64位系统上。这些只是常量wProcessorArchitecture = 0wProcessorArchitecture = 9。这些是常见的32位和64位架构。 PROCESSOR_ARCHITECTURE_INTEL稍微不那么罕见,肯定是PROCESSOR_ARCHITECTURE_AMD64

更新

是的,我看到了你的问题。在C中,PROCESSOR_ARCHITECTURE_IA64 = 6表示您一次选择一个选项。也就是说,结构是

PROCESSOR_ARCHITECTURE_UNKNOWN = 65535

union

因为一个DWORD由两个字(2×2)组成的字节数(4),所以替代方案只是两种方式来对整个结构的数据进行寻址(和命名)。在我们的情况下,我们对DWORD dwOemId; DWORD dwPageSize; LPVOID lpMinimumApplicationAddress; LPVOID lpMaximumApplicationAddress; DWORD_PTR dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel; WORD wProcessorRevision; 词更感兴趣,而不是WORD wProcessorArchitecture; WORD wReserved; DWORD dwPageSize; LPVOID lpMinimumApplicationAddress; LPVOID lpMaximumApplicationAddress; DWORD_PTR dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel; WORD wProcessorRevision; 的增强wProcessorArchitecture和完全无趣的dwOemId词。

答案 1 :(得分:0)

我认为你喜欢这样,

BOOL SafeGetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo)
{
    BOOL bRet = FALSE;

    do 
    {
        if (lpSystemInfo == NULL)
        {
            break;
        }

        typedef void(WINAPI *GetNativeSystemInfoProc) (LPSYSTEM_INFO lpSystemInfo);
        GetNativeSystemInfoProc pFun = (GetNativeSystemInfoProc)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetNativeSystemInfo");
        if (NULL != pFun)
        {
            pFun(lpSystemInfo);
        }
        else
        {
            GetSystemInfo(lpSystemInfo);
        }

        bRet = TRUE;
    } while (FALSE);
    return bRet;
}


BOOL GetOSDisplayString( LPTSTR pszOS)
{
    GRS_USEPRINTF();

    OSVERSIONINFOEX osvi = {sizeof(OSVERSIONINFOEX)};
    SYSTEM_INFO si = {};
    BOOL bOsVersionInfoEx;
    DWORD dwType;

    if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
    {
        return FALSE;
    }

    //GetSystemInfo(&si);
    if (SafeGetNativeSystemInfo(&si) == FALSE)
    {
        return FALSE;
    }

    if ( VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4 )
    {
        StringCchCopy(pszOS, BUFSIZE, _T("Microsoft "));

        if ( osvi.dwMajorVersion == 6 )
        {
            if( 0 == osvi.dwMinorVersion )
            {
                if( osvi.wProductType == VER_NT_WORKSTATION )
                {
                    StringCchCat(pszOS, BUFSIZE, _T("Windows Vista "));
                }
                else
                {
                    StringCchCat(pszOS, BUFSIZE, _T("Windows Server 2008 " ));
                }

            }
            else if( 1 == osvi.dwMinorVersion )
            {
                if( osvi.wProductType == VER_NT_WORKSTATION )
                {
                    StringCchCat(pszOS, BUFSIZE, _T("Windows 7 "));
                }
                else
                {
                    StringCchCat(pszOS, BUFSIZE, _T("Windows Unknown "));
                }
            }
            else
            {
                StringCchCat(pszOS, BUFSIZE, _T("Windows Unknown "));
            }

            GetProductInfo( 6, 0, 0, 0, &dwType);
            switch( dwType )
            {
            case PRODUCT_ULTIMATE:
                StringCchCat(pszOS, BUFSIZE, _T("Ultimate Edition" ));
                break;
            case PRODUCT_HOME_PREMIUM:
                StringCchCat(pszOS, BUFSIZE, _T("Home Premium Edition" ));
                break;
            case PRODUCT_HOME_BASIC:
                StringCchCat(pszOS, BUFSIZE, _T("Home Basic Edition" ));
                break;
            case PRODUCT_ENTERPRISE:
                StringCchCat(pszOS, BUFSIZE, _T("Enterprise Edition" ));
                break;
            case PRODUCT_BUSINESS:
                StringCchCat(pszOS, BUFSIZE, _T("Business Edition" ));
                break;
            case PRODUCT_STARTER:
                StringCchCat(pszOS, BUFSIZE, _T("Starter Edition" ));
                break;
            case PRODUCT_CLUSTER_SERVER:
                StringCchCat(pszOS, BUFSIZE, _T("Cluster Server Edition" ));
                break;
            case PRODUCT_DATACENTER_SERVER:
                StringCchCat(pszOS, BUFSIZE, _T("Datacenter Edition" ));
                break;
            case PRODUCT_DATACENTER_SERVER_CORE:
                StringCchCat(pszOS, BUFSIZE, _T("Datacenter Edition (core installation)" ));
                break;
            case PRODUCT_ENTERPRISE_SERVER:
                StringCchCat(pszOS, BUFSIZE, _T("Enterprise Edition" ));
                break;
            case PRODUCT_ENTERPRISE_SERVER_CORE:
                StringCchCat(pszOS, BUFSIZE, _T("Enterprise Edition (core installation)" ));
                break;
            case PRODUCT_ENTERPRISE_SERVER_IA64:
                StringCchCat(pszOS, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems" ));
                break;
            case PRODUCT_SMALLBUSINESS_SERVER:
                StringCchCat(pszOS, BUFSIZE, _T("Small Business Server" ));
                break;
            case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
                StringCchCat(pszOS, BUFSIZE, _T("Small Business Server Premium Edition" ));
                break;
            case PRODUCT_STANDARD_SERVER:
                StringCchCat(pszOS, BUFSIZE, _T("Standard Edition" ));
                break;
            case PRODUCT_STANDARD_SERVER_CORE:
                StringCchCat(pszOS, BUFSIZE, _T("Standard Edition (core installation)" ));
                break;
            case PRODUCT_WEB_SERVER:
                StringCchCat(pszOS, BUFSIZE, _T("Web Server Edition" ));
                break;
            }

            if ( si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 )
            {
                StringCchCat(pszOS, BUFSIZE, _T( ", 64-bit" ));
            }
            else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
            {
                StringCchCat(pszOS, BUFSIZE, _T(", 64-bit"));
            }
            else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ALPHA64)
            {
                StringCchCat(pszOS, BUFSIZE, _T(", 64-bit"));
            }
            else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL )
            {
                StringCchCat(pszOS, BUFSIZE, _T(", 32-bit"));
            }
        }

        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
        {
            if( GetSystemMetrics(SM_SERVERR2) )
            {
                StringCchCat(pszOS, BUFSIZE, _T( "Windows Server 2003 R2, "));
            }
            else if ( osvi.wSuiteMask==VER_SUITE_STORAGE_SERVER )
            {
                StringCchCat(pszOS, BUFSIZE, _T( "Windows Storage Server 2003"));
            }
            else if( osvi.wProductType == VER_NT_WORKSTATION &&
                si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
            {
                StringCchCat(pszOS, BUFSIZE, _T( "Windows XP Professional x64 Edition"));
            }
            else 
            {
                StringCchCat(pszOS, BUFSIZE, _T("Windows Server 2003, "));
            }
            if ( osvi.wProductType != VER_NT_WORKSTATION )
            {
                if ( si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )
                {
                    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Datacenter Edition for Itanium-based Systems" ));
                    }
                    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Enterprise Edition for Itanium-based Systems" ));
                    }
                }

                else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
                {
                    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Datacenter x64 Edition" ));
                    }
                    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                    {                   
                        StringCchCat(pszOS, BUFSIZE, _T( "Enterprise x64 Edition" ));
                    }
                    else
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Standard x64 Edition" ));
                    }
                }

                else
                {
                    if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Compute Cluster Edition" ));
                    }
                    else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Datacenter Edition" ));
                    }
                    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Enterprise Edition" ));
                    }
                    else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Web Edition" ));
                    }
                    else
                    {
                        StringCchCat(pszOS, BUFSIZE, _T( "Standard Edition" ));
                    }
                }
            }
        }

        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
        {
            StringCchCat(pszOS, BUFSIZE, _T("Windows XP "));
            if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
            {
                StringCchCat(pszOS, BUFSIZE, _T( "Home Edition" ));
            }
            else
            {
                StringCchCat(pszOS, BUFSIZE, _T( "Professional" ));
            }
        }

        if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
        {
            StringCchCat(pszOS, BUFSIZE, _T("Windows 2000 "));

            if ( osvi.wProductType == VER_NT_WORKSTATION )
            {
                StringCchCat(pszOS, BUFSIZE, _T( "Professional" ));
            }
            else 
            {
                if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                {
                    StringCchCat(pszOS, BUFSIZE, _T( "Datacenter Server" ));
                }
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                {
                    StringCchCat(pszOS, BUFSIZE, _T( "Advanced Server" ));
                }
                else
                {
                    StringCchCat(pszOS, BUFSIZE, _T( "Server" ));
                }
            }
        }

        // Include service pack (if any) and build number.

        if( _tcslen(osvi.szCSDVersion) > 0 )
        {
            StringCchCat(pszOS, BUFSIZE, _T(" ") );
            StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
        }

        TCHAR buf[80];

        StringCchPrintf( buf, 80, _T(" (build %d)"), osvi.dwBuildNumber);
        StringCchCat(pszOS, BUFSIZE, buf);

        return TRUE; 
    }
    else
    {  
        GRS_PRINTF(_T( "This sample does not support this version of Windows.\n") );
        return FALSE;
    }
}