術語對照
- 註冊表操作常用API
API 說明 RegCreateKey 創建一個KEY,並返回相應的HKEY RegOpenKey 打開註冊表,得到一個HKEY,用來作爲下面這些函數的第一個參數。 RegOpenKeyEx 同RegOpenKey類似,一般很少用,增加了一個訪問控制類型參數。 RegSetValue 設置一個HKEY的默認值 RegSetValueEx 設置一個HKEY除默認值以外其它的值 RegQueryValue 獲取一個HKEY的默認值 RegQueryValueEx 獲取一個HKEY除默認值以外其它的值 RegDeleteKey 刪除一個KEY,此KEY不能包含子KEY SHDeleteKey 刪除一個KEY以及所有子KEY RegDeleteValue 刪除KEY裏面的值 RegCloseKey 關閉註冊表 - 註冊表數據類型
類型 說明 REG_DWORD 32位數字 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。
- RegCreateKey
- 實例
/*++ 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 <stdio.h> #include <windows.h> #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; }
運行結果: