如何通过MFC技术获取个人电脑的硬件配置详情?

2026-05-08 19:233阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1070个文字,预计阅读时间需要5分钟。

如何通过MFC技术获取个人电脑的硬件配置详情?

本文以家庭分享为例,展示了如何使用C++和MFC获取PC硬件配置信息的整体代码。主要内容包括函数和具体代码如下:

函数示例:

cppvoid GetHardwareInfo(){ // 获取硬件信息}

具体代码:

cpp#include

void GetHardwareInfo(){ // 获取CPU信息 SYSTEM_INFO si; GetSystemInfo(&si); printf(CPU: %s\n, si.processorArchitecture);

// 获取内存信息 MEMORYSTATUS ms; GlobalMemoryStatus(&ms); printf(Memory: %ld MB\n, ms.dwTotalPhys / 1024 / 1024);

// 获取硬盘信息 char driveType[5]; driveType[0]='C'; driveType[1]=':'; driveType[2]='\\'; driveType[3]='\0'; char volumeName[64]; char filesystem[64]; DWORD serialNumber; GetVolumeInformation(driveType, volumeName, sizeof(volumeName), &serialNumber, NULL, NULL, filesystem, 0); printf(Hard Disk: %s (%s, SN: %ld)\n, volumeName, filesystem, serialNumber);

// 获取网络信息 char computerName[256]; char domainName[256]; char workgroupName[256]; GetComputerName(computerName, sizeof(computerName)); GetDomainName(domainName, sizeof(domainName)); GetComputerGroup(domainName, workgroupName, sizeof(workgroupName)); printf(Computer: %s, Domain: %s, Workgroup: %s\n, computerName, domainName, workgroupName);}

int main(){ GetHardwareInfo(); return 0;}

使用VS2010编译,在XP和Win7电脑上运行。使用循环(loop)测试此方法。

本文实例为大家分享了C++使用MFC获取PC硬件配置信息的具体代码,供大家参考,具体内容如下

函数如下, 使用VS2010编译通过,在XP和Win7电脑上运行过,被注释的loop 循环用于测试此方法是否稳定:

//包含的头文件: #include "afxdialogex.h" #include <windows.h> #include <sstream> #include <fstream> void Cget_systemDlg::OnBnClickedButton1() { //for (int i = 0; i < 10000; i++) { std::ostringstream osConfig; osConfig.precision(1); // Computer Name TCHAR infoBuf[512] = {0}; DWORD bufCharCount = 512; if (GetComputerName(infoBuf, &bufCharCount)) // MSDN: If the function succeeds, the return value is a nonzero value. osConfig << "Computer Name: " << infoBuf << std::endl; else osConfig << "!Error: failed to get computer name" << std::endl; // CPU CString strPath = CString("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); CRegKey regKey; LONG lResult; lResult = regKey.Open(HKEY_LOCAL_MACHINE, strPath, KEY_READ); if (lResult == ERROR_SUCCESS) { TCHAR chCPUName[256] = {0}; DWORD dwSize = 256; LPCSTR name = "ProcessorNameString"; regKey.QueryValue(chCPUName,_T("ProcessorNameString"),&dwSize); osConfig << "CPU Type: " << (char*)chCPUName << std::endl; regKey.Close(); } else osConfig << "!Error: failed to open register for read CPU." << std::endl; // OS Name char szOsType[MAX_PATH]; HKEY hKey; LPCTSTR StrKey="Software\\Microsoft\\Windows NT\\CurrentVersion"; if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,StrKey,NULL,KEY_READ,&hKey)) { DWORD dwSize = 255; DWORD dwType = REG_SZ; LPCSTR KeyValue = "ProductName"; if (ERROR_SUCCESS == ::RegQueryValueEx(hKey,KeyValue,0,&dwType,(BYTE *)szOsType,&dwSize)) osConfig << "OS : " << szOsType; else osConfig << "!Error: failed to get OS name." << std::endl; ::RegCloseKey(hKey); } else osConfig << "!Error: failed to get OS name." << std::endl; SYSTEM_INFO si; GetNativeSystemInfo(&si); // MSDN: This function does not return a value. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 ) osConfig << " 64bits"; else osConfig << " 32bits"; OSVERSIONINFO osVer; osVer.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (::GetVersionEx (&osVer)) // MSDN: If the function succeeds, the return value is a nonzero value. osConfig << " " << osVer.szCSDVersion << " " << std::endl; else osConfig << "!Error: failed to get OS CSD version."; // Memory MEMORYSTATUSEX statusex; statusex.dwLength = sizeof (statusex); if (GlobalMemoryStatusEx(&statusex)) // MSDN: If the function succeeds, the return value is nonzero. { osConfig << "Installed RAM: "<< std::fixed << statusex.ullTotalPhys /1024/1024/1024.0 << "GB"; osConfig << " Free RAM: " << std::fixed << statusex.ullAvailPhys/1024/1024/1024.0 << "GB" << std::endl; } else osConfig << "!Error: failed to get RAM information." << std::endl; // Hard Drive // GetLogicalDriveStrings(sizeof(cDriveArray), cDriveArray); // MSDN: If the function fails, the return value is zero. // If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, // not including the terminating null character ULARGE_INTEGER free; ULARGE_INTEGER total; ULARGE_INTEGER totalfree; TCHAR szDriveArray[128] = {0}; TCHAR *p, *pStart; pStart = szDriveArray; p = szDriveArray; int nLen = GetLogicalDriveStrings(sizeof(szDriveArray), szDriveArray); if (nLen !=0 ) { while ((p = _tcschr(p, _T('\0'))) && p != szDriveArray + nLen ) { if (GetDiskFreeSpaceEx(pStart, &free, &total, &totalfree)) // if success, nonzero { osConfig << "[" << pStart << "]" << " Total: " << std::fixed << total.QuadPart/1024/1024/1024.0 << "GB Available: " << std::fixed << free.QuadPart/1024/1024/1024.0 << "GB"; if (GetDriveType(pStart) == 3) osConfig <<" - Fixed Drive" << std::endl; else if (GetDriveType(pStart) == 4) osConfig <<" - (Remote Drive)" << std::endl; else osConfig <<" - Other" << std::endl; } pStart = ++p; } } else osConfig << "!Error: failed to get logical drive name." << std::endl; AfxMessageBox(osConfig.str().c_str()); //std::ostringstream file; //file << i << ".txt"; //std::ofstream log(file.str().c_str()); //log << osConfig.str().c_str(); //log.close(); //log.clear(); //} AfxMessageBox("DONE"); }

输出类似如下的信息:

如何通过MFC技术获取个人电脑的硬件配置详情?

Computer Name: ATE CPU Type: Intel(R) Core(TM)2 Duo CPU E7500 @ 2.93GHz OS : Windows 7 Ultimate 64bits Service Pack 1 Installed RAM: 3.9GB Free RAM: 1.2GB [C:\] Total: 48.8GB Available: 13.4GB - Fixed Drive [D:\] Total: 100.0GB Available: 52.5GB - Fixed Drive [E:\] Total: 149.3GB Available: 7.2GB - Fixed Drive [K:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [M:\] Total: 2000.0GB Available: 1703.8GB - (Remote Drive) [N:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [P:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [T:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [X:\] Total: 142.8GB Available: 20.9GB - (Remote Drive)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计1070个文字,预计阅读时间需要5分钟。

如何通过MFC技术获取个人电脑的硬件配置详情?

本文以家庭分享为例,展示了如何使用C++和MFC获取PC硬件配置信息的整体代码。主要内容包括函数和具体代码如下:

函数示例:

cppvoid GetHardwareInfo(){ // 获取硬件信息}

具体代码:

cpp#include

void GetHardwareInfo(){ // 获取CPU信息 SYSTEM_INFO si; GetSystemInfo(&si); printf(CPU: %s\n, si.processorArchitecture);

// 获取内存信息 MEMORYSTATUS ms; GlobalMemoryStatus(&ms); printf(Memory: %ld MB\n, ms.dwTotalPhys / 1024 / 1024);

// 获取硬盘信息 char driveType[5]; driveType[0]='C'; driveType[1]=':'; driveType[2]='\\'; driveType[3]='\0'; char volumeName[64]; char filesystem[64]; DWORD serialNumber; GetVolumeInformation(driveType, volumeName, sizeof(volumeName), &serialNumber, NULL, NULL, filesystem, 0); printf(Hard Disk: %s (%s, SN: %ld)\n, volumeName, filesystem, serialNumber);

// 获取网络信息 char computerName[256]; char domainName[256]; char workgroupName[256]; GetComputerName(computerName, sizeof(computerName)); GetDomainName(domainName, sizeof(domainName)); GetComputerGroup(domainName, workgroupName, sizeof(workgroupName)); printf(Computer: %s, Domain: %s, Workgroup: %s\n, computerName, domainName, workgroupName);}

int main(){ GetHardwareInfo(); return 0;}

使用VS2010编译,在XP和Win7电脑上运行。使用循环(loop)测试此方法。

本文实例为大家分享了C++使用MFC获取PC硬件配置信息的具体代码,供大家参考,具体内容如下

函数如下, 使用VS2010编译通过,在XP和Win7电脑上运行过,被注释的loop 循环用于测试此方法是否稳定:

//包含的头文件: #include "afxdialogex.h" #include <windows.h> #include <sstream> #include <fstream> void Cget_systemDlg::OnBnClickedButton1() { //for (int i = 0; i < 10000; i++) { std::ostringstream osConfig; osConfig.precision(1); // Computer Name TCHAR infoBuf[512] = {0}; DWORD bufCharCount = 512; if (GetComputerName(infoBuf, &bufCharCount)) // MSDN: If the function succeeds, the return value is a nonzero value. osConfig << "Computer Name: " << infoBuf << std::endl; else osConfig << "!Error: failed to get computer name" << std::endl; // CPU CString strPath = CString("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); CRegKey regKey; LONG lResult; lResult = regKey.Open(HKEY_LOCAL_MACHINE, strPath, KEY_READ); if (lResult == ERROR_SUCCESS) { TCHAR chCPUName[256] = {0}; DWORD dwSize = 256; LPCSTR name = "ProcessorNameString"; regKey.QueryValue(chCPUName,_T("ProcessorNameString"),&dwSize); osConfig << "CPU Type: " << (char*)chCPUName << std::endl; regKey.Close(); } else osConfig << "!Error: failed to open register for read CPU." << std::endl; // OS Name char szOsType[MAX_PATH]; HKEY hKey; LPCTSTR StrKey="Software\\Microsoft\\Windows NT\\CurrentVersion"; if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,StrKey,NULL,KEY_READ,&hKey)) { DWORD dwSize = 255; DWORD dwType = REG_SZ; LPCSTR KeyValue = "ProductName"; if (ERROR_SUCCESS == ::RegQueryValueEx(hKey,KeyValue,0,&dwType,(BYTE *)szOsType,&dwSize)) osConfig << "OS : " << szOsType; else osConfig << "!Error: failed to get OS name." << std::endl; ::RegCloseKey(hKey); } else osConfig << "!Error: failed to get OS name." << std::endl; SYSTEM_INFO si; GetNativeSystemInfo(&si); // MSDN: This function does not return a value. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 ) osConfig << " 64bits"; else osConfig << " 32bits"; OSVERSIONINFO osVer; osVer.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (::GetVersionEx (&osVer)) // MSDN: If the function succeeds, the return value is a nonzero value. osConfig << " " << osVer.szCSDVersion << " " << std::endl; else osConfig << "!Error: failed to get OS CSD version."; // Memory MEMORYSTATUSEX statusex; statusex.dwLength = sizeof (statusex); if (GlobalMemoryStatusEx(&statusex)) // MSDN: If the function succeeds, the return value is nonzero. { osConfig << "Installed RAM: "<< std::fixed << statusex.ullTotalPhys /1024/1024/1024.0 << "GB"; osConfig << " Free RAM: " << std::fixed << statusex.ullAvailPhys/1024/1024/1024.0 << "GB" << std::endl; } else osConfig << "!Error: failed to get RAM information." << std::endl; // Hard Drive // GetLogicalDriveStrings(sizeof(cDriveArray), cDriveArray); // MSDN: If the function fails, the return value is zero. // If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, // not including the terminating null character ULARGE_INTEGER free; ULARGE_INTEGER total; ULARGE_INTEGER totalfree; TCHAR szDriveArray[128] = {0}; TCHAR *p, *pStart; pStart = szDriveArray; p = szDriveArray; int nLen = GetLogicalDriveStrings(sizeof(szDriveArray), szDriveArray); if (nLen !=0 ) { while ((p = _tcschr(p, _T('\0'))) && p != szDriveArray + nLen ) { if (GetDiskFreeSpaceEx(pStart, &free, &total, &totalfree)) // if success, nonzero { osConfig << "[" << pStart << "]" << " Total: " << std::fixed << total.QuadPart/1024/1024/1024.0 << "GB Available: " << std::fixed << free.QuadPart/1024/1024/1024.0 << "GB"; if (GetDriveType(pStart) == 3) osConfig <<" - Fixed Drive" << std::endl; else if (GetDriveType(pStart) == 4) osConfig <<" - (Remote Drive)" << std::endl; else osConfig <<" - Other" << std::endl; } pStart = ++p; } } else osConfig << "!Error: failed to get logical drive name." << std::endl; AfxMessageBox(osConfig.str().c_str()); //std::ostringstream file; //file << i << ".txt"; //std::ofstream log(file.str().c_str()); //log << osConfig.str().c_str(); //log.close(); //log.clear(); //} AfxMessageBox("DONE"); }

输出类似如下的信息:

如何通过MFC技术获取个人电脑的硬件配置详情?

Computer Name: ATE CPU Type: Intel(R) Core(TM)2 Duo CPU E7500 @ 2.93GHz OS : Windows 7 Ultimate 64bits Service Pack 1 Installed RAM: 3.9GB Free RAM: 1.2GB [C:\] Total: 48.8GB Available: 13.4GB - Fixed Drive [D:\] Total: 100.0GB Available: 52.5GB - Fixed Drive [E:\] Total: 149.3GB Available: 7.2GB - Fixed Drive [K:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [M:\] Total: 2000.0GB Available: 1703.8GB - (Remote Drive) [N:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [P:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [T:\] Total: 4607.9GB Available: 230.9GB - (Remote Drive) [X:\] Total: 142.8GB Available: 20.9GB - (Remote Drive)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。