- 1 //"[平台:2, 系统:10.0.19044(处理器(586):12), CPU: 12th Gen Intel(R) Core(TM) i5-12400 (2.50GHz), CSD():0.0, 套件掩码:256, 其他信息:1, 内存:15.7GB,虚拟内存:4.0GB]"
- 2 //系统版本对照表:https://learn.microsoft.com/zh-cn/windows/win32/sysinfo/operating-system-version 或者 https://learn.microsoft.com/zh-cn/windows/win32/api/winnt/ns-winnt-osversioninfoexa
- 3 CString GetOSVersion()
- 4 {
- 5 //获取cpu名称和主频
- 6 CHAR chCPUName[50] = { 0 };//cpu名称
- 7 DWORD dwSize = 50;
- 8 DWORD dwValue; //主频值
- 9 CString strPath = _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");//注册表子键路径
- 10 CRegKey regkey;//定义注册表类对象
- 11 LONG lResult;//LONG型变量-反应结果
- 12 lResult = regkey.Open(HKEY_LOCAL_MACHINE, LPCTSTR(strPath), KEY_ALL_ACCESS); //打开注册表键
- 13 if (lResult == ERROR_SUCCESS)
- 14 {
- 15 //获取ProcessorNameString字段值
- 16 if (ERROR_SUCCESS != regkey.QueryStringValue(_T("ProcessorNameString"), chCPUName, &dwSize))
- 17 {
- 18 ZeroMemory(chCPUName, sizeof(chCPUName));
- 19 }
- 20
- 21 //查询CPU主频
- 22 if (ERROR_SUCCESS != regkey.QueryDWORDValue(_T("~MHz"), dwValue))
- 23 {
- 24 dwValue = 0;
- 25 }
- 26 }
- 27
- 28
- 29 //获取内存信息
- 30 MEMORYSTATUSEX statex;
- 31 statex.dwLength = sizeof(statex);
- 32 GlobalMemoryStatusEx(&statex);
- 33 DWORDLONG physical_memory = statex.ullTotalPhys / (1024 * 1024);
- 34 DWORDLONG virtual_totalmemory = statex.ullTotalVirtual / (1024 * 1024);
- 35
- 36
- 37
- 38 //获取系统信息
- 39 CString strOSInfo;
- 40 OSVERSIONINFOEX osvi;
- 41 SYSTEM_INFO si;
- 42 BOOL bOSVersionInfoEx;
- 43 ZeroMemory(&si, sizeof(SYSTEM_INFO));
- 44 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
- 45 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- 46 if (!(bOSVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi)))
- 47 {
- 48 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- 49 GetVersionEx((OSVERSIONINFO*)&osvi);
- 50 }
- 51
- 52 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
- 53 GetSystemInfo(&si);
- 54
- 55 strOSInfo.Format("[平台:%d, 系统:%d.%d.%d(处理器(%d):%d), CPU: %s (%.2fGHz), CSD(%s):%d.%d, 套件掩码:%d, 其他信息:%d, 内存:%.1fGB,虚拟内存:%.1fGB]", osvi.dwPlatformId, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
- 56 si.dwProcessorType, si.dwNumberOfProcessors,
- 57 chCPUName, dwValue / 1000.0,
- 58 osvi.szCSDVersion, osvi.wServicePackMajor, osvi.wServicePackMinor, osvi.wSuiteMask, osvi.wProductType,
- 59 physical_memory / 1024.0, virtual_totalmemory / 1024.0);
- 60
- 61 //GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead
- 62 //win8.1之后系统,获取结果都是 6.2
- 63 if (osvi.dwMajorVersion >= 6)
- 64 {
- 65 //定义变量
- 66 typedef LONG(__stdcall* fnRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);
- 67 fnRtlGetVersion pRtlGetVersion;
- 68 HMODULE hNtdll;
- 69 LONG ntStatus = 1;
- 70 ULONG dwMajorVersion = 0;
- 71 ULONG dwMinorVersion = 0;
- 72 ULONG dwBuildNumber = 0;
- 73 RTL_OSVERSIONINFOW VersionInformation = { 0 };
- 74 DWORD OsVersion;
- 75
- 76 do
- 77 {
- 78 hNtdll = GetModuleHandle("ntdll.dll");
- 79 if (hNtdll == NULL)break;
- 80
- 81 pRtlGetVersion = (fnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
- 82 if (pRtlGetVersion == NULL)break;
- 83
- 84 VersionInformation.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOW);
- 85 ntStatus = pRtlGetVersion(&VersionInformation);
- 86
- 87 if (ntStatus != 0) break;
- 88
- 89 dwMajorVersion = VersionInformation.dwMajorVersion;
- 90 dwMinorVersion = VersionInformation.dwMinorVersion;
- 91 dwBuildNumber = VersionInformation.dwBuildNumber;
- 92
- 93 } while (FALSE);
- 94
- 95 if (ntStatus == 0)
- 96 {
- 97 strOSInfo.Format("[平台:%d, 系统:%d.%d.%d(处理器(%d):%d), CPU: %s (%.2fGHz), CSD(%s):%d.%d, 套件掩码:%d, 其他信息:%d, 内存:%.1fGB,虚拟内存:%.1fGB]", osvi.dwPlatformId, dwMajorVersion, dwMinorVersion, dwBuildNumber,
- 98 si.dwProcessorType, si.dwNumberOfProcessors,
- 99 chCPUName, dwValue/1000.0,
- 100 osvi.szCSDVersion, osvi.wServicePackMajor, osvi.wServicePackMinor, osvi.wSuiteMask, osvi.wProductType,
- 101 physical_memory / 1024.0, virtual_totalmemory / 1024.0);
- 102 }
- 103 }
- 104 return strOSInfo;
- 105 }
复制代码
  - 1 //"[平台:2, 系统:10.0.19044(处理器(586):12), CPU: 12th Gen Intel(R) Core(TM) i5-12400 (2.50GHz), CSD():0.0, 套件掩码:256, 其他信息:1, 内存:15.7GB,虚拟内存:4.0GB]"
- 2 //系统版本对照表:https://learn.microsoft.com/zh-cn/windows/win32/sysinfo/operating-system-version 或者 https://learn.microsoft.com/zh-cn/windows/win32/api/winnt/ns-winnt-osversioninfoexa
- 3 //By SamRichard https://home.cnblogs.com/u/SamRichard/
- 4 CString GetOSVersion()
- 5 {
- 6 //获取cpu名称和主频
- 7 CHAR chCPUName[50] = { 0 };//cpu名称
- 8 DWORD dwSize = 50;
- 9 DWORD dwValue; //主频值
- 10 CString strPath = _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");//注册表子键路径
- 11 CRegKey regkey;//定义注册表类对象
- 12 LONG lResult;//LONG型变量-反应结果
- 13 lResult = regkey.Open(HKEY_LOCAL_MACHINE, LPCTSTR(strPath), KEY_ALL_ACCESS); //打开注册表键
- 14 if (lResult == ERROR_SUCCESS)
- 15 {
- 16 //获取ProcessorNameString字段值
- 17 if (ERROR_SUCCESS != regkey.QueryStringValue(_T("ProcessorNameString"), chCPUName, &dwSize))
- 18 {
- 19 ZeroMemory(chCPUName, sizeof(chCPUName));
- 20 }
- 21
- 22 //查询CPU主频
- 23 if (ERROR_SUCCESS != regkey.QueryDWORDValue(_T("~MHz"), dwValue))
- 24 {
- 25 dwValue = 0;
- 26 }
- 27 }
- 28
- 29
- 30 //获取内存信息
- 31 MEMORYSTATUSEX statex;
- 32 statex.dwLength = sizeof(statex);
- 33 GlobalMemoryStatusEx(&statex);
- 34 DWORDLONG physical_memory = statex.ullTotalPhys / (1024 * 1024);
- 35 DWORDLONG virtual_totalmemory = statex.ullTotalVirtual / (1024 * 1024);
- 36
- 37
- 38
- 39 //获取系统信息
- 40 CString strOSInfo;
- 41 OSVERSIONINFOEX osvi;
- 42 SYSTEM_INFO si;
- 43 BOOL bOSVersionInfoEx;
- 44 ZeroMemory(&si, sizeof(SYSTEM_INFO));
- 45 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
- 46 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- 47 if (!(bOSVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi)))
- 48 {
- 49 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- 50 GetVersionEx((OSVERSIONINFO*)&osvi);
- 51 }
- 52
- 53 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
- 54 GetSystemInfo(&si);
- 55
- 56 strOSInfo.Format("[平台:%d, 系统:%d.%d.%d(处理器(%d):%d), CPU: %s (%.2fGHz), CSD(%s):%d.%d, 套件掩码:%d, 其他信息:%d, 内存:%.1fGB,虚拟内存:%.1fGB]", osvi.dwPlatformId, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
- 57 si.dwProcessorType, si.dwNumberOfProcessors,
- 58 chCPUName, dwValue / 1000.0,
- 59 osvi.szCSDVersion, osvi.wServicePackMajor, osvi.wServicePackMinor, osvi.wSuiteMask, osvi.wProductType,
- 60 physical_memory / 1024.0, virtual_totalmemory / 1024.0);
- 61
- 62 //GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead
- 63 //win8.1之后系统,获取结果都是 6.2
- 64 if (osvi.dwMajorVersion >= 6)
- 65 {
- 66 //定义变量
- 67 typedef LONG(__stdcall* fnRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);
- 68 fnRtlGetVersion pRtlGetVersion;
- 69 HMODULE hNtdll;
- 70 LONG ntStatus = 1;
- 71 ULONG dwMajorVersion = 0;
- 72 ULONG dwMinorVersion = 0;
- 73 ULONG dwBuildNumber = 0;
- 74 RTL_OSVERSIONINFOW VersionInformation = { 0 };
- 75 DWORD OsVersion;
- 76
- 77 do
- 78 {
- 79 hNtdll = GetModuleHandle("ntdll.dll");
- 80 if (hNtdll == NULL)break;
- 81
- 82 pRtlGetVersion = (fnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
- 83 if (pRtlGetVersion == NULL)break;
- 84
- 85 VersionInformation.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOW);
- 86 ntStatus = pRtlGetVersion(&VersionInformation);
- 87
- 88 if (ntStatus != 0) break;
- 89
- 90 dwMajorVersion = VersionInformation.dwMajorVersion;
- 91 dwMinorVersion = VersionInformation.dwMinorVersion;
- 92 dwBuildNumber = VersionInformation.dwBuildNumber;
- 93
- 94 } while (FALSE);
- 95
- 96 if (ntStatus == 0)
- 97 {
- 98 strOSInfo.Format("[平台:%d, 系统:%d.%d.%d(处理器(%d):%d), CPU: %s (%.2fGHz), CSD(%s):%d.%d, 套件掩码:%d, 其他信息:%d, 内存:%.1fGB,虚拟内存:%.1fGB]", osvi.dwPlatformId, dwMajorVersion, dwMinorVersion, dwBuildNumber,
- 99 si.dwProcessorType, si.dwNumberOfProcessors,
- 100 chCPUName, dwValue/1000.0,
- 101 osvi.szCSDVersion, osvi.wServicePackMajor, osvi.wServicePackMinor, osvi.wSuiteMask, osvi.wProductType,
- 102 physical_memory / 1024.0, virtual_totalmemory / 1024.0);
- 103 }
- 104 }
- 105 return strOSInfo;
- 106 }
复制代码 View Code
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |