#include <iostream>
#include <Windows.h>
#include <iphlpapi.h>
#include <sstream>
#pragma comment(lib, "iphlpapi.lib")
using namespace std;
// 获取主板信息
void getBIOSInfo(BIOS_Message& bios_msg) {
char* pBuffer = NULL;
DWORD dwBufSize = 0;
// 获取缓冲区大小
GetSystemFirmwareTable('RSMB', 0, NULL, dwBufSize);
if (dwBufSize) {
pBuffer = new char[dwBufSize];
if (pBuffer) {
// 获取主板信息
GetSystemFirmwareTable('RSMB', 0, pBuffer, dwBufSize);
SMBIOS_HEADER* pSMBIOSHeader = (SMBIOS_HEADER*)pBuffer;
while (pSMBIOSHeader->Type != 0x00) {
if (pSMBIOSHeader->Type == 0x02) {
SMBIOS_TYPE2* pSMBIOS = (SMBIOS_TYPE2*)pSMBIOSHeader;
bios_msg.Motherboard_Manufacturer_Name = (char*)pSMBIOS + pSMBIOS->Manufacturer;
bios_msg.Motherboard_Model = (char*)pSMBIOS + pSMBIOS->Product;
bios_msg.Motherboard_Version = (char*)pSMBIOS + pSMBIOS->Version;
bios_msg.Serial_Number = (char*)pSMBIOS + pSMBIOS->SerialNumber;
break;
}
pSMBIOSHeader = (SMBIOS_HEADER*)((char*)pSMBIOSHeader + pSMBIOSHeader->Length);
}
}
delete[] pBuffer;
}
// 获取系统信息
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
bios_msg.System_Manufacturer_Name = "Microsoft Corporation";
bios_msg.System_Product_Name = "Virtual Machine";
bios_msg.System_Version = "None";
bios_msg.System_Serial_Number = "None";
// 获取电脑型号
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\SystemInformation", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char szData[256] = { 0 };
DWORD dwType = REG_SZ;
DWORD dwSize = sizeof(szData);
if (RegQueryValueEx(hKey, "SystemProductName", NULL, &dwType, (LPBYTE)szData, &dwSize) == ERROR_SUCCESS) {
bios_msg.System_Product_Name = szData;
}
RegCloseKey(hKey);
}
// 获取电脑序列号
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\SystemInformation", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char szData[256] = { 0 };
DWORD dwType = REG_SZ;
DWORD dwSize = sizeof(szData);
if (RegQueryValueEx(hKey, "SystemSKU", NULL, &dwType, (LPBYTE)szData, &dwSize) == ERROR_SUCCESS) {
bios_msg.System_Serial_Number = szData;
}
RegCloseKey(hKey);
}
// 获取计算机型号
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\SystemInformation", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char szData[256] = { 0 };
DWORD dwType = REG_SZ;
DWORD dwSize = sizeof(szData);
if (RegQueryValueEx(hKey, "SystemFamily", NULL, &dwType, (LPBYTE)szData, &dwSize) == ERROR_SUCCESS) {
bios_msg.Classis_Model = szData;
}
RegCloseKey(hKey);
}
}
// 获取CPU信息
void getCPUInfo(CPU_Message& cpu_msg) {
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
cpu_msg.CPU_brand = "Unknown";
cpu_msg.CPU_name = "Unknown";
cpu_msg.CPU_cores = sys_info.dwNumberOfProcessors;
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char szData[256] = { 0 };
DWORD dwType = REG_SZ;
DWORD dwSize = sizeof(szData);
if (RegQueryValueEx(hKey, "ProcessorNameString", NULL, &dwType, (LPBYTE)szData, &dwSize) == ERROR_SUCCESS) {
cpu_msg.CPU_name = szData;
}
if (RegQueryValueEx(hKey, "VendorIdentifier", NULL, &dwType, (LPBYTE)szData, &dwSize) == ERROR_SUCCESS) {
cpu_msg.CPU_brand = szData;
}
RegCloseKey(hKey);
}
}
// 获取硬盘信息
void getHDInfo(HD_Message& hd_msg) {
hd_msg.HD_brand = "Unknown";
hd_msg.HD_name = "Unknown";
hd_msg.HD_serial_number = "Unknown";
hd_msg.HD_firmware_version = "Unknown";
hd_msg.HD_capacity = 0;
// 获取硬盘信息
HANDLE hDevice = CreateFileA("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if (hDevice != INVALID_HANDLE_VALUE) {
STORAGE_PROPERTY_QUERY query;
char buffer[1024] = { 0 };
memset(&query, 0, sizeof(query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), &buffer, sizeof(buffer), NULL, NULL)) {
STORAGE_DEVICE_DESCRIPTOR* pDevDesc = (STORAGE_DEVICE_DESCRIPTOR*)&buffer;
char* pVendorIdOffset = buffer + pDevDesc->VendorIdOffset;
char* pProductIdOffset = buffer + pDevDesc->ProductIdOffset;
char* pProductRevisionOffset = buffer + pDevDesc->ProductRevisionOffset;
hd_msg.HD_brand = pVendorIdOffset;
hd_msg.HD_name = pProductIdOffset;
hd_msg.HD_firmware_version = pProductRevisionOffset;
}
memset(&query, 0, sizeof(query));
query.PropertyId = StorageDeviceIdProperty;
query.QueryType = PropertyStandardQuery;
if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), &buffer, sizeof(buffer), NULL, NULL)) {
STORAGE_DEVICE_ID_DESCRIPTOR* pDevIdDesc = (STORAGE_DEVICE_ID_DESCRIPTOR*)&buffer;
char* pSerialNumberOffset = buffer + pDevIdDesc->SerialNumberOffset;
hd_msg.HD_serial_number = pSerialNumberOffset;
}
GET_LENGTH_INFORMATION LengthInfo;
if (DeviceIoControl(hDevice, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &LengthInfo, sizeof(GET_LENGTH_INFORMATION), NULL, NULL)) {
hd_msg.HD_capacity = LengthInfo.Length.QuadPart / 1024 / 1024 / 1024;
}
CloseHandle(hDevice);
}
}
// 获取内存信息
void getMemoryInfo(Memory_Message& mem_msg) {
mem_msg.Memory_brand = "Unknown";
mem_msg.Memory_name = "Unknown";
mem_msg.Memory_capacity = 0;
// 获取内存信息
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
mem_msg.Memory_capacity = statex.ullTotalPhys / 1024 / 1024 / 1024;
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char szData[256] = { 0 };
DWORD dwType = REG_SZ;
DWORD dwSize = sizeof(szData);
if (RegQueryValueEx(hKey, "VendorIdentifier", NULL, &dwType, (LPBYTE)szData, &dwSize) == ERROR_SUCCESS) {
mem_msg.Memory_brand = szData;
}
RegCloseKey(hKey);
}
}
// 获取MAC地址信息
void getMACInfo(MAC_Message& mac_msg) {
mac_msg.MAC_address = "Unknown";
mac_msg.MAC_port = 0;
mac_msg.MAC_VLANID = 0;
mac_msg.MAC_aging_time = 0;
// 获取MAC地址信息
IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);
DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
if (dwStatus == ERROR_SUCCESS) {
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
while (pAdapterInfo) {
if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET && pAdapterInfo->AddressLength == 6) {
stringstream ss;
for (int i = 0; i < pAdapterInfo->AddressLength; i++) {
ss << hex << (int)pAdapterInfo->Address[ i];
if (i < pAdapterInfo->AddressLength - 1) {
ss << ":";
}
}
mac_msg.MAC_address = ss.str();
mac_msg.MAC_port = pAdapterInfo->Index;
mac_msg.MAC_VLANID = pAdapterInfo->VlanId;
mac_msg.MAC_aging_time = pAdapterInfo->LeaseExpires;
break;
}
pAdapterInfo = pAdapterInfo->Next;
}
}
}
// 获取IP地址信息
void getIPInfo(IP_Message& ip_msg) {
ip_msg.IP_type = "Unknown";
ip_msg.IP_address = "Unknown";
// 获取IP地址信息
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
ULONG ulFlags = GAA_FLAG_INCLUDE_PREFIX;
ULONG ulAddrTableSize = 0;
GetAdaptersAddresses(AF_UNSPEC, ulFlags, NULL, pAddresses, &ulAddrTableSize);
pAddresses = (PIP_ADAPTER_ADDRESSES)malloc(ulAddrTableSize);
GetAdaptersAddresses(AF_UNSPEC, ulFlags, NULL, pAddresses, &ulAddrTableSize);
PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses;
while (pCurrAddresses) {
if (pCurrAddresses->OperStatus == IfOperStatusUp) {
if (pCurrAddresses->IfType == IF_TYPE_ETHERNET_CSMACD || pCurrAddresses->IfType == IF_TYPE_IEEE80211) {
if (pCurrAddresses->FirstUnicastAddress) {
sockaddr* pAddr = pCurrAddresses->FirstUnicastAddress->Address.lpSockaddr;
if (pAddr->sa_family == AF_INET) {
ip_msg.IP_type = "ipv4";
char szIP[256] = { 0 };
DWORD dwSize = sizeof(szIP);
WSAAddressToStringA(pAddr, pCurrAddresses->FirstUnicastAddress->Address.iSockaddrLength, NULL, szIP, &dwSize);
ip_msg.IP_address = szIP;
}
else if (pAddr->sa_family == AF_INET6) {
ip_msg.IP_type = "ipv6";
char szIP[256] = { 0 };
DWORD dwSize = sizeof(szIP);
WSAAddressToStringA(pAddr, pCurrAddresses->FirstUnicastAddress->Address.iSockaddrLength, NULL, szIP, &dwSize);
ip_msg.IP_address = szIP;
}
}
break;
}
}
pCurrAddresses = pCurrAddresses->Next;
}
free(pAddresses);
}
// 获取电脑配置信息
void getPCInfo(PC_Message& pc_msg) {
getBIOSInfo(pc_msg.BIOS_msg);
getCPUInfo(pc_msg.CPU_msg);
getHDInfo(pc_msg.HD_msg);
getMemoryInfo(pc_msg.Mem_msg);
getMACInfo(pc_msg.MAC_msg);
getIPInfo(pc_msg.IP_msg);
}
int main() {
PC_Message pc_msg;
getPCInfo(pc_msg);
cout << "BIOS信息:" << endl;
cout << "主板制造商:" << pc_msg.BIOS_msg.Motherboard_Manufacturer_Name << endl;
cout << "主板型号:" << pc_msg.BIOS_msg.Motherboard_Model << endl;
cout << "主板版本号:" << pc_msg.BIOS_msg.Motherboard_Version << endl;
cout << "主板序列号:" << pc_msg.BIOS_msg.Serial_Number << endl;
cout << "系统制造商:" << pc_msg.BIOS_msg.System_Manufacturer_Name << endl;
cout << "系统产品名称:" << pc_msg.BIOS_msg.System_Product_Name << endl;
cout << "系统版本号:" << pc_msg.BIOS_msg.System_Version << endl;
cout << "系统序列号:" << pc_msg.BIOS_msg.System_Serial_Number << endl;
cout << "计算机制造商:" << pc_msg.BIOS_msg.Classis_Manufacturer_Name << endl;
cout << "计算机型号:" << pc_msg.BIOS_msg.Classis_Model << endl;
cout << "计算机版本号:" << pc_msg.BIOS_msg.Classis_Version << endl;
cout << "CPU信息:" << endl;
cout << "CPU品牌:" << pc_msg.CPU_msg.CPU_brand << endl;
cout << "CPU名称:" << pc_msg.CPU_msg.CPU_name << endl;
cout << "CPU核心数:" << pc_msg.CPU_msg.CPU_cores << endl;
cout << "硬盘信息:" << endl;
cout << "硬盘品牌:" << pc_msg.HD_msg.HD_brand << endl;
cout << "硬盘名称:" << pc_msg.HD_msg.HD_name << endl;
cout << "硬盘序列号:" << pc_msg.HD_msg.HD_serial_number << endl;
cout << "硬盘固件版本:" << pc_msg.HD_msg.HD_firmware_version << endl;
cout << "硬盘容量:" << pc_msg.HD_msg.HD_capacity << "GB" << endl;
cout << "内存信息:" << endl;
cout << "内存品牌:" << pc_msg.Mem_msg.Memory_brand << endl;
cout << "内存名称:" << pc_msg.Mem_msg.Memory_name << endl;
cout << "内存容量:" << pc_msg.Mem_msg.Memory_capacity << "GB" << endl;
cout << "MAC地址信息:" << endl;
cout << "MAC地址:" << pc_msg.MAC_msg.MAC_address << endl;
cout << "MAC端口号:" << pc_msg.MAC_msg.MAC_port << endl;
cout << "MAC VLAN ID:" << pc_msg.MAC_msg.MAC_VLANID << endl;
cout << "MAC老化时间:" << pc_msg.MAC_msg.MAC_aging_time << endl;
cout << "IP地址信息:" << endl;
cout << "IP种类:" << pc_msg.IP_msg.IP_type << endl;
cout << "IP地址:" << pc_msg.IP_msg.IP_address << endl;
return 0;
}