轉載:http://www.xuyibo.org/article/71.htm
- 術語對照
- 註冊表操作常用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。
- 執行個體
/*++Copyright (c) 2007 http://www.xuyibo.orgModule Name: reg.cAbstract: Small registry demo for my good friend LiuMin ;)Author: xuyibo (xuyibo) 2007-05-15Revision 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;}
運行結果:
- 相關文章
程式異常捕獲庫 - CrashRpt
MAPI MAPISendMail
輕量級的瀏覽器控制項HTMLLITE
在XP下使用Link控制項
我的電腦軟配置
讓你的程式支援脫拽
背景透明的按鈕