C++读取注册表

GetRegValue.h:

#ifndef __GETREGVALUE_H__

#define __GETREGVALUE_H__

#include

//---------------------------------------------------------------

//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

#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,0,_Dsize);

wcstombs(_Dest,_Source,_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, _Source, 0) + 1;

wchar_t *_Dest = new wchar_t[_Dsize];

wmemset(_Dest, 0, _Dsize);

mbstowcs(_Dest,_Source,_Dsize);

std::wstring result = _Dest;

delete []_Dest;

setlocale(LC_ALL, curLocale.c_str());

return result;

}

std::string GetRegValue(int nKeyType, const std::string& strUrl, 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(), 0, KEY_QUERY_VALUE, &hKeyResult))

{

// 获取缓存的长度dwSize及类型dwDataType

::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, NULL, &dwSize);

switch (dwDataType)

{

case REG_MULTI_SZ:

{

//分配内存大小

BYTE* lpValue = new BYTE[dwSize];

//获取注册表中指定的键所对应的值

LONG lRet = ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, lpValue, &dwSize);

delete[] lpValue;

break;

}

case REG_SZ:

{

//分配内存大小

wchar_t* lpValue = new wchar_t[dwSize];

memset(lpValue, 0, dwSize * sizeof(wchar_t));

//获取注册表中指定的键所对应的值

if (ERROR_SUCCESS == ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, (LPBYTE)lpValue, &dwSize))

{

std::wstring wstrValue(lpValue);

strValue = ws2s(wstrValue);

}

delete[] lpValue;

break;

}

default:

break;

}

}

//关闭注册表

::RegCloseKey(hKeyResult);

return strValue;

}

测试代码:

#include "stdafx.h"

#include

#include "GetRegValue.h"

int _tmain(int argc, _TCHAR* argv[])

{

std::string strValue = GetRegValue(2, "SOFTWARE\\360Safe\\Liveup", "mid");

return 0;

}

结果:

strValue:

“ebd1360403764c9d48c585ef93a6eacbd89ded596f043f78e54eb0adeba7251d”

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

c 语言读取注册表信息,C++读取注册表的实现方法相关推荐

  1. 计算机维修人员评价表,信息技术课学生评价表

    一名优秀的教师应具有现代化的教育思想.教学观念,掌握现代化的教学方法和教学手段,熟练运用信息工具对信息资源进行有效的收 集.组织.运用.下面是学习啦小编整理信息技术课学生评价表的范文,欢迎阅读! 信息 ...

  2. 读取服务器进程信息,服务器读取客户端进程

    服务器读取客户端进程 内容精选 换一换 用户更改dhcp租约时间后,只有在ECS实例获取到新的租约后配置才会生效.存量ECS实例不会立即刷新租约,而是要等到当前租约续租时才会更新租约.续租时间为当前租 ...

  3. linux查看注册表信息,linux下登录档及其查看方法

    一.Linux 常见的登录档档名 登录档可以帮助我们瞭解很多系统重要的事件,包括登入者的部分资讯,因此登录档的权限通常是设定为仅有 root 能够读取而已. 而由于登录档可以记载系统这麽多的详细资讯, ...

  4. python读取xml文件信息_python读取xml文件方法解析

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python来读取xml文件. 什么是xm ...

  5. hive表信息查询:查看表结构、表操作等--转

    原文地址:http://www.aboutyun.com/forum.PHP?mod=viewthread&tid=8590&highlight=Hive 问题导读: 1.如何查看hi ...

  6. jdbc获取mysql第二行表信息_【奇技淫巧】MySQL另类方法获取元数据信息

    问:在进行MySQL注入时,我们通常是通过information_schema元数据来获取表名.字段名信息,从而读取相应数据.但是如果waf或其它过滤了information_schema关键字,那么 ...

  7. hive表信息查询:查看表结构、表操作、建表语句

    问题导读: 1.如何查看hive表结构? 2.如何查看表结构信息? 3.如何查看分区信息? 4.哪个命令可以模糊搜索表 28.怎么查询创建表语句 1.hive模糊搜索表 show tables lik ...

  8. hive表信息查询:查看表结构、表操作等

    转自:http://www.aboutyun.com/forum.php?mod=viewthread&tid=8590&highlight=hive 问题导读: 1.如何查看hive ...

  9. hive表信息查询:查看表结构、表操作等(转)

    转自:http://www.aboutyun.com/forum.php?mod=viewthread&tid=8590&highlight=hive 问题导读: 1.如何查看hive ...

最新文章

  1. python collections模块(数据结构常用模块)计数器Counter 双向队列deque 默认字典defaultdict 有序字典OrderedDict 可命名元组namedtuple
  2. web笔记Error:That IP address
  3. vc2010中设置ipch与sdf的位置
  4. 强化学习中的调参经验与编程技巧(on policy篇)
  5. GP学习(十)-Mosaic raster datasets to a file raster format
  6. 2019 7.14学习笔记
  7. hand keypoint detection in single images using multiview bootstrapping
  8. Bailian2996 选课【置换】
  9. Photoshop 常用快捷键
  10. svn汉化依然失败无解
  11. macOS如何获取惠普打印机离线驱动安装包
  12. FlashCC导入使用greensock(TweenLite)包
  13. [莓技巧]黑莓7290输入短信时文字变为“?”的解决办法
  14. 通过QQ 2012 客户端协议获取clientkey的0x30数据包分析
  15. 网站启用CDN加速对百度SEO排名有什么影响?
  16. MindSpore21天实战营(1):基于MindSpore Lite开发目标检测的安卓APP实战
  17. Istio的Ingress与Egress网关
  18. 我学英文的方法---张五常
  19. 语音编码标准(G.711 G.723 G.726 G.729 iLBC)
  20. java笔试题分类集锦

热门文章

  1. 前端面试题(附上自己的回答)
  2. 一篇文章为你深度解析HTTPS 协议
  3. CF1080F Katya and Segments Sets
  4. 实现 通过数据库里一个字段值相等 则把 他合为一条数据
  5. SQL Server 关于列的权限控制
  6. 【BZOJ4262】Sum 单调栈+线段树
  7. property修饰关键字
  8. Linux驱动开发常用头文件
  9. python基础学习1-三元表达式和lambda表达式
  10. 使用PHP创建一个REST API(Create a REST API with PHP)