注册表API简易教程

注册表API简易教程

  • 术语对照
  • 注册表操作常用API
  • API
说明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函数。
  1. 函数用法
  2. RegCreateKey
  3. LONG RegCreateKey(
  4.   HKEY hKey,        // handle to an open key
  5.   LPCTSTR lpSubKey, // subkey name
  6.   PHKEY phkResult   // buffer for key handle
  7. );
  8. 假如我们要将demo程序的许多相机参数保存到:HKEY_LOCAL_MACHINE/SOFTWARE/daheng_directx,使用这个函数来创建指定的key,得到对于的HKEY以便进一步操作。HKEY hKey;
  9. if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  10. // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
  11. }
  12. RegCloseKey(hKey);
  13. 注意:一般程序经常保持数据的位置有:HKEY_LOCAL_MACHINE/SOFTWARE和HKEY_CURRENT_USER/Software,两者的区别为:前者保持的数据,操作系统上的所有账户都可以访问(比如你的机器上有两个账户,一个是徐艺波,一个是康康,假如你将注册表保存在HKEY_LOCAL_MACHINE/SOFTWARE,那么当系统以徐艺波的账户登录加入后,运行demo和进入康康运行demo,获取的初始值都是一样的。),而HKEY_CURRENT_USER/Softwar是针对当前账户的,系统以不同的账户登录,这个KEY下面的值是不一样的。
  14. RegOpenKey
  15. LONG RegOpenKey(
  16.   HKEY hKey,        // handle to open key
  17.   LPCTSTR lpSubKey, // name of subkey to open
  18.   PHKEY phkResult   // handle to open key
  19. );
  20. 这个函数不同于RegCreateKey的地方在于,如果这个KEY不存在,那么此函数执行失败(而RegCreateKey:存在的话,返回存在的HKEY;不存在,创建一个并返回其HKEY)。 假如我们要将demo程序的许多相机参数保存到:HKEY_LOCAL_MACHINE/SOFTWARE/daheng_directx,使用这个函数来打开指定的key,得到对于的HKEY以便进一步操作。HKEY hKey;
  21. if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  22. // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
  23. }
  24. RegCloseKey(hKey);
  25. RegSetValueEx
  26. LONG RegSetValueEx(
  27.   HKEY hKey,           // handle to key
  28.   LPCTSTR lpValueName, // value name
  29.   DWORD Reserved,      // reserved
  30.   DWORD dwType,        // value type
  31.   CONST BYTE *lpData,  // value data
  32.   DWORD cbData         // size of value data
  33. );
  34. 假设我们要保持相机曝光数据到HKEY_LOCAL_MACHINE/SOFTWARE/daheng_directx,数据名为AEC,值为1:HKEY hKey;
  35.     HKEY hSubKey;
  36.     DWORD dwValue = 1;
  37.     char Buffer[] = "raw2rgb.dll";
  38.    
  39.     // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
  40.     if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  41.         //
  42.         // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
  43.         //
  44.         if (RegSetValueEx(hKey, "AEC", 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS) {
  45.             printf("RegSetValueEx: AEC = %d/n", dwValue);
  46.         }
  47.         //
  48.         // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
  49.         // 重新获取这个结点的HKEY。
  50.         //
  51.         
  52.         if (RegCreateKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
  53.             if (RegSetValueEx(hSubKey, "颜色校正插件", 0, REG_SZ, (CONST BYTE*)Buffer,strlen(Buffer) + 1) == ERROR_SUCCESS) {
  54.                 printf("RegSetValueEx: 颜色校正插件 = %s/n", Buffer);
  55.             }
  56.             RegCloseKey(hSubKey);
  57.         }
  58.     }
  59.     RegCloseKey(hKey);
  60. RegQueryValueEx
  61. LONG RegQueryValueEx(
  62.   HKEY hKey,            // handle to key
  63.   LPCTSTR lpValueName,  // value name
  64.   LPDWORD lpReserved,   // reserved
  65.   LPDWORD lpType,       // type buffer
  66.   LPBYTE lpData,        // data buffer
  67.   LPDWORD lpcbData      // size of data buffer
  68. );
  69. 假设我们要读取上面设置RegSetValueEx设置的值:HKEY hKey;
  70.     HKEY hSubKey;
  71.     DWORD dwType;
  72.     DWORD dwValue;
  73.     DWORD dwSize;
  74.     // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
  75.     if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  76.         //
  77.         // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
  78.         //
  79.         dwType = REG_DWORD;
  80.         dwSize = sizeof(DWORD);
  81.         if (RegQueryValueEx(hKey, "AEC", 0, &dwType, &dwValue, &dwSize) == ERROR_SUCCESS) {
  82.             printf("RegQueryValueEx AEC = %d/n", dwValue);
  83.         } else {
  84.             printf("Some error occurred!/n");
  85.         }
  86.         //
  87.         // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
  88.         // 重新获取这个结点的HKEY。
  89.         //
  90.         if (RegOpenKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
  91.             char Buffer[256];
  92.             dwType = REG_SZ;
  93.             dwSize = sizeof(Buffer);
  94.             if (RegQueryValueEx(hSubKey, "颜色校正插件", 0, &dwType, (LPBYTE)Buffer, &dwSize) == ERROR_SUCCESS) {
  95.                     printf("RegQueryValueEx 颜色校正插件 = %s/n", Buffer);
  96.             } else {
  97.                     printf("Some error occurred!/n");
  98.             }
  99.             RegCloseKey(hSubKey);
  100.         }
  101.     }
  102.     RegCloseKey(hKey);
  103. RegDeleteKey
  104. LONG RegDeleteKey(
  105.   HKEY hKey,         // handle to open key
  106.   LPCTSTR lpSubKey   // subkey name
  107. );
  108. 假设我们要删除RegSetValueEx设置的KEY:RegDeleteKey (HKEY_LOCAL_MACHINE, "Software//daheng_directx");
  109. SHDeleteKey
  110. LONG SHDeleteKey(
  111.   HKEY hKey,         // handle to open key
  112.   LPCTSTR lpSubKey   // subkey name
  113. );
  114. 假设我们要删除RegSetValueEx设置的KEY以及所有子KEY:SHDeleteKey (HKEY_LOCAL_MACHINE, "Software//daheng_directx");
  115. RegDeleteValue
  116. LONG RegDeleteValue(
  117.   HKEY hKey,            // handle to key
  118.   LPCTSTR lpValueName   // value name
  119. );
  120. 假设我们要删除上面设置RegSetValueEx设置的值:HKEY hKey;
  121.     HKEY hSubKey;
  122.     DWORD dwType;
  123.     DWORD dwValue;
  124.     DWORD dwSize;
  125.     // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
  126.     if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  127.         dwType = REG_DWORD;
  128.         dwSize = sizeof(DWORD);
  129.         if (RegDeleteValue(hKey, "AEC") == ERROR_SUCCESS) {
  130.             printf("RegDeleteValue AEC = %d/n", dwValue);
  131.         } else {
  132.             printf("Some error occurred!/n");
  133.         }
  134.     }
  135.     RegCloseKey(hKey);
  136. RegCloseKey
  137. LONG RegCloseKey(
  138.   HKEY hKey   // handle to key to close
  139. );
  140. HKEY hKey;
  141. if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  142.       // …
  143. }
  144. RegCloseKey(hKey);
  145. 这个函数比较简单,参数1为RegCreateKey、RegOpenKey、RegCreateKeyEx、RegOpenKeyEx函数返回的HKEY。
  146. 实例
  147. /*++
  148. Copyright (c) 2007 http://www.xuyibo.org
  149. Module Name:
  150.     reg.c
  151. Abstract:
  152.     Small registry demo for my good friend LiuMin ;)
  153. Author:
  154.     xuyibo (xuyibo) 2007-05-15
  155. Revision History:
  156. --*/
  157. #include
  158. #include
  159. #pragma comment(lib, "advapi32.lib")
  160. void SetRegistryValue()
  161. {
  162.     HKEY hKey;
  163.     HKEY hSubKey;
  164.     DWORD dwValue = 1;
  165.     char Buffer[] = "raw2rgb.dll";
  166.    
  167.     // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
  168.     if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  169.         //
  170.         // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
  171.         //
  172.         if (RegSetValueEx(hKey, "AEC", 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS) {
  173.             printf("RegSetValueEx: AEC = %d/n", dwValue);
  174.         }
  175.         //
  176.         // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
  177.         // 重新获取这个结点的HKEY。
  178.         //
  179.         
  180.         if (RegCreateKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
  181.             if (RegSetValueEx(hSubKey, "颜色校正插件", 0, REG_SZ, (CONST BYTE*)Buffer,strlen(Buffer) + 1) == ERROR_SUCCESS) {
  182.                 printf("RegSetValueEx: 颜色校正插件 = %s/n", Buffer);
  183.             }
  184.             RegCloseKey(hSubKey);
  185.         }
  186.     }
  187.     RegCloseKey(hKey);
  188. }
  189. void GetRegistryValue()
  190. {
  191.     HKEY hKey;
  192.     HKEY hSubKey;
  193.     DWORD dwType;
  194.     DWORD dwValue;
  195.     DWORD dwSize;
  196.     // 使用RegCreateKey能保证如果Software/daheng_directx不存在的话,创建一个。
  197.     if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software//daheng_directx", &hKey) == ERROR_SUCCESS) {
  198.         //
  199.         // 在这里就可以使用hKey来操作daheng_directx这个KEY里面的值了。
  200.         //
  201.         dwType = REG_DWORD;
  202.         dwSize = sizeof(DWORD);
  203.         if (RegQueryValueEx(hKey, "AEC", 0, &dwType, &dwValue, &dwSize) == ERROR_SUCCESS) {
  204.             printf("RegQueryValueEx AEC = %d/n", dwValue);
  205.         } else {
  206.             printf("Some error occurred!/n");
  207.         }
  208.         //
  209.         // 如果想在Software//daheng_directx创建一个plugins key,那么就不能再使用hKey了,需要
  210.         // 重新获取这个结点的HKEY。
  211.         //
  212.         if (RegOpenKey(hKey, "plugins", &hSubKey) == ERROR_SUCCESS) {
  213.             char Buffer[256];
  214.             dwType = REG_SZ;
  215.             dwSize = sizeof(Buffer);
  216.             if (RegQueryValueEx(hSubKey, "颜色校正插件", 0, &dwType, (LPBYTE)Buffer, &dwSize) == ERROR_SUCCESS) {
  217.                     printf("RegQueryValueEx 颜色校正插件 = %s/n", Buffer);
  218.             } else {
  219.                     printf("Some error occurred!/n");
  220.             }
  221.             RegCloseKey(hSubKey);
  222.         }
  223.     }
  224.     RegCloseKey(hKey);
  225. }
  226. int main(int argc, char* argv[])
  227. {
  228.     SetRegistryValue();
  229.     GetRegistryValue();
  230.    
  231.     getchar();
  232.     return 0;
  233. }
复制代码
运行结果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值