---
title: "注册表操作类 vc++"
author: "eteran"
date: "2008-04-18T17:12:00Z"
tags: []
slug: "vc"
draft: false
lastmod: "2008-04-18T17:12:00Z"
id: 771909
url: "https://eteran.iteye.com/blog/771909"
---

这个其实发在这里有点不合适，因为在.NET环境下本身很多数据都采用xml格式化，存储在本地，另一方面是.net的类库也非常的灵活，对注册表操作非常方便，没有必要画蛇添足来这么一个包装的类，但是如果你用 **vc++**, **WIN32 API**,做开发的时候，你会发现要对注册表操作非常的困难，更让人寒心的是，它居然没有提供对注册表键的重命名，在NTDLL.dll 中有一个可以替代的API：NTRenameKey, 但是它只有winXP之后的操作系统才提供，所以，笔者在这里提供这样一个操作类，主要是弥补了**WIN32 API** 在这方面的不足。如果读者有兴趣的话可以去看[**Dan Madden**](http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=57) 写的一篇文章：[http://www.codeproject.com/KB/system/NtRegistry.aspx](http://www.codeproject.com/KB/system/NtRegistry.aspx "http://www.codeproject.com/KB/system/NtRegistry.aspx") 但是他的项目必须要用到MFC的类库，感觉不爽。

废话到此为止。  
**主要方法：**

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)bool ExistKey(HKEY hKey, const LPTSTR subKey) const; //是否存在这样一个键  
  bool KeyRename(HKEY hKey, LPTSTR lpRootKey, LPTSTR lpOldName, LPTSTR lpNewName); //hKey 为HKEY\_* 之一， 把lpRootKey下面的lpOldName名字改成 lpNewName， 比如要把SOFTWARE\\\\\*\\\\subkey\\\\TEST改成 SOFTWARE\\\\\*\\\\subkey\\\\renamed, 参数设置应该如下： lpRootKey = SOFTWARE\\\\\*\\\\subkey， lpOldName =TEST，lpNewName=renamed  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)bool CopyKeyValues(HKEY hKey, const LPTSTR lpSourceKey, const LPTSTR lpDestKey) const; //把lpSourceKey 键的所有值，复制给lpDestKey  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)bool DeleteKey(HKEY hKey, const LPTSTR lpSubKey) const;// 删除lpSubKey下所有的键，子键，值  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)bool HasSubKey(HKEY hKey, const LPTSTR lpSubKey) const; //是否有子键

**代码下载：**

[我是下载链接](http://www.cnblogs.com/Files/eteran/EditRegistry.zip "我是下载链接")

所有代码在vc6.0 windows XP下测试通过。