MFC操作注册表

打开注册表键

 

1 LONG RegOpenKeyEx(
2   HKEY hKey,         // handle to open key主键
3   LPCTSTR lpSubKey,  // subkey name子键
4   DWORD ulOptions,   // reserved。必须是0
5   REGSAM samDesired, // security access mask读写标识
6   PHKEY phkResult    // handle to open key返回的HKEY类型的指针。以后,读写,关闭用这个指针
7 );

 

 

如:

1 // 打开HKEY_LOCAL_MACHINE主键下的SoftWare\\Knight Studio\\Knight子键
2  HKEY hKEY;
3  HKEY  hKeyRoot = HKEY_LOCAL_MACHINE;
4  long ret0=(::RegOpenKeyEx(hKeyRoot,"SoftWare\\Knight Studio\\Knight",0,KEY_READ,&hKEY));
5  if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
6  {
7   AfxMessageBox("错误:无法打开有关的hKEY");
8   return;
9  }

 

读取注册表

1 LONG RegQueryValueEx(
2   HKEY hKey,            // handle to key打开注册表指针
3   LPCTSTR lpValueName,  // value name要读取的键名称
4   LPDWORD lpReserved,   // reserved  must be NULL. 必须是NULL
5   LPDWORD lpType,       // type buffer,键类型。我最常用REG_SZ,REG_DWORD
6   LPBYTE lpData,        // data buffer。保存查询结果的缓冲区
7   LPDWORD lpcbData      // size of data buffer。缓冲区大小
8 );

 

如:

 1 // hKEY是上面打开时得到的指针。
 2  LPBYTE getValue = new BYTE[80];//得到的键值
 3  DWORD keyType = REG_SZ;//定义数据类型
 4  DWORD DataLen = 80;//定义数据长度
 5  CString strUser = _T("UserName");//要查询的键名称
 6  long ret1=::RegQueryValueEx(hKEY,strUser,NULL,&keyType,getValue,&DataLen);
 7  if(ret1!=ERROR_SUCCESS)
 8  {
 9   AfxMessageBox("错误:无法查询有关的注册表信息");
10   return;
11  }

 

写注册表

1 LONG RegSetValueEx(
2   HKEY hKey,           // handle to key。打开注册表的指针
3   LPCTSTR lpValueName, // value name 要写入的键
4   DWORD Reserved,      // reserved  必须是0
5   DWORD dwType,        // value type 写入值类型
6   CONST BYTE *lpData,  // value data 要写入的数据
7   DWORD cbData         // size of value data 。数据SIZE
8 );

 

如:

 1 // 写注册表。修改UserName为Knight
 2 // 写入CString类型的数据
 3 CString strUser = _T("UserName");//要写入的键名称
 4 LPCTSTR strUserValue = "Knight";
 5 long ret = ::RegSetValueEx(hKEY, strUser, 0, REG_SZ, (const BYTE *) strUserValue, strlen(strUserValue)+1);
 6 if(ret1!=ERROR_SUCCESS)
 7 {
 8  AfxMessageBox("错误:无法查询有关的注册表信息");
 9  return;
10 }

 

创建一个新键

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

 

删除一个键

1 LONG RegDeleteKey(
2   HKEY hKey,         // handle to open key
3   LPCTSTR lpSubKey   // subkey name
4 );

 

删除一个键值

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

 

刷新注册表

1 LONG RegFlushKey(
2   HKEY hKey   // handle to key to write。写入所有的值,在给定的指针
3 );

 

导入一个注册表文件到指定的键下

1 LONG RegLoadKey(
2   HKEY hKey,        // handle to open key
3   LPCTSTR lpSubKey, // subkey name
4   LPCTSTR lpFile    // registry file name
5 );

 

关闭打开的注册表

1 LONG RegCloseKey(
2   HKEY hKey   // handle to key to close
3 );

 

 

posted @ 2012-07-16 14:34  ifeixiang  阅读(11760)  评论(0编辑  收藏  举报