MFC操作注册表

来源:互联网 发布:下载软件下载 编辑:程序博客网 时间:2024/05/17 00:06

转自:http://www.cnblogs.com/trying/archive/2012/07/16/2863763.html

打开注册表键

1 LONG RegOpenKeyEx(2   HKEY hKey,         // handle to open key主键3   LPCTSTR lpSubKey,  // subkey name子键4   DWORD ulOptions,   // reserved。必须是05   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. 必须是NULL5   LPDWORD lpType,       // type buffer,键类型。我最常用REG_SZ,REG_DWORD6   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  必须是05   DWORD dwType,        // value type 写入值类型6   CONST BYTE *lpData,  // value data 要写入的数据7   DWORD cbData         // size of value data 。数据SIZE8 );
如:

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_RESTORE10   REGSAM samDesired,                          // desired security access。操作权限。一般KEY_ALL_ACCESS,除非有特殊需要,请查阅MSDN11   LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance。继承性。一般为NULL12   PHKEY phkResult,                            // key handle 。返回该键值镇。13   LPDWORD lpdwDisposition                     // disposition value buffer14              //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 key3   LPCTSTR lpSubKey   // subkey name4 );
删除一个键值

1 LONG RegDeleteValue(2   HKEY hKey,            // handle to key3   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 key3   LPCTSTR lpSubKey, // subkey name4   LPCTSTR lpFile    // registry file name5 );

关闭打开的注册表

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

0 0
原创粉丝点击