程序笔记   发布时间:2022-05-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C++读取注册表的实现方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

C++读取注册表

GetRegValue.h:

#ifndef __GETREGVALUE_H__
#define __GETREGVALUE_H__

#include <String>

//---------------------------------------------------------------
//function: 
//     GetRegValue 获取注册表中指定键的值
//Access:
//      public 
//Parameter:
//     [in] int nKeyType - 注册表项的类型,传入的参数只可能是以下数值:
//               0:HKEY_CLASSES_ROOT
//               1:HKEY_CURRENT_USER
//               2:HKEY_LOCAL_MACHINE
//               3:HKEY_USERS
//               4:HKEY_PERFORMANCE_DATA
//               5:HKEY_CURRENT_CONfig
//               6:HKEY_DYN_DATA
//               7:HKEY_CURRENT_user_LOCAL_SETTinGS
//               8:HKEY_PERFORMANCE_TEXT
//               9:HKEY_PERFORMANCE_NLSTEXT
//     [in] const std::string & strUrl - 要查找 的键的路径
//     [in] const std::string & strKey - 指定的键
//Returns:
//     std::string - 指定键的值
//REMARKs:
//     ...
//author:  luoweifu
//---------------------------------------------------------------
std::string GetRegValue(int nKeyType,const std::string& strUrl,const std::string& strKey);

//可移植版本 wString => String
std::string ws2s(const std::wString& ws);

//可移植版本 String => wString
std::wString s2ws(const std::string& s);


#endif //__GETREGVALUE_H__

GetRegValue.cpp

#include "stdafx.h"
#include <windows.h>
#include "GetRegValue.h"

//可移植版本 wString => String
std::string ws2s(const std::wString& ws)
{
  std::string curLocale = setlocale(LC_ALL,"");
  const wchar_t* _source = ws.c_str();
  size_t _Dsize = wcstombs(NulL,_source,0) + 1;
  char *_Dest = new char[_Dsize];
  memset(_Dest,_DsizE);
  wcstombs(_Dest,_DsizE);
  std::string result = _Dest;
  delete []_Dest;
  setlocale(LC_ALL,curLocale.c_str());
  return result;
}

//可移植版本 String => wString
std::wString s2ws(const std::string& s)
{
  std::string curLocale = setlocale(LC_ALL,""); 
  const char* _source = s.c_str();
  size_t _Dsize = mbstowcs(NulL,0) + 1;
  wchar_t *_Dest = new wchar_t[_Dsize];
  wmemset(_Dest,_DsizE);
  mbstowcs(_Dest,_DsizE);
  std::wString result = _Dest;
  delete []_Dest;
  setlocale(LC_ALL,curLocale.c_str());
  return result;
}

std::string GetRegValue(int nKeyType,const std::string& strKey)
{
  std::string strValue("");
  HKEY hKey = NulL;
  HKEY hKeyResult = NulL;
  DWORD DWSize   = 0;
  DWORD DWDataType = 0;
  std::wString wstrUrl = s2ws(strUrl);
  std::wString wstrKey = s2ws(strKey);

  switch(nKeyTypE)
  {
  case 0:
    {
      hKey = HKEY_CLASSES_ROOT;
      break;
    }
  case 1:
    {
      hKey = HKEY_CURRENT_USER;
      break;
    }
  case 2:
    {
      hKey = HKEY_LOCAL_MACHINE;
      break;
    }
  case 3:
    {
      hKey = HKEY_USERS;
      break;
    }
  case 4:
    {
      hKey = HKEY_PERFORMANCE_DATA;
      break;
    }
  case 5:
    {
      hKey = HKEY_CURRENT_CONfig;
      break;
    }
  case 6:
    {
      hKey = HKEY_DYN_DATA;
      break;
    }
  case 7:
    {
      hKey = HKEY_CURRENT_user_LOCAL_SETTinGS;
      break;
    }
  case 8:
    {
      hKey = HKEY_PERFORMANCE_TEXT;
      break;
    }
  case 9:
    {
      hKey = HKEY_PERFORMANCE_NLSTEXT;
      break;
    }
  default:
    {
      return strValue;
    }
  }

  //打开注册表
  if(ERROR_succesS == ::regOpenKeyEx(hKey,wstrUrl.c_str(),KEY_query_VALUE,&hKeyResult))
  {
    // 获取缓存的长度DWSize及类型DWDataType
    ::regqueryValueEx(hKeyResult,wstrKey.c_str(),&DWDataType,NulL,&DWSizE); 
    switch (DWDataTypE)
    {
    case REG_MulTI_SZ:
      {
        //分配内存大小
        BYTE* lpValue = new BYTE[DWSize];
        //获取注册表中指定的键所对应的值
        LONG lRet = ::regqueryValueEx(hKeyResult,lpValue,&DWSizE);
        delete[] lpValue;
        break;
      }
    case REG_SZ:
      {
        //分配内存大小
        wchar_t* lpValue = new wchar_t[DWSize];
        memset(lpValue,DWSize * sizeof(wchar_t));
        //获取注册表中指定的键所对应的值
        if (ERROR_succesS == ::regqueryValueEx(hKeyResult,(LPBYTE)lpValue,&DWSizE))
        {
          std::wString wstrValue(lpvalue);
          strValue = ws2s(wstrvalue);
        }
        delete[] lpValue;
        break;
      }
    default:
      break;
    }
  }

  //关闭注册表
  ::regCloseKey(hKeyResult);


  return strValue;
}

测试代码:

#include "stdafx.h"
#include <String>
#include "GetRegValue.h"

int _tmain(int argc,_TCHAR* argv[])
{
  std::string strValue = GetRegValue(2,"SOFTWARE\\360Safe\\liveup","mID");
  return 0;
}

结果:

strValue:

“ebd1360403764c9d48c585ef93a6eACBd89ded596f043f78e54eb0adeba7251d”

C++读取注册表的实现方法

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

大佬总结

以上是大佬教程为你收集整理的C++读取注册表的实现方法全部内容,希望文章能够帮你解决C++读取注册表的实现方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。