C++安全的取得真实系统信息

77次阅读
没有评论
#include "tchar.h"

#if defined(WIN32) || defined(WIN64)
typedef void(__stdcall *FUN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);
// 安全的取得真实系统信息
TCHAR* GetSystemBits()
{
SYSTEM_INFO si;
FUN_GetNativeSystemInfo pfn = (FUN_GetNativeSystemInfo)GetProcAddress(GetModuleHandle(_T("kernel32")), "GetNativeSystemInfo");
if (pfn)
{
pfn(&si);
switch (si.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64:
return _T("x64");
case PROCESSOR_ARCHITECTURE_IA64:
return _T("IA64");
}
}
return _T("x86");
}
#endif




int GetOSInfo(TCHAR *info)
{
TCHAR szComputerName[MAXBYTE] = { 0 };
TCHAR osNameType[MAXBYTE] = { 0 };
TCHAR osVersion[MAXBYTE] = { 0 };
TCHAR coreVersion[MAXBYTE] = { 0 };
TCHAR 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:
_stprintf(osNameType, _T("Windows 2000"));
break;
case 1:
_stprintf(osNameType, _T("Windows XP"));
break;
case 2:
if (2 == GetSystemMetrics(/*SM_SERVERR2*/89))
_stprintf(osNameType, _T("Windows 2003 R2"));
else
_stprintf(osNameType, _T("Windows 2003"));
break;
default:
printf("Other System");
}
}
else if (OsVer.dwMajorVersion == 6)
{
switch (OsVer.dwMinorVersion)
{
case 0:
if (OsVer.wProductType != VER_NT_WORKSTATION)
_stprintf(osNameType, _T("Windows Server 2008"));
else
_stprintf(osNameType, _T("Windows Vista"));
break;
case 1:
if (OsVer.wProductType != VER_NT_WORKSTATION)
_stprintf(osNameType, _T("Windows Server 2008 R2"));
else
_stprintf(osNameType, _T("Windows 7"));
break;
case 2:
if (OsVer.wProductType != VER_NT_WORKSTATION)
_stprintf(osNameType, _T("Windows Server 2012"));
else
_stprintf(osNameType, _T("Windows 8"));
break;
case 3:
if (OsVer.wProductType != VER_NT_WORKSTATION)
_stprintf(osNameType, _T("Windows Server 2012 R2"));
else
_stprintf(osNameType, _T("Windows 8.1"));
break;
default:
_stprintf(osNameType, _T("Other System"));
}
}
else if (OsVer.dwMajorVersion == 10)
{
switch (OsVer.dwMinorVersion)
{
case 0:
if (OsVer.wProductType != VER_NT_WORKSTATION)
_stprintf(osNameType, _T("Windows Server 2016"));
else
_stprintf(osNameType, _T("Windows 10"));
break;
default:
_stprintf(osNameType, _T("Other System"));
}
}
else
{
_stprintf(osNameType, _T("Sorry. Unknown System"));
}
}
GetComputerName(szComputerName, &nSize);
_stprintf(coreVersion, _T("%d.%d(%d)"), OsVer.dwMajorVersion, OsVer.dwMinorVersion, OsVer.dwBuildNumber);
_stprintf(ProcessorArchitecture, _T("%s"), GetSystemBits());
#else
#endif

_stprintf(info, _T("%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作为输出参数,否则程序会崩溃
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR Info[256] = { 0 };
GetOSInfo(Info);

_tprintf(Info);

system("pause");
return 0;
}
yiywain
版权声明:本站原创文章,由 yiywain2022-03-14发表,共计2846字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码