c++如何获取当前系统名称

要求能够识别windows2003,windowsxpwindows7... 要求能够识别windows2003,windows xp windows7 展开
 我来答
qiaodamo
2011-10-19 · TA获得超过912个赞
知道小有建树答主
回答量:337
采纳率:100%
帮助的人:273万
展开全部
给你个函数,获得操作系统详细名字。
BOOL GetOSName( CString& csOsName )
{
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
BOOL bOsVersionInfoEx;

ZeroMemory(&si, sizeof(SYSTEM_INFO));
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
// If that fails, try using the OSVERSIONINFO structure.

osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

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

// Call GetNativeSystemInfo if supported
// or GetSystemInfo otherwise.
else GetSystemInfo(&si);

switch (osvi.dwPlatformId)
{
// Test for the Windows NT product family.

case VER_PLATFORM_WIN32_NT:

// Test for the specific product.

if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 )
{
if( osvi.wProductType == VER_NT_WORKSTATION )
{
csOsName += _T( "Windows Vista " );
}
else
{
csOsName += _T( "Windows Server \"Longhorn\" " );
}
}

if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
{
if( osvi.wProductType == VER_NT_WORKSTATION &&
si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
{
csOsName += _T( "Microsoft Windows XP Professional x64 Edition " );
}
else
{
csOsName += _T( "Microsoft Windows Server 2003, " );
}
}

if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
{
csOsName += _T( "Microsoft Windows XP " );
}

if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
{
csOsName += _T( "Microsoft Windows 2000 " );
}

if ( osvi.dwMajorVersion <= 4 )
{
csOsName += _T( "Microsoft Windows NT " );
}

// Test for specific product on Windows NT 4.0 SP6 and later.
if( bOsVersionInfoEx )
{
// Test for the workstation type.
if ( osvi.wProductType == VER_NT_WORKSTATION &&
si.wProcessorArchitecture!=PROCESSOR_ARCHITECTURE_AMD64)
{
if( osvi.dwMajorVersion == 4 )
{
csOsName += _T( "Workstation 4.0 " );
}
else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
{
csOsName += _T( "Home Edition " );
}
else
{
csOsName += _T( "Professional " );
}
}

// Test for the server type.
else if ( osvi.wProductType == VER_NT_SERVER ||
osvi.wProductType == VER_NT_DOMAIN_CONTROLLER )
{
if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==2)
{
if ( si.wProcessorArchitecture ==
PROCESSOR_ARCHITECTURE_IA64 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
{
csOsName += _T("Datacenter Edition for Itanium-based Systems");
}
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
{
csOsName += _T("Enterprise Edition for Itanium-based Systems");
}
}

else if ( si.wProcessorArchitecture ==
PROCESSOR_ARCHITECTURE_AMD64 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
{
csOsName += _T( "Datacenter x64 Edition " );
}
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
{

}
else
{
csOsName += _T( "Standard x64 Edition " );
}
}
else
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
{
csOsName += _T( "Datacenter Edition " );
}
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
{
csOsName += _T( "Enterprise Edition " );
}
else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
{
csOsName += _T( "Web Edition " );
}
else
{
csOsName += _T( "Standard Edition " );
}
}
}
else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
{
csOsName += _T( "Datacenter Server " );
}
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
{
csOsName += _T( "Advanced Server " );
}
else
{
csOsName += _T( "Server " );
}
}
else // Windows NT 4.0
{
if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
{
csOsName += _T( "Server 4.0, Enterprise Edition " );
}
else
{
csOsName += _T( "Server 4.0 " );
}
}
}
}
// Test for specific product on Windows NT 4.0 SP5 and earlier
else
{
HKEY hKey;
TCHAR szProductType[256];
DWORD dwBufLen=256*sizeof(TCHAR);
LONG lRet;

lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return FALSE;

lRet = RegQueryValueEx( hKey, TEXT("ProductType"),
NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
RegCloseKey( hKey );

if( (lRet != ERROR_SUCCESS) ||
(dwBufLen > 256*sizeof(TCHAR)) )
return FALSE;

if ( lstrcmpi( TEXT("WINNT"), szProductType) == 0 )
{
csOsName += _T( "Workstation " );
}
if ( lstrcmpi( TEXT("LANMANNT"), szProductType) == 0 )
{
csOsName += _T( "Server " );
}
if ( lstrcmpi( TEXT("SERVERNT"), szProductType) == 0 )
{
csOsName += _T( "Advanced Server " );
}
CString cstmp;
cstmp.Format( _T( "%d.%d " ), osvi.dwMajorVersion, osvi.dwMinorVersion );
csOsName += cstmp;
}

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

if( osvi.dwMajorVersion == 4 &&
lstrcmpi( osvi.szCSDVersion, TEXT("Service Pack 6") ) == 0 )
{
HKEY hKey;
LONG lRet;

// Test for SP6 versus SP6a.
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"),
0, KEY_QUERY_VALUE, &hKey );
if( lRet == ERROR_SUCCESS )
{
CString cstmp;
cstmp.Format( _T( "Service Pack 6a (Build %d)" ), osvi.dwBuildNumber & 0xFFFF );
csOsName += cstmp;
}
else // Windows NT 4.0 prior to SP6a
{
CString cstmp;
cstmp.Format( _T( "%s (Build %d)" ), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF );
csOsName += cstmp;
}
RegCloseKey( hKey );
}
else // not Windows NT 4.0
{
CString cstmp;
cstmp.Format( _T( "%s (Build %d)" ), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF );
csOsName += cstmp;
}

break;

// Test for the Windows Me/98/95.
case VER_PLATFORM_WIN32_WINDOWS:

if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
csOsName += _T( "Microsoft Windows 95 " );
if (osvi.szCSDVersion[1]=='C' || osvi.szCSDVersion[1]=='B')
{
csOsName += _T( "OSR2 " );
}
}

if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
csOsName += _T("Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1]=='A' || osvi.szCSDVersion[1]=='B')
{
csOsName += _T("SE " );
}
}

if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
csOsName += _T("Microsoft Windows Millennium Edition");
}
break;

case VER_PLATFORM_WIN32s:

csOsName += _T("Microsoft Win32s\n");
break;
}
return TRUE;
}
wind2031
2011-10-19 · TA获得超过137个赞
知道小有建树答主
回答量:321
采纳率:0%
帮助的人:188万
展开全部
GetVersionEx()
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式