vc++基础班[25]---系统信息的获取

---------------------------------------------------------------------------
VC 驿站
WwW.CcTry.CoM
多抽出一分钟时间来学习,让你的生命更加精彩!
C、C++、VC++ 各种学习资源,免费教程,期待您的加入!
动画教程只是起到技术交流的作用,请大家不用利用此方法做非法用途.
由此动画造成的任何后果和动画作者及本站无关.
----------------------------------------------------------------------------------------
大家好,我是Syc
今天给大家做的教程是:
vc++基础班[25]---系统信息的获取
------------------------------------------ Begin ----------------------------------------
 
①、获取当前登录用户名称:
函数为:GetUserName,准确的说此函数为获取当前线程关联的用户名称;
 
②、获取计算机名称:
函数为:GetComputerName,当然也可以通过 SetComputerName 函数来进行计算机名称的设置,但必须重新启动系统!
 
③、获取CPU的描述信息:
CString GetCpuDescription()
{
 CString strCPU;
 HKEY hKey = NULL;
 LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  _T("Hardware\\Description\\System\\CentralProcessor\\0") , 0, KEY_QUERY_VALUE, &hKey);
 if(lRet == ERROR_SUCCESS){
  DWORD dwSize = 0;
  BYTE *pszBuf = NULL;
  lRet = RegQueryValueEx(hKey, _T("ProcessorNameString"), NULL, NULL, NULL, &dwSize);
  if(lRet == ERROR_SUCCESS && dwSize > 0){
   pszBuf = new BYTE[dwSize+1];
   ZeroMemory(pszBuf, dwSize+1);
   RegQueryValueEx(hKey, _T("ProcessorNameString"), NULL, NULL, pszBuf, &dwSize);
  }
  RegCloseKey(hKey);
  strCPU.Format(_T("%s"), pszBuf);
  if (pszBuf) delete [] pszBuf;
 }
 return strCPU;
}
 
④、获取内存信息:
ULONGLONG GetMemoryInfo()
{
 ULONGLONG dwMemSpace = 0;
 MEMORYSTATUSEX memStatus = {0};
 memStatus.dwLength = sizeof(MEMORYSTATUSEX);
 GlobalMemoryStatusEx(&memStatus);
 dwMemSpace = (ULONGLONG)(memStatus.ullTotalPhys /1024/1024);
 return dwMemSpace;
}
 
⑤、获取磁盘列表及容量信息:
计算机存储单位:Byte、KB、MB、GB、TB、PB、EB、ZB、YB、DB、NB
////////////////////////////////////////////////////////////////////////////////
CString EnumDrives()
{
 UINT nCount = 0;
 CString strDrives;
 TCHAR *pBuf = NULL;
 //原则上来说 4*26+1 大小就够用了,每个盘符占用4个字符,也就是:C:\\0
 TCHAR szBuf[MAX_PATH] = {0};
 DWORD dwRet = GetLogicalDriveStrings(MAX_PATH, szBuf);
 if (dwRet != 0){
  nCount = dwRet/4;
  pBuf = szBuf;
  for (UINT idx = 0; idx < nCount; ++idx, pBuf += 4){
   strDrives.AppendFormat(_T("%s%s"), pBuf, _T("\r\n"));
  }
 }
 return strDrives;
}
 
CString GetDriveTypeString(CString strDrive)
{
 //传递的参数至少要包含:盘符+ :
 //也可以是目录的路径,但是结尾必须有\\符号
 CString strDriveTpye;
 UINT uType = GetDriveType(strDrive);
 switch (uType)
 {
 case DRIVE_UNKNOWN:
  strDriveTpye = _T("类型未知!");
  break;
 case DRIVE_NO_ROOT_DIR:
  strDriveTpye = _T("指定的盘符不存在!");
  break;
 case DRIVE_REMOVABLE:
  strDriveTpye = _T("软盘或U盘");
  break;
 case DRIVE_FIXED:
  strDriveTpye = _T("本地硬盘或移动硬盘");
  break;
 case DRIVE_REMOTE:
  strDriveTpye = _T("网络磁盘");
  break;
 case DRIVE_CDROM:
  strDriveTpye = _T("CD-ROM");
  break;
 case DRIVE_RAMDISK:
  strDriveTpye = _T("RAM 磁盘");
  break;
 default:
  break;
 }
 return strDriveTpye;
}
 
CString GetDriveSpaceInfo(CString strDrive)
{
 CString strInfo;
 ULARGE_INTEGER nFreeBytesAvailable = {0};
 ULARGE_INTEGER nTotalNumberOfBytes = {0};
 ULARGE_INTEGER nTotalNumberOfFreeBytes = {0};
 
 if (GetDiskFreeSpaceEx(strDrive, &nFreeBytesAvailable, &nTotalNumberOfBytes, &nTotalNumberOfFreeBytes)) {
  //单位是字节
  //strInfo.Format(_T("可用容量:%I64u Byte\r\n总容量:%I64u Byte\r\n所有可用容量:%I64u Byte"),
  // nFreeBytesAvailable.QuadPart, nTotalNumberOfBytes.QuadPart, nTotalNumberOfFreeBytes.QuadPart);
 
  //单位是GB
  long double dFreeBytesAvailable = ((long double)(nFreeBytesAvailable.QuadPart))/(1024*1024*1024);
  long double dTotalNumberOfBytes = ((long double)(nTotalNumberOfBytes.QuadPart))/(1024*1024*1024);
  long double dTotalNumberOfFreeBytes = ((long double)(nTotalNumberOfFreeBytes.QuadPart))/(1024*1024*1024);
 
  strInfo.Format(_T("可用容量:%.2Lf GB\r\n总容量:%.2Lf GB\r\n所有可用容量:%.2Lf GB"),
   dFreeBytesAvailable, dTotalNumberOfBytes, dTotalNumberOfFreeBytes);
 }
 
 return strInfo;
}
 
posted @ 2015-01-23 09:53  廖先生  阅读(248)  评论(0编辑  收藏  举报