C++ - 整合LibreHardwareMonitor库实现硬件监控教程(温度、CPU、内存、网络、硬盘、风扇等)
作者:hangge | 2024-12-23 08:53
最近需要使用 C++ 编写一个电脑硬件监控软件,能够实时获取计算机的 CPU 利用率、内存利用率、显卡利用率、CPU 温度、显卡温度、硬盘温度、主板温度、硬盘利用率、CPU 频率、风扇转速、总网速、实时上传速率、实时下载速率等一系列监控指标。要实现该功能,我们可以借助 LibreHardwareMonitor 这个第三方库,下面通过样例进行演示。
1,下载 LibreHardwareMonitor
(2)将下载下来的压缩包解压后,里面的 LibreHardwareMonitorLib.dll 就是我们后面需要用到的 dll 文件。
(3)其实压缩包里有一个官方提供的监控工具软件,我们运行 LibreHardwareMonitor.exe 可以看到其能够获取到的各种监测数据。
2,创建项目
(1)使用“以管理员身份运行”方式打开 Visual Studio。如果没有管理员权限,程序运行调试时温度监控、显卡利用率、硬盘利用率、风扇转速等硬件监控信息会获取不到。
(2)为方便演示,我这里创建一个“控制台应用”
(3)项目创建完毕后,把前面我们下载下来的“LibreHardwareMonitorLib.dll”放到刚创建的项目文件夹中。
(4)然后在项目上右键点击选择“属性”,接着在弹出的属性页中将配置切换成“所有配置”,点击“高级”,将公共语言运行时支持改成“.NET Framework 运行时支持(/clr)”
(5)然后切换到“生成后事件”,在命令行一栏填写如下内容,然后应用。这个配置是为了让 LibreHardwareMonitorLib.dll 文件能自动复制到生成的 .exe 所在的文件夹里,避免手动复制。
Copy /Y "$(SolutionDir)*.dll" "$(TargetDir)"
3,编写代码
我们将项目默认生成的 cpp 文件内容修改成如下代码。该代码通过调用 LibreHardwareMonitorLib.dll 实现对计算机硬件信息(如 CPU、GPU、内存等)的实时监控和递归遍历输出,每隔 5 秒更新一次。
#include <iostream> #include <thread> #include <chrono> #include <msclr\marshal_cppstd.h> #using "LibreHardwareMonitorLib.dll" // 使用的命名空间 using namespace System; using namespace LibreHardwareMonitor::Hardware; // 函数:GetHardwareInfo // 描述:递归地获取硬件信息并打印到控制台 // 参数:IHardware^ hardware - 当前硬件对象 // 返回值:int - 成功返回 0,失败返回-1 int GetHardwareInfo (IHardware^ hardware) { try { hardware->Update(); // 更新硬件数据 // 根据硬件类型执行不同的操作 switch (hardware->HardwareType) { case HardwareType::Cpu: { // CPU 信息 std::cout << "CPU Information:" << std::endl; // 遍历所有传感器,获取并打印传感器名称和值 for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } case HardwareType::GpuIntel: // Intel GPU 信息 case HardwareType::GpuNvidia: // NVIDIA GPU 信息 case HardwareType::GpuAmd: { // AMD GPU 信息 std::cout << "GPU Information:" << std::endl; for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } case HardwareType::Memory: { // 内存信息 std::cout << "Memory Information:" << std::endl; for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } case HardwareType::Motherboard: { // 主板信息 std::cout << "Motherboard Information:" << std::endl; for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } case HardwareType::Storage: { // 存储设备信息 std::cout << "Storage Information:" << std::endl; for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } case HardwareType::Network: { // 网络设备信息 std::cout << "Network Information:" << std::endl; for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } case HardwareType::SuperIO: { // SuperIO 信息 std::cout << "SuperIO Information:" << std::endl; for each(ISensor ^ sensor in hardware->Sensors) { std::string name = msclr::interop::marshal_as<std::string>(sensor->Name); float value = sensor->Value.HasValue ? sensor->Value.Value : -1; if (value != -1) { std::cout << " " << name << ": " << value << std::endl; } } break; } default: // 未知硬件类型 std::cout << "Unknown Hardware Type." << std::endl; break; } // 如果硬件有子硬件,递归调用该函数处理子硬件信息 for (int i = 0; i < hardware->SubHardware->Length; i++) { GetHardwareInfo (hardware->SubHardware[i]); } } catch (Exception^ ex) { // 捕获并打印异常信息 std::cerr << "Error: " << msclr::interop::marshal_as<std::string>(ex->Message) << std::endl; return -1; } return 0; // 操作成功 } // 主函数 int main() { // 创建 Computer 对象,用于管理硬件信息 Computer^ computer = gcnew Computer(); // 启用所需硬件类型 computer->IsCpuEnabled = true; computer->IsGpuEnabled = true; computer->IsMemoryEnabled = true; computer->IsMotherboardEnabled = true; computer->IsStorageEnabled = true; computer->IsNetworkEnabled = true; computer->IsBatteryEnabled = true; computer->Open(); // 打开硬件监控 while (true) { // 遍历所有硬件并获取信息 for (int i = 0; i < computer->Hardware->Count; i++) { IHardware^ hardware = computer->Hardware[i]; GetHardwareInfo (hardware); } // 延迟 5 秒后继续监控 std::this_thread::sleep_for (std::chrono::seconds(5)); } computer->Close(); // 关闭硬件监控 return 0; // 程序正常退出 }
4,运行测试
(1)我们可以点击 Visual Studio 顶部工具栏的运行、或者调试按钮启动程序。
- 也可以直接运行生成的 exe(注意右键选择“以管理员身份运行”)
(2)程序启动后控制台输出内容如下:
附:功能优化改进
1,改进说明
(1)上面样例只是简单地把所有通过 LibreHardwareMonitor 库获取到所有硬件信息直接打印出来,不是很直观。这里我对代码进行如下改进:
- 将各类监控数据使用相应的变量进行保存,方便后续使用。
- 由于可能存在多块硬盘。因此除了保存所有硬盘的温度、使用率外,还存储了所有硬盘中目前最高的温度和使用率。
- 由于可能存在多个风扇。因此除了保存所有风扇的转速外,还存储了所有风扇中目前最高转速。
- 网络上下行速率根据数值的大小,动态选择合适的单位显示(KB/s、MB/s、GB/s、TB/s)
(2)程序执行效果如下:
2,完整代码
#include <iostream> #include <thread> #include <chrono> #include <msclr\marshal_cppstd.h> #include <map> #include <ctime> #include <sstream> #include <iomanip> #using "LibreHardwareMonitorLib.dll" // 使用的命名空间 using namespace System; using namespace LibreHardwareMonitor::Hardware; float m_cpu_usage{}; // CPU 利用率 float m_cpu_freq{}; // CPU 频率 float m_cpu_temperature{}; // CPU 温度 float m_memory_usage{}; // 内存利用率 float m_gpu_temperature{}; // GPU 温度 float m_hdd_temperature{}; // 硬盘温度 (所有硬盘中最高温度) float m_hdd_usage{}; // 硬盘利用率 (所有硬盘中占用率最高的) float m_main_board_temperature{}; // 主板温度 float m_gpu_usage{}; // GPU 利用率 float m_fan_speed{}; // 风扇转速 (所有风扇中转速最大个一个) float m_out_speed{}; // 上传速率 float m_in_speed{}; // 下载速率 std::map<std::wstring, float> m_all_hdd_temperature; // 所有硬盘温度 std::map<std::wstring, float> m_all_cpu_temperature; ; // 所有 CPU 核心温度 std::map<std::wstring, float> m_all_cpu_clock; // 所有 CPU 核心频率 std::map<std::wstring, float> m_all_hdd_usage; // 所有硬盘利用率 std::map<std::wstring, float> m_all_fan_speed; // 所有风扇转速 // 重置所有值 void ResetAllValues() { m_cpu_usage = -1; // CPU 利用率 m_cpu_freq = -1; // CPU 频率 m_cpu_temperature = -1; // CPU 温度 m_memory_usage = -1; // 内存利用率 m_gpu_temperature = -1; // GPU 温度 m_hdd_temperature = -1; // 硬盘温度 m_main_board_temperature = -1; // 主板温度 m_gpu_usage = -1; // GPU 利用率 m_hdd_usage = -1; // 硬盘利用率 m_fan_speed = -1; // 风扇转速 m_out_speed = -1; // 上传速率 m_in_speed = -1; // 下载速率 m_all_hdd_temperature.clear(); // 所有硬盘温度 m_all_hdd_usage.clear(); // 所有硬盘利用率 m_all_fan_speed.clear(); // 所有风扇转速 } // 数据大小格式化显示 std::string DataSizeToString (float size, bool with_space) { std::stringstream ss; if (size < 1024 * 10) // 10KB 以下以 KB 为单位,保留 2 位小数 ss << std::fixed << std::setprecision(2) << size / 1024.0 << " KB"; else if (size < 1024 * 1024) // 1MB 以下以 KB 为单位,保留 1 位小数 ss << std::fixed << std::setprecision(1) << size / 1024.0 << " KB"; else if (size < 1024 * 1024 * 1024) // 1GB 以下以 MB 为单位,保留 2 位小数 ss << std::fixed << std::setprecision(2) << size / 1024.0 / 1024.0 << " MB"; else if (size < 1024ll * 1024 * 1024 * 1024) ss << std::fixed << std::setprecision(2) << size / 1024.0 / 1024.0 / 1024.0 << " GB"; else ss << std::fixed << std::setprecision(2) << size / 1024.0 / 1024.0 / 1024.0 / 1024.0 << " TB"; std::string str = ss.str(); if (!with_space) str.erase (std::remove (str.begin(), str.end(), ' '), str.end()); return str; } // 输出所有值 void PrintAllValues() { // 获取当前时间 std::time_t now = std::time (nullptr); // 使用 localtime_s 代替 localtime std::tm localTime; // 使用 localtime_s 代替 localtime if (localtime_s(&localTime, &now) != 0) { std::cerr << "获取本地时间失败" << std::endl; return; } // 打印当前时间 std::cout << "------ " << (localTime.tm_hour < 10 ? "0" : "") << localTime.tm_hour << ":" << (localTime.tm_min < 10 ? "0" : "") << localTime.tm_min << ":" << (localTime.tm_sec < 10 ? "0" : "") << localTime.tm_sec << " ------" << std::endl; std::cout << "CPU 负载: " << m_cpu_usage << " %" << std::endl; std::cout << "CPU 频率: " << m_cpu_freq << " GHz" << std::endl; std::cout << "CPU 温度: " << m_cpu_temperature << " ℃" << std::endl; std::cout << "内存负载: " << m_memory_usage << " %" << std::endl; std::cout << "GPU 温度: " << m_gpu_temperature << " ℃" << std::endl; std::cout << "GPU 负载: " << m_gpu_usage << " %" << std::endl; std::cout << "主板温度: " << m_main_board_temperature << " ℃" << std::endl; std::cout << "硬盘最高温度: " << m_hdd_temperature << " ℃" << std::endl; // 遍历并打印硬盘温度 for (const auto& pair : m_all_hdd_temperature) { const std::wstring& name = pair.first; float temperature = pair.second; std::string name_str (name.begin(), name.end()); std::cout << " 硬盘【" << name_str << "】温度: " << temperature << " ℃" << std::endl; } std::cout << "硬盘最高使用率: " << m_hdd_usage << " %" << std::endl; // 遍历并打印硬盘利用率 for (const auto& pair : m_all_hdd_usage) { const std::wstring& name = pair.first; float usage = pair.second; std::string name_str (name.begin(), name.end()); std::cout << " 硬盘【" << name_str << "】使用率: " << usage << " %" << std::endl; } std::cout << "风扇最大转速: " << m_fan_speed << " 转/分" << std::endl; // 遍历并打印各风扇转速 for (const auto& pair : m_all_fan_speed) { const std::wstring& name = pair.first; float speed = pair.second; std::string name_str (name.begin(), name.end()); std::cout << " 风扇【" << name_str << "】转速: " << speed << " 转/分" << std::endl; } std::cout << "上传速率: " << DataSizeToString (m_out_speed, true) << "/s" << std::endl; std::cout << "下载速率: " << DataSizeToString (m_in_speed, true) << "/s" << std::endl; std::cout << std::endl; } // 将值插入到 map 中,如果 key 已经存在,则在末尾添加" #1",如果已经存在" #1",则将其加 1 void InsertValueToMap (std::map<std::wstring, float>& value_map, const std::wstring& key, float value) { auto iter = value_map.find (key); if (iter == value_map.end()) { value_map[key] = value; } else { std::wstring key_exist = iter->first; size_t index = key_exist.rfind (L'#'); //查找字符串是否含有#号 if (index != std::wstring::npos) { //取到#号后面的数字,将其加 1 int num = _wtoi (key_exist.substr (index + 1).c_str()); num++; key_exist = key_exist.substr(0, index + 1); key_exist += std::to_wstring (num); } else //没有#号则在末尾添加" #1" { key_exist += L" #1"; } value_map[key_exist] = value; } } //将 CRL 的 String 类型转换成 C++的 std::wstring 类型 static std::wstring ClrStringToStdWstring (System::String^ str) { if (str == nullptr) { return std::wstring(); } else { const wchar_t* chars = (const wchar_t*)(Runtime::InteropServices::Marshal:: StringToHGlobalUni (str)).ToPointer(); std::wstring os = chars; Runtime::InteropServices::Marshal::FreeHGlobal (IntPtr((void*)chars)); return os; } } // 计算 Cpu 利用率 bool GetCpuUsage (IHardware^ hardware, float& cpu_usage) { for (int i = 0; i < hardware->Sensors->Length; i++) { //找到负载 if (hardware->Sensors[i]->SensorType == SensorType::Load) { //if (hardware->Sensors[i]->Name == L"Total Activity") if (hardware->Sensors[i]->Name == L"CPU Total") { cpu_usage = Convert::ToDouble (hardware->Sensors[i]->Value); return true; } } } return false; } // 计算 CPU 温度 (所有核心温度平均值) bool GetCpuTemperature (IHardware^ hardware, float& temperature) { m_all_cpu_temperature.clear(); for (int i = 0; i < hardware->Sensors->Length; i++) { //找到温度传感器 if (hardware->Sensors[i]->SensorType == SensorType::Temperature) { String^ name = hardware->Sensors[i]->Name; //保存每个 CPU 传感器的温度 m_all_cpu_temperature[ClrStringToStdWstring (name)] = Convert::ToDouble (hardware->Sensors[i]->Value); } } //计算平均温度 if (!m_all_cpu_temperature.empty()) { float sum{}; for (const auto& item : m_all_cpu_temperature) sum += item.second; temperature = sum / m_all_cpu_temperature.size(); } return temperature > 0; } // 计算 CPU 频率 (所有核心时钟频率平均值) bool GetCPUFreq (IHardware^ hardware, float& freq) { for (int i = 0; i < hardware->Sensors->Length; i++) { if (hardware->Sensors[i]->SensorType == SensorType::Clock) { String^ name = hardware->Sensors[i]->Name; if (name != L"Bus Speed") m_all_cpu_clock[ClrStringToStdWstring (name)] = Convert::ToDouble (hardware->Sensors[i]->Value); } } float sum{}; for (auto i : m_all_cpu_clock) sum += i.second; freq = sum / m_all_cpu_clock.size() / 1000.0; return true; } // 计算内存利用率 bool GetMemoryUsage (IHardware^ hardware, float& memory_usage) { for (int i = 0; i < hardware->Sensors->Length; i++) { //找到负载 if (hardware->Sensors[i]->SensorType == SensorType::Load) { //if (hardware->Sensors[i]->Name == L"Total Activity") if (hardware->Sensors[i]->Name == L"Memory") { memory_usage = Convert::ToDouble (hardware->Sensors[i]->Value); return true; } } } return false; } // 计算硬件温度 (GPU、主板、硬盘) bool GetHardwareTemperature (IHardware^ hardware, float& temperature) { std::vector<float> all_temperature; float core_temperature{ -1 }; System::String^ temperature_name; switch (hardware->HardwareType) { case HardwareType::Cpu: temperature_name = L"Core Average"; break; case HardwareType::GpuNvidia: case HardwareType::GpuAmd: case HardwareType::GpuIntel: temperature_name = L"GPU Core"; break; default: break; } for (int i = 0; i < hardware->Sensors->Length; i++) { //找到温度传感器 if (hardware->Sensors[i]->SensorType == SensorType::Temperature) { float cur_temperture = Convert::ToDouble (hardware->Sensors[i]->Value); all_temperature.push_back (cur_temperture); //如果找到了名称为 temperature_name 的温度传感器,则将温度保存到 core_temperature 里 if (hardware->Sensors[i]->Name == temperature_name) core_temperature = cur_temperture; } } if (core_temperature >= 0) { temperature = core_temperature; return true; } if (!all_temperature.empty()) { //如果有多个温度传感器,则取平均值 float sum{}; for (auto i : all_temperature) sum += i; temperature = sum / all_temperature.size(); return true; } //如果没有找到温度传感器,则在 SubHardware 中寻找 for (int i = 0; i < hardware->SubHardware->Length; i++) { if (GetHardwareTemperature (hardware->SubHardware[i], temperature)) return true; } return false; } // 计算 GPU 利用率 bool GetGpuUsage (IHardware^ hardware, float& gpu_usage) { for (int i = 0; i < hardware->Sensors->Length; i++) { //找到负载 if (hardware->Sensors[i]->SensorType == SensorType::Load) { if (hardware->Sensors[i]->Name == L"GPU Core") { float cur_gpu_usage = Convert::ToDouble (hardware->Sensors[i]->Value); if (gpu_usage < cur_gpu_usage) { gpu_usage = cur_gpu_usage; } } } } return gpu_usage > 0; } // 计算硬盘利用率 bool GetHddUsage (IHardware^ hardware, float& hdd_usage) { for (int i = 0; i < hardware->Sensors->Length; i++) { //找到负载 if (hardware->Sensors[i]->SensorType == SensorType::Load) { //if (hardware->Sensors[i]->Name == L"Total Activity") if (hardware->Sensors[i]->Name == L"Used Space") { hdd_usage = Convert::ToDouble (hardware->Sensors[i]->Value); return true; } } } return false; } // 计算风扇转速 bool GetFanSpeed (IHardware^ hardware, float& fan_speed) { m_all_fan_speed.clear(); for (int i = 0; i < hardware->Sensors->Length; i++) { //找到风扇 if (hardware->Sensors[i]->SensorType == SensorType::Fan) { String^ name = hardware->Sensors[i]->Name; float speed = Convert::ToDouble (hardware->Sensors[i]->Value); //保存每个风扇的转速 m_all_fan_speed[ClrStringToStdWstring (name)] = speed; if (speed > fan_speed) fan_speed = speed; } } return fan_speed > 0; } // 计算网络上下行速率 bool GetNetworkSpeed (IHardware^ hardware, float& m_out_speed, float& m_in_speed) { bool flag = false; for (int i = 0; i < hardware->Sensors->Length; i++) { //找到负载 if (hardware->Sensors[i]->SensorType == SensorType::Throughput) { //if (hardware->Sensors[i]->Name == L"Total Activity") if (hardware->Sensors[i]->Name == L"Upload Speed") { float speed = Convert::ToDouble (hardware->Sensors[i]->Value); if (m_out_speed < speed) { m_out_speed = speed; } flag = true; } else if (hardware->Sensors[i]->Name == L"Download Speed") { float speed = Convert::ToDouble (hardware->Sensors[i]->Value); if (m_in_speed < speed) { m_in_speed = speed; } flag = true; } } } return flag; } // 函数:GetHardwareInfo // 描述:递归地获取硬件信息并打印到控制台 // 参数:IHardware^ hardware - 当前硬件对象 // 返回值:int - 成功返回 0,失败返回-1 int GetHardwareInfo (IHardware^ hardware) { try { hardware->Update(); // 更新硬件数据 // 根据硬件类型执行不同的操作 switch (hardware->HardwareType) { case HardwareType::Cpu: { // CPU 信息 // 获取 CPU 利用率 if (m_cpu_usage < 0) GetCpuUsage (hardware, m_cpu_usage); // 获取 CPU 温度 if (m_cpu_temperature < 0) GetCpuTemperature (hardware, m_cpu_temperature); // 获取 CPU 频率 if (m_cpu_freq < 0) GetCPUFreq (hardware, m_cpu_freq); break; } case HardwareType::GpuIntel: // Intel GPU 信息 case HardwareType::GpuNvidia: // NVIDIA GPU 信息 case HardwareType::GpuAmd: { // AMD GPU 信息 // 获取 GPU 温度 if (m_gpu_temperature < 0) GetHardwareTemperature (hardware, m_gpu_temperature); // 获取 GPU 利用率 if (m_gpu_usage < 0) GetGpuUsage (hardware, m_gpu_usage); break; } case HardwareType::Memory: { // 内存信息 // 获取内存利用率 if (m_memory_usage < 0) GetMemoryUsage (hardware, m_memory_usage); break; } case HardwareType::Motherboard: { // 主板信息 // 获取主板温度 if (m_main_board_temperature < 0) GetHardwareTemperature (hardware, m_main_board_temperature); break; } case HardwareType::Storage: { // 存储设备信息 // 获取硬盘温度 float cur_hdd_temperature = -1; GetHardwareTemperature (hardware, cur_hdd_temperature); InsertValueToMap (m_all_hdd_temperature, ClrStringToStdWstring (hardware->Name), cur_hdd_temperature); if (m_hdd_temperature < cur_hdd_temperature) m_hdd_temperature = cur_hdd_temperature; // 获取硬盘利用率 float cur_hdd_usage = -1; GetHddUsage (hardware, cur_hdd_usage); InsertValueToMap (m_all_hdd_usage, ClrStringToStdWstring (hardware->Name), cur_hdd_usage); if (m_hdd_usage < cur_hdd_usage) m_hdd_usage = cur_hdd_usage; break; } case HardwareType::Network: { // 网络设备信息 // 获取网络速率 //if (m_out_speed < 0 || m_in_speed GetNetworkSpeed (hardware, m_out_speed, m_in_speed); break; } case HardwareType::SuperIO: { // SuperIO 信息 // 计算风扇转速 if (m_fan_speed < 0) GetFanSpeed (hardware, m_fan_speed); break; } default: // 未知硬件类型 //std::cout << "Unknown Hardware Type." << std::endl; break; } // 如果硬件有子硬件,递归调用该函数处理子硬件信息 for (int i = 0; i < hardware->SubHardware->Length; i++) { GetHardwareInfo (hardware->SubHardware[i]); } } catch (Exception^ ex) { // 捕获并打印异常信息 std::cerr << "Error: " << msclr::interop::marshal_as<std::string>(ex->Message) << std::endl; return -1; } return 0; // 操作成功 } // 主函数 int main() { // 创建 Computer 对象,用于管理硬件信息 Computer^ computer = gcnew Computer(); // 启用所需硬件类型 computer->IsCpuEnabled = true; computer->IsGpuEnabled = true; computer->IsMemoryEnabled = true; computer->IsMotherboardEnabled = true; computer->IsStorageEnabled = true; computer->IsNetworkEnabled = true; computer->IsBatteryEnabled = true; computer->Open(); // 打开硬件监控 while (true) { // 重置所有值 ResetAllValues(); // 遍历所有硬件并获取信息 for (int i = 0; i < computer->Hardware->Count; i++) { IHardware^ hardware = computer->Hardware[i]; GetHardwareInfo (hardware); } // 输出所有值 PrintAllValues(); // 延迟 5 秒后继续监控 std::this_thread::sleep_for (std::chrono::seconds(5)); } computer->Close(); // 关闭硬件监控 return 0; // 程序正常退出 }
全部评论(0)