一、头文件如下:

#include <Windows.h>
#include <string>
#include <iostream>
#include <winnt.h>
#include <tchar.h>
#include <IPHlpApi.h>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"Advapi32.lib")
#pragma comment(lib, "Iphlpapi.lib")
using namespace std; // ***** global macros ***** //
#define GBYTES 1073741824
#define MBYTES 1048576
#define KBYTES 1024
#define DKBYTES 1024.0 class CSysInfo
{
public:
CSysInfo();
~CSysInfo(); public:
const std::string GetOSVersion();
void SafeGetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo);
const std::string GetCPUInfo();
const std::string GetMemoryStatus(); void GetNetCardAndIPInfo(std::string &adapter_info, std::string &MAC_address, std::string &IP);
const std::string GetComputerUserName();
BOOL GetHardDiskInfo(LPTSTR pModelNo, LPTSTR pSerialNo);
void ToLittleEndian(PUSHORT pWords, int nFirstIndex, int nLastIndex, LPTSTR pBuf); };

二、cpp如下:

#include "SysInfo.h"

CSysInfo::CSysInfo()
{
} CSysInfo::~CSysInfo()
{
} const std::string CSysInfo::GetOSVersion()
{
std::string os_version("");
SYSTEM_INFO system_info;
memset(&system_info, , sizeof(SYSTEM_INFO));
GetSystemInfo(&system_info);
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if (GetVersionEx((OSVERSIONINFO *)&os))
{
switch (os.dwMajorVersion) {
case :
//1996年7月发布
switch (os.dwMinorVersion) {
case :
if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
os_version = "Microsoft Windows NT 4.0 ";
else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
os_version = "Microsoft Windows 95 ";
break;
case :
os_version = "Microsoft Windows 98 ";
break;
case :
os_version = "Microsoft Windows Me ";
break;
}
break;
case :
switch (os.dwMinorVersion) {
//1999年12月发布
case :
os_version = "Microsoft Windows 2000 ";
if (os.wSuiteMask == VER_SUITE_ENTERPRISE)
os_version.append("Advanced Server ");
break;
//2001年8月发布
case :
os_version = "Microsoft Windows XP ";
if (os.wSuiteMask == VER_SUITE_EMBEDDEDNT)
os_version.append("Embedded ");
else if (os.wSuiteMask == VER_SUITE_PERSONAL)
os_version.append("Home Edition ");
else
os_version.append("Professional ");
break;
case :
if (os.wProductType == VER_NT_WORKSTATION &&
system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
os_version = "Microsoft Windows XP Professional x64 Edition ";
if (GetSystemMetrics(SM_SERVERR2) == && os.wSuiteMask == VER_SUITE_BLADE)
os_version = "Microsoft Windows Server 2003 Web Edition ";
else if (GetSystemMetrics(SM_SERVERR2) == && os.wSuiteMask == VER_SUITE_COMPUTE_SERVER)
os_version = ("Microsoft Windows Server 2003 Compute Cluster Edition ");
else if (GetSystemMetrics(SM_SERVERR2) == && os.wSuiteMask == VER_SUITE_STORAGE_SERVER)
os_version = ("Microsoft Windows Server 2003 Storage Server ");
else if (GetSystemMetrics(SM_SERVERR2) == && os.wSuiteMask == VER_SUITE_DATACENTER)
os_version = ("Microsoft Windows Server 2003 Datacenter Edition ");
else if (GetSystemMetrics(SM_SERVERR2) == && os.wSuiteMask == VER_SUITE_ENTERPRISE)
os_version = ("Microsoft Windows Server 2003 Enterprise Edition ");
else if (GetSystemMetrics(SM_SERVERR2) != && os.wSuiteMask == VER_SUITE_STORAGE_SERVER)
os_version = ("Microsoft Windows Server 2003 R2 Storage Server ");
break;
}
break;
case :
switch (os.dwMinorVersion) {
case :
if (os.wProductType == VER_NT_WORKSTATION)
{
os_version = "Microsoft Windows Vista ";
if (os.wSuiteMask == VER_SUITE_PERSONAL)
os_version.append("Home ");
}
else if (os.wProductType != VER_NT_WORKSTATION)
{
os_version = "Microsoft Windows Server 2008 ";
if (os.wSuiteMask == VER_SUITE_DATACENTER)
os_version.append("Datacenter Server ");
else if (os.wSuiteMask == VER_SUITE_ENTERPRISE)
os_version.append("Enterprise ");
}
break;
case :
if (os.wProductType == VER_NT_WORKSTATION)
os_version = "Microsoft Windows 7 ";
else
os_version = "Microsoft Windows Server 2008 R2 ";
break;
}
break;
default:
os_version = "? ";
}
}
SYSTEM_INFO si;
SafeGetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
os_version.append("64bit");
else os_version.append("32bit");
return os_version; } void CSysInfo::SafeGetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo)
{
if (NULL == lpSystemInfo)
return;
typedef VOID(WINAPI *LPFN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);
LPFN_GetNativeSystemInfo nsInfo =
(LPFN_GetNativeSystemInfo)GetProcAddress(GetModuleHandle(_T("kernel32")), "GetNativeSystemInfo");;
if (NULL != nsInfo)
{
nsInfo(lpSystemInfo);
}
else
{
GetSystemInfo(lpSystemInfo);
} } const std::string CSysInfo::GetCPUInfo()
{
std::string processor_name("");
std::string str_path = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
HKEY key;
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)str_path.c_str(), , KEY_ALL_ACCESS, &key) == ERROR_SUCCESS)
{
char processor_value[];
DWORD type = REG_SZ;
DWORD value_size = sizeof(processor_value);
if (::RegQueryValueEx(key, "ProcessorNameString", , &type, (LPBYTE)&processor_value, &value_size) == ERROR_SUCCESS)
processor_name.append(processor_value, value_size);
RegCloseKey(key); }
return processor_name;
} const std::string CSysInfo::GetMemoryStatus()
{
std::string memory_info("");
MEMORYSTATUSEX statusex;
statusex.dwLength = sizeof(statusex);
if (GlobalMemoryStatusEx(&statusex))
{
unsigned long long total = , remain_total = , avl = , remain_avl = ;
double decimal_total = , decimal_avl = ;
remain_total = statusex.ullTotalPhys % GBYTES;
total = statusex.ullTotalPhys / GBYTES;
avl = statusex.ullAvailPhys / GBYTES;
remain_avl = statusex.ullAvailPhys % GBYTES;
if (remain_total > )
decimal_total = (remain_total / MBYTES) / DKBYTES;
if (remain_avl > )
decimal_avl = (remain_avl / MBYTES) / DKBYTES; decimal_total += (double)total;
decimal_avl += (double)avl;
char buffer[MAX_PATH];
//sprintf_s(buffer, MAX_PATH, "%.2f GB (%.2f GB可用)", decimal_total, decimal_avl);
sprintf_s(buffer, MAX_PATH, "%.2f GB", decimal_total);
memory_info.append(buffer);
}
return memory_info;
} void CSysInfo::GetNetCardAndIPInfo(std::string &adapter_info, std::string &MAC_address, std::string &IP)
{
PIP_ADAPTER_INFO pIp_adapter_info = new IP_ADAPTER_INFO();
unsigned long adapter_size = sizeof(IP_ADAPTER_INFO);
int ret = GetAdaptersInfo(pIp_adapter_info, &adapter_size);
if (ERROR_BUFFER_OVERFLOW == ret)
{
delete pIp_adapter_info;
pIp_adapter_info = (PIP_ADAPTER_INFO)new BYTE[adapter_size];
ret = GetAdaptersInfo(pIp_adapter_info, &adapter_size);
}
if (ERROR_SUCCESS == ret)
{
while (pIp_adapter_info)
{
adapter_info.append("name: ");
adapter_info.append(pIp_adapter_info->AdapterName);
adapter_info.append("\ndescription: ");
adapter_info.append(pIp_adapter_info->Description);
adapter_info.append("\ntype: ");
std::string card_type("");
switch (pIp_adapter_info->Type)
{
case MIB_IF_TYPE_OTHER:
card_type = "other";
break;
case MIB_IF_TYPE_ETHERNET:
card_type = "ethernet";
break;
case MIB_IF_TYPE_TOKENRING:
card_type = "tokenring";
break;
case MIB_IF_TYPE_FDDI:
card_type = "fddi";
break;
case MIB_IF_TYPE_PPP:
card_type = "ppp";
break;
case MIB_IF_TYPE_LOOPBACK:
card_type = "loopback";
break;
case MIB_IF_TYPE_SLIP:
card_type = "slip";
break;
default:
break;
}
adapter_info.append(card_type);
MAC_address.append("\nMACAddr: ");
char buffer[MAX_PATH];
for (DWORD i = ; i < pIp_adapter_info->AddressLength; i++)
if (i < pIp_adapter_info->AddressLength - )
{
sprintf_s(buffer, MAX_PATH, "%02X", pIp_adapter_info->Address[i]);
MAC_address.append(buffer);
MAC_address.append("-");
}
else
{
sprintf_s(buffer, MAX_PATH, "%02X", pIp_adapter_info->Address[i]);
MAC_address.append(buffer);
adapter_info.append("\n");
} IP_ADDR_STRING *pIp_addr_string = &(pIp_adapter_info->IpAddressList);
do
{
IP.append("IPAddr:");
IP.append(pIp_addr_string->IpAddress.String);;
IP.append("\nIpMask:");
IP.append(pIp_addr_string->IpMask.String);
IP.append("\nGateway:");
IP.append(pIp_adapter_info->GatewayList.IpAddress.String);
IP.append("\n");
pIp_addr_string = pIp_addr_string->Next;
} while (pIp_addr_string);
adapter_info.append("\n");
pIp_adapter_info = pIp_adapter_info->Next;
} }
if (pIp_adapter_info)
{
delete pIp_adapter_info;
pIp_adapter_info = nullptr;
}
} const std::string CSysInfo::GetComputerUserName()
{ char szUsername[BUFSIZ];
DWORD bufCharCount = BUFSIZ; // Get and display the name of the computer.
if (GetComputerName(szUsername, &bufCharCount)) {
return szUsername; }else {
return "";
} } BOOL CSysInfo::GetHardDiskInfo(LPTSTR pModelNo, LPTSTR pSerialNo)
{
//-1是因为 SENDCMDOUTPARAMS 的结尾是 BYTE bBuffer[1];
BYTE IdentifyResult[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - ];
DWORD dwBytesReturned;
GETVERSIONINPARAMS get_version;
SENDCMDINPARAMS send_cmd = { }; HANDLE hFile = CreateFile(_T("\\\\.\\PHYSICALDRIVE0"), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, , NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE; //get version
DeviceIoControl(hFile, SMART_GET_VERSION, NULL, ,
&get_version, sizeof(get_version), &dwBytesReturned, NULL); //identify device
send_cmd.irDriveRegs.bCommandReg = (get_version.bIDEDeviceMap & 0x10) ? ATAPI_ID_CMD : ID_CMD;
DeviceIoControl(hFile, SMART_RCV_DRIVE_DATA, &send_cmd, sizeof(SENDCMDINPARAMS) - ,
IdentifyResult, sizeof(IdentifyResult), &dwBytesReturned, NULL);
CloseHandle(hFile); //adjust the byte order
PUSHORT pWords = (USHORT*)(((SENDCMDOUTPARAMS*)IdentifyResult)->bBuffer); ToLittleEndian(pWords, , , pModelNo);
ToLittleEndian(pWords, , , pSerialNo); return true;
} //把WORD数组调整字节序为little-endian,并滤除字符串结尾的空格。
void CSysInfo::ToLittleEndian(PUSHORT pWords, int nFirstIndex, int nLastIndex, LPTSTR pBuf)
{
int index;
LPTSTR pDest = pBuf;
for (index = nFirstIndex; index <= nLastIndex; ++index)
{
pDest[] = pWords[index] >> ;
pDest[] = pWords[index] & 0xFF;
pDest += ;
}
*pDest = ; //trim space at the endof string; 0x20: _T(' ')
--pDest;
while (*pDest == 0x20)
{
*pDest = ;
--pDest;
}
}

C/C++应用--Window下获取硬件信息(CPU, 硬盘,网卡等)的更多相关文章

  1. 使用dmidecode在Linux下获取硬件信息

    dmidecode命令可以让你在Linux系统下获取有关硬件方面的信息.dmidecode的作用是将DMI数据库中的信息解码,以可读的文本方式显示.由于DMI信息可以人为修改,因此里面的信息不一定是系 ...

  2. linux下dmidecode命令获取硬件信息

    linux下dmidecode命令获取硬件信息 2 A+ 所属分类:Linux 运维工具 dmidecode在 Linux 系统下获取有关硬件方面的信息.dmidecode 遵循 SMBIOS/DMI ...

  3. Linux 系统下查看硬件信息命令大全

    有许多命令可以用来查看 Linux 系统上的硬件信息.有些命令只能够打印出像 CPU 和内存这一特定的硬件组件信息,另外一些命令可以查看多种硬件组件的信息. 这个教程可以带大家快速了解一下查看各种硬件 ...

  4. WMI 获取硬件信息的封装函数与获取联想台式机的出厂编号方法

    原文:WMI 获取硬件信息的封装函数与获取联想台式机的出厂编号方法 今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都是可以提取出来的,就自己把那些公共部分提出出来,以后如果要获取 某部分的 ...

  5. 用C++和shell获取本机CPU、网卡IO、内存、磁盘等的基本信息

    用C++和shell获取本机CPU.网卡.内存.磁盘等的基本信息: 由于对C++相关的函数没多少了解,但是觉得用shell反而相对简单一些: 一.shell脚本,用来辅助C++获取主机的资源使用信息 ...

  6. Web网站中利用JavaScript中ActiveXObject对象获取硬件信息(显示器数量、分辨率)从而进行单双屏跳转

    前言:最近这两天工作上,要实现一个功能,在好友阿聪的帮助下,算是比较好的解决了这个需求. B/S的Web网站,需要实现点击按钮时,根据客户端连接的显示屏(监视器)数量进行,单双屏跳转显示新页面. 由于 ...

  7. Linux系统下查看硬件信息命令大全

    导读 有许多命令可以用来查看 Linux 系统上的硬件信息.有些命令只能够打印出像 CPU 和内存这一特定的硬件组件信息,另外一些命令可以查看多种硬件组件的信息. 这个教程可以带大家快速了解一下查看各 ...

  8. C#获取硬件信息

    //硬件信息 public class GF_Hardware { /// <summary> /// cpu序列号 /// </summary> /// <return ...

  9. C++ WMI获取系统硬件信息(CPU/DISK/NetWork etc)

    官网找到一个例子,根据例子修改下可以获取很多信息 #define _WIN32_DCOM #include <iostream> using namespace std; #include ...

随机推荐

  1. JVM OOM异常会导致JVM退出吗?

    出处:  https://mp.weixin.qq.com/s/8j8YTcr2qhVActLGzOqe7Q  https://blog.csdn.net/h2604396739/article/de ...

  2. shell习题第25题:判断是否开启web服务

    [题目要求] 写一个脚本判断我的linux服务器是否开启web服务?监听80端口 [核心要点] netstat -lntp | grep '80' [脚本] #!/bin/bash n=`netsta ...

  3. 数据库及MYSQL基础(3)-JDBC

    教学视频链接:https://edu.aliyun.com/course/1694?spm=5176.11400004.0.0.29254768sg2H5P 程序文件链接:https://pan.ba ...

  4. 定时任务FluentScheduler

    1.Nuget 安装包 2.创建3个不同的任务 public class MyJob : IJob { void IJob.Execute() { Trace.WriteLine("现在时间 ...

  5. mybatis查询foreach使用

    1.mybatis传入map参数,map中包含list: List<FukaModel> fukaModels = price.getSchemaPrice().getFukaList() ...

  6. Win10安装PyQt5与Qt Designer

    1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 会自动下载PyQt5以及sip并安装,因为PyQt5不再提供Qt Designer等工具,所以需要再安装pyqt5- ...

  7. javascript--HTML DOM常用元素对象

    二,Select:访问select元素 属性:.selectedIndex  获取select中当前选中项的下标 .options  获取select中所有的option元素 返回值为数组 .opti ...

  8. 使用的jQuery加载源的优势【问题】

    [问题]使用的jQuery加载源的优势? [答案]许多用户在访问其他站点时,已经从谷歌或微软加载过 jQuery.所有结果是,当他们访问您的站点时,会从缓存中加载 jQuery,这样可以减少加载时间. ...

  9. 与app交互因异步造成的坑记录

    一.问题产生背景: 在app内跳转到H5页面,初始页面获取用户等各种信息,前端除了可以获取链接上的参数去请求接口,接着进行数据的缓存等,也可以去获取app写入window的数据,然后进行其他的操作.公 ...

  10. java序列化和反序列化使用总结

    一.概念 java对象序列化的意思就是将对象的状态转化成字节流,以后可以通过这些值再生成相同状态的对象.对象序列化是对象持久化的一种实现方法,它是将对象的属性和方法转化为一种序列化的形式用于存储和传输 ...