九:[VC++]MFC操作注册表

[VC++]MFC操作注册表

 

MFC操作注册表

一:打开注册表键
LONG RegOpenKeyEx(
  HKEY hKey,         // handle to open key主键
  LPCTSTR lpSubKey,  // subkey name子键
  DWORD ulOptions,   // reserved。必须是0
  REGSAM samDesired, // security access mask读写标识
  PHKEY phkResult    // handle to open key返回的HKEY类型的指针。以后,读写,关闭用这个指针
);

如:
[code]
// 打开HKEY_LOCAL_MACHINE主键下的SoftWare//Knight Studio//Knight子键
 HKEY hKEY;
 HKEY  hKeyRoot = HKEY_LOCAL_MACHINE;
 long ret0=(::RegOpenKeyEx(hKeyRoot,"SoftWare//Knight Studio//Knight",0,KEY_READ,&hKEY));
 if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
 {
  AfxMessageBox("错误:无法打开有关的hKEY");
  return;
 }
[/code]

二:读取注册表
LONG RegQueryValueEx(
  HKEY hKey,            // handle to key打开注册表指针
  LPCTSTR lpValueName,  // value name要读取的键名称
  LPDWORD lpReserved,   // reserved  must be NULL. 必须是NULL
  LPDWORD lpType,       // type buffer,键类型。我最常用REG_SZ,REG_DWORD
  LPBYTE lpData,        // data buffer。保存查询结果的缓冲区
  LPDWORD lpcbData      // size of data buffer。缓冲区大小
);
如:
[code]
// hKEY是上面打开时得到的指针。
 LPBYTE getValue = new BYTE[80];//得到的键值
 DWORD keyType = REG_SZ;//定义数据类型
 DWORD DataLen = 80;//定义数据长度
 CString strUser = _T("UserName");//要查询的键名称
 long ret1=::RegQueryValueEx(hKEY,strUser,NULL,&keyType,getValue,&DataLen);
 if(ret1!=ERROR_SUCCESS)
 {
  AfxMessageBox("错误:无法查询有关的注册表信息");
  return;
 }
[/code]

三:写注册表
LONG RegSetValueEx(
  HKEY hKey,           // handle to key。打开注册表的指针
  LPCTSTR lpValueName, // value name 要写入的键
  DWORD Reserved,      // reserved  必须是0
  DWORD dwType,        // value type 写入值类型
  CONST BYTE *lpData,  // value data 要写入的数据
  DWORD cbData         // size of value data 。数据SIZE
);
如:
[code]
// 写注册表。修改UserName为Knight
// 写入CString类型的数据
CString strUser = _T("UserName");//要写入的键名称
LPCTSTR strUserValue = "Knight";
long ret = ::RegSetValueEx(hKEY, strUser, 0, REG_SZ, (const BYTE *) strUserValue, strlen(strUserValue)+1);
if(ret1!=ERROR_SUCCESS)
{
 AfxMessageBox("错误:无法查询有关的注册表信息");
 return;
}
[/code]

四:创建一个新键
LONG RegCreateKeyEx(
  HKEY hKey,                                  // handle to open key。打开的注册表指针
  LPCTSTR lpSubKey,                           // subkey name。子键名称
  DWORD Reserved,                             // reserved。必须为0
  LPTSTR lpClass,                             // class string。已经存在时用,一般为NULL
  DWORD dwOptions,                            // special options
               //默认值REG_OPTION_VOLATILE,保存在注册表,下次开机仍然存在
               //REG_OPTION_VOLATILE,保存在内存
               //REG_OPTION_BACKUP_RESTORE
  REGSAM samDesired,                          // desired security access。操作权限。一般KEY_ALL_ACCESS,除非有特殊需要,请查阅MSDN
  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance。继承性。一般为NULL
  PHKEY phkResult,                            // key handle 。返回该键值镇。
  LPDWORD lpdwDisposition                     // disposition value buffer
             //REG_CREATED_NEW_KEY The key did not exist and was created.
           //REG_OPENED_EXISTING_KEY The key existed and was simply opened without being changed.

);

五:删除一个键
LONG RegDeleteKey(
  HKEY hKey,         // handle to open key
  LPCTSTR lpSubKey   // subkey name
);

六:删除一个键值
LONG RegDeleteValue(
  HKEY hKey,            // handle to key
  LPCTSTR lpValueName   // value name。值名称,不是打开的那个指针,是查询到的指针,如果为空RegSetValueEx创建的值将被删除
);

七:刷新注册表
LONG RegFlushKey(
  HKEY hKey   // handle to key to write。写入所有的值,在给定的指针
);

八:导入一个注册表文件
到指定的键下
LONG RegLoadKey(
  HKEY hKey,        // handle to open key
  LPCTSTR lpSubKey, // subkey name
  LPCTSTR lpFile    // registry file name
);

九:关闭打开的注册表
LONG RegCloseKey(
  HKEY hKey   // handle to key to close
);

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值