说明RegCreateKey创建一个KEY,并返回相应的HKEYRegOpenKey打开注册表,得到一个HKEY,用来作为下面这些函数的第一个参数。RegOpenKeyEx同RegOpenKey类似,一般很少用,增加了一个访问控制类型参数。RegSetValue设置一个HKEY的默认值RegSetValueEx设置一个HKEY除默认值以外其它的值RegQueryValue获取一个HKEY的默认值RegQueryValueEx获取一个HKEY除默认值以外其它的值RegDeleteKey删除一个KEY,此KEY不能包含子KEYSHDeleteKey删除一个KEY以及所有子KEYRegDeleteValue删除KEY里面的值RegCloseKey关闭注册表
说明REG_DWORD32位数字REG_SZ以NULL结尾的字符串,它可以为Unicode或ANSI字符串,取决于是否使用的是Unicode还是ANSI函数。
- 函数用法
- RegCreateKey
- LONG RegCreateKey(
- HKEY hKey, // handle to an open key
- LPCTSTR lpSubKey, // subkey name
- PHKEY phkResult // buffer for key handle
- );
- 假如我们要将demo程序的许多相机参数保存到:HKEY_LOCAL_MACHINE/SOFTWARE/daheng_directx,使用这个函数来创建指定的key,得到对于的HKEY以便进一步操作。HKEY hKey;
- if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
- }
- RegCloseKey(hKey);
- 注意:一般程序经常保持数据的位置有:HKEY_LOCAL_MACHINE/SOFTWARE和HKEY_CURRENT_USER/Software,两者的区别为:前者保持的数据,操作系统上的所有账户都可以访问(比如你的机器上有两个账户,一个是徐艺波,一个是康康,假如你将注册表保存在HKEY_LOCAL_MACHINE/SOFTWARE,那么当系统以徐艺波的账户登录加入后,运行demo和进入康康运行demo,获取的初始值都是一样的。),而HKEY_CURRENT_USER/Softwar是针对当前账户的,系统以不同的账户登录,这个KEY下面的值是不一样的。
- RegOpenKey
- LONG RegOpenKey(
- HKEY hKey, // handle to open key
- LPCTSTR lpSubKey, // name of subkey to open
- PHKEY phkResult // handle to open key
- );
- 这个函数不同于RegCreateKey的地方在于,如果这个KEY不存在,那么此函数执行失败(而RegCreateKey:存在的话,返回存在的HKEY;不存在,创建一个并返回其HKEY)。 假如我们要将demo程序的许多相机参数保存到:HKEY_LOCAL_MACHINE/SOFTWARE/daheng_directx,使用这个函数来打开指定的key,得到对于的HKEY以便进一步操作。HKEY hKey;
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
- }
- RegCloseKey(hKey);
- RegSetValueEx
- LONG RegSetValueEx(
- HKEY hKey, // handle to key
- LPCTSTR lpValueName, // value name
- DWORD Reserved, // reserved
- DWORD dwType, // value type
- CONST BYTE *lpData, // value data
- DWORD cbData // size of value data
- );
- 假设我们要保持相机曝光数据到HKEY_LOCAL_MACHINE/SOFTWARE/daheng_directx,数据名为AEC,值为1:HKEY hKey;
- HKEY hSubKey;
- DWORD dwValue = 1;
- char Buffer[] = "raw2rgb.dll";
-
- // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
- if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- //
- // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
- //
- if (RegSetValueEx(hKey, "AEC", 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS) {
- printf("RegSetValueEx: AEC = %d/n", dwValue);
- }
- //
- // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
- // 重新获取这个结点的HKEY。
- //
-
- if (RegCreateKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
- if (RegSetValueEx(hSubKey, "颜色校正插件", 0, REG_SZ, (CONST BYTE*)Buffer,strlen(Buffer) + 1) == ERROR_SUCCESS) {
- printf("RegSetValueEx: 颜色校正插件 = %s/n", Buffer);
- }
- RegCloseKey(hSubKey);
- }
- }
- RegCloseKey(hKey);
- RegQueryValueEx
- LONG RegQueryValueEx(
- HKEY hKey, // handle to key
- LPCTSTR lpValueName, // value name
- LPDWORD lpReserved, // reserved
- LPDWORD lpType, // type buffer
- LPBYTE lpData, // data buffer
- LPDWORD lpcbData // size of data buffer
- );
- 假设我们要读取上面设置RegSetValueEx设置的值:HKEY hKey;
- HKEY hSubKey;
- DWORD dwType;
- DWORD dwValue;
- DWORD dwSize;
- // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- //
- // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
- //
- dwType = REG_DWORD;
- dwSize = sizeof(DWORD);
- if (RegQueryValueEx(hKey, "AEC", 0, &dwType, &dwValue, &dwSize) == ERROR_SUCCESS) {
- printf("RegQueryValueEx AEC = %d/n", dwValue);
- } else {
- printf("Some error occurred!/n");
- }
- //
- // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
- // 重新获取这个结点的HKEY。
- //
- if (RegOpenKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
- char Buffer[256];
- dwType = REG_SZ;
- dwSize = sizeof(Buffer);
- if (RegQueryValueEx(hSubKey, "颜色校正插件", 0, &dwType, (LPBYTE)Buffer, &dwSize) == ERROR_SUCCESS) {
- printf("RegQueryValueEx 颜色校正插件 = %s/n", Buffer);
- } else {
- printf("Some error occurred!/n");
- }
- RegCloseKey(hSubKey);
- }
- }
- RegCloseKey(hKey);
- RegDeleteKey
- LONG RegDeleteKey(
- HKEY hKey, // handle to open key
- LPCTSTR lpSubKey // subkey name
- );
- 假设我们要删除RegSetValueEx设置的KEY:RegDeleteKey (HKEY_LOCAL_MACHINE, "Software//daheng_directx");
- SHDeleteKey
- LONG SHDeleteKey(
- HKEY hKey, // handle to open key
- LPCTSTR lpSubKey // subkey name
- );
- 假设我们要删除RegSetValueEx设置的KEY以及所有子KEY:SHDeleteKey (HKEY_LOCAL_MACHINE, "Software//daheng_directx");
- RegDeleteValue
- LONG RegDeleteValue(
- HKEY hKey, // handle to key
- LPCTSTR lpValueName // value name
- );
- 假设我们要删除上面设置RegSetValueEx设置的值:HKEY hKey;
- HKEY hSubKey;
- DWORD dwType;
- DWORD dwValue;
- DWORD dwSize;
- // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- dwType = REG_DWORD;
- dwSize = sizeof(DWORD);
- if (RegDeleteValue(hKey, "AEC") == ERROR_SUCCESS) {
- printf("RegDeleteValue AEC = %d/n", dwValue);
- } else {
- printf("Some error occurred!/n");
- }
- }
- RegCloseKey(hKey);
- RegCloseKey
- LONG RegCloseKey(
- HKEY hKey // handle to key to close
- );
- HKEY hKey;
- if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- // …
- }
- RegCloseKey(hKey);
- 这个函数比较简单,参数1为RegCreateKey、RegOpenKey、RegCreateKeyEx、RegOpenKeyEx函数返回的HKEY。
- 实例
- /*++
- Copyright (c) 2007 http://www.xuyibo.org
- Module Name:
- reg.c
- Abstract:
- Small registry demo for my good friend LiuMin ;)
- Author:
- xuyibo (xuyibo) 2007-05-15
- Revision History:
- --*/
- #include
- #include
- #pragma comment(lib, "advapi32.lib")
- void SetRegistryValue()
- {
- HKEY hKey;
- HKEY hSubKey;
- DWORD dwValue = 1;
- char Buffer[] = "raw2rgb.dll";
-
- // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
- if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- //
- // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
- //
- if (RegSetValueEx(hKey, "AEC", 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS) {
- printf("RegSetValueEx: AEC = %d/n", dwValue);
- }
- //
- // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
- // 重新获取这个结点的HKEY。
- //
-
- if (RegCreateKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
- if (RegSetValueEx(hSubKey, "颜色校正插件", 0, REG_SZ, (CONST BYTE*)Buffer,strlen(Buffer) + 1) == ERROR_SUCCESS) {
- printf("RegSetValueEx: 颜色校正插件 = %s/n", Buffer);
- }
- RegCloseKey(hSubKey);
- }
- }
- RegCloseKey(hKey);
- }
- void GetRegistryValue()
- {
- HKEY hKey;
- HKEY hSubKey;
- DWORD dwType;
- DWORD dwValue;
- DWORD dwSize;
- // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
- //
- // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
- //
- dwType = REG_DWORD;
- dwSize = sizeof(DWORD);
- if (RegQueryValueEx(hKey, "AEC", 0, &dwType, &dwValue, &dwSize) == ERROR_SUCCESS) {
- printf("RegQueryValueEx AEC = %d/n", dwValue);
- } else {
- printf("Some error occurred!/n");
- }
- //
- // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
- // 重新获取这个结点的HKEY。
- //
- if (RegOpenKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
- char Buffer[256];
- dwType = REG_SZ;
- dwSize = sizeof(Buffer);
- if (RegQueryValueEx(hSubKey, "颜色校正插件", 0, &dwType, (LPBYTE)Buffer, &dwSize) == ERROR_SUCCESS) {
- printf("RegQueryValueEx 颜色校正插件 = %s/n", Buffer);
- } else {
- printf("Some error occurred!/n");
- }
- RegCloseKey(hSubKey);
- }
- }
- RegCloseKey(hKey);
- }
- int main(int argc, char* argv[])
- {
- SetRegistryValue();
- GetRegistryValue();
-
- getchar();
- return 0;
- }
复制代码
运行结果:
|