网站建设资讯

NEWS

网站建设资讯

C++使用MFC获取PC硬件配置信息-创新互联

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

为木垒哈萨克等地区用户提供了全套网页设计制作服务,及木垒哈萨克网站建设行业解决方案。主营业务为网站制作、网站设计、木垒哈萨克网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

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

//包含的头文件:
#include "afxdialogex.h"
#include 
#include 
#include 

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");
}

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:C++使用MFC获取PC硬件配置信息-创新互联
本文网址:http://njwzjz.com/article/piigc.html