#if defined(WIN32) || defined(WIN64)
typedef void (__stdcall *FUN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);
// 安全的取得真实系统信息
char* GetSystemBits()
{
SYSTEM_INFO si;
FUN_GetNativeSystemInfo pfn = (FUN_GetNativeSystemInfo)GetProcAddress(GetModuleHandleA("kernel32"), "GetNativeSystemInfo");
if (pfn)
{
pfn(&si);
switch(si.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64:
return "x64";
case PROCESSOR_ARCHITECTURE_IA64:
return "IA64";
}
}
return "x86";
}
#endif
int GetOSInfo(char *info)
{
char szComputerName[MAXBYTE] = { 0 };
char osNameType[MAXBYTE] = { 0 };
char osVersion[MAXBYTE] = { 0 };
char coreVersion[MAXBYTE] = { 0 };
char ProcessorArchitecture[MAXBYTE] = { 0 };
unsigned long nSize = MAXBYTE;
#if defined(WIN32) || defined(WIN64)
OSVERSIONINFOEX OsVer;
SYSTEM_INFO SystemInfo;
OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx((OSVERSIONINFO *)&OsVer);
if( OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT )
{
if( OsVer.dwMajorVersion == 5 )
{
switch ( OsVer.dwMinorVersion )
{
case 0:
sprintf(osNameType, "Windows 2000");
break;
case 1:
sprintf(osNameType, "Windows XP");
break;
case 2:
if(2==GetSystemMetrics(/*SM_SERVERR2*/89))
sprintf(osNameType, "Windows 2003 R2");
else
sprintf(osNameType, "Windows 2003");
break;
default:
printf("Other System");
}
}
else if ( OsVer.dwMajorVersion == 6 )
{
switch(OsVer.dwMinorVersion)
{
case 0:
if(OsVer.wProductType != VER_NT_WORKSTATION)
sprintf(osNameType, "Windows Server 2008");
else
sprintf(osNameType, "Windows Vista");
break;
case 1:
if(OsVer.wProductType != VER_NT_WORKSTATION)
sprintf(osNameType, "Windows Server 2008 R2");
else
sprintf(osNameType, "Windows 7");
break;
case 2:
if(OsVer.wProductType != VER_NT_WORKSTATION)
sprintf(osNameType, "Windows Server 2012");
else
sprintf(osNameType, "Windows 8");
break;
case 3:
if(OsVer.wProductType != VER_NT_WORKSTATION)
sprintf(osNameType, "Windows Server 2012 R2");
else
sprintf(osNameType, "Windows 8.1");
break;
default:
sprintf(osNameType, "Other System");
}
}
else if ( OsVer.dwMajorVersion == 10 )
{
switch ( OsVer.dwMinorVersion )
{
case 0:
if(OsVer.wProductType != VER_NT_WORKSTATION)
sprintf(osNameType, "Windows Server 2016");
else
sprintf(osNameType, "Windows 10");
break;
default:
sprintf(osNameType, "Other System");
}
}
else
{
sprintf(osNameType, "Sorry. Unknown System");
}
}
GetComputerName(szComputerName, &nSize);
sprintf(coreVersion, "%d.%d(%d)", OsVer.dwMajorVersion, OsVer.dwMinorVersion, OsVer.dwBuildNumber);
sprintf(ProcessorArchitecture, "%s", GetSystemBits());
#else
#endif
sprintf(info, "%s, %s, v%s, %s, %s(%d.%d)", szComputerName, osNameType, coreVersion, ProcessorArchitecture,
OsVer.szCSDVersion, OsVer.wServicePackMajor, OsVer.wServicePackMinor);
return 0;
}
输出结果为,myname, Windows 7, v6.1(7601), x64, Service Pack 1(1.0)
注意点:
1)调用GetNativeSystemInfo的方法GetSystemBits,不能以SYSTEM_INFO si作为输出参数,否则程序会崩溃
获取Windows操作系统信息计算机名,操作系统型号,版本号,CPU架构,SP号
最新推荐文章于 2023-01-04 09:13:02 发布