#include <windows.h>
static char * _T(char *s){return s;}
static char *const_getSystemVersion()
{
SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器
GetSystemInfo(&info); //调用GetSystemInfo函数填充结构
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX); /*在调用函数前必须用sizeof(OSVERSIONINFOEXA)填充dwOSVersionInfoSize结构成员*/
if(GetVersionEx((OSVERSIONINFO *)&os)) /*调用GetVersionEx函数OSVERSIONINFOEX结构必须将指针类型强制转换*/
{
char * vname;
//下面根据版本信息判断操作系统名称
switch(os.dwMajorVersion){ //判断主版本号
case 4:
switch(os.dwMinorVersion){ //判断次版本号
case 0:
if(os.dwPlatformId==VER_PLATFORM_WIN32_NT)
vname=_T("Microsoft Windows NT 4.0"); //1996年7月发布
else if(os.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
vname=_T("Microsoft Windows 95");
break;
case 10:
vname=_T("Microsoft Windows 98");
break;
case 90:
vname=_T("Microsoft Windows Me");
break;
}
break;
case 5:
switch(os.dwMinorVersion){ //再比较dwMinorVersion的值
case 0:
vname=_T("Microsoft Windows 2000"); //1999年12月发布
break;
case 1:
vname=_T("Microsoft Windows XP"); //2001年8月发布
break;
case 2:
if(os.wProductType==VER_NT_WORKSTATION &&
info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
vname=_T("Microsoft Windows XP Professional x64 Edition");
else if(GetSystemMetrics(SM_SERVERR2)==0)
vname=_T("Microsoft Windows Server 2003"); //2003年3月发布
else if(GetSystemMetrics(SM_SERVERR2)!=0)
vname=_T("Microsoft Windows Server 2003 R2");
break;
}
break;
case 6:
switch(os.dwMinorVersion){
case 0:
if(os.wProductType==VER_NT_WORKSTATION)/*
VER_NT_WORKSTATION是桌面系统 */
vname=_T("Microsoft Windows Vista");
else
vname=_T("Microsoft Windows Server 2008"); //服务器版本
break;
case 1:
if(os.wProductType==VER_NT_WORKSTATION)
vname=_T("Microsoft Windows 7");
else
vname=_T("Microsoft Windows Server 2008 R2");
break;
}
break;
default:
vname=_T("未知Windows操作系统");
}
return vname;
}
else
return _T("获取系统版本失败!");
}
#endif
#ifdef __linux__
static char *const_getSystemVersion()
{
static char *version = NULL;
if (version) return version;
FILE *fp = fopen("/etc/issue", "r");
if (!fp) {
version = "未知的Linux操作系统";
return version;
}
version = g_new(char, 100);
memset(version, 0, sizeof(char) * 100);
fgets(version, 99, fp);
fclose(fp);
return version;
}
#endif
转载于:https://my.oschina.net/jjyuangu/blog/189166