您的位置:首页 > 其它

MFC操作注册表

2012-07-16 14:34 323 查看
打开注册表键

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类型的指针。以后,读写,关闭用这个指针
);


如:

// 打开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;
}


读取注册表

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。缓冲区大小
);


如:

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


写注册表

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
);


如:

// 写注册表。修改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;
}


创建一个新键

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
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: