1. 打開注冊表鍵
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\\Cleaner\\Cleaner子鍵
HKEY hKEY;
HKEY hKeyRoot = HKEY_LOCAL_MACHINE;
long ret0=(::RegOpenKeyEx(hKeyRoot,"SoftWare\\Cleaner\\Cleaner",0,KEY_READ,&hKEY));
if(ret0!=ERROR_SUCCESS)//如果無法打開hKEY,則中止程序的執行
{
AfxMessageBox("錯誤:無法打開有關的hKEY");
return;
}
2. 讀取注冊表
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("Version");//要查詢的鍵名稱
long ret1=::RegQueryValueEx(hKEY,strUser,NULL,&keyType,getValue,&DataLen);
if(ret1!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
return;
}
3. 寫注冊表
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
);
如:
// 寫注冊表。修改Version為1.0.12
// 寫入CString類型的數據
CString strVersion = _T("Version");//要寫入的鍵名稱
LPCTSTR strVersionValue = "1.0.12";
long ret = ::RegSetValueEx(hKEY, strVersion, 0, REG_SZ, (const BYTE *) strVersionValue, strlen(strVersionValue)+1);
if(ret!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
return;
}
[/code]
4. 創建一個新鍵
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.
);
5. 刪除一個鍵
LONG RegDeleteKey(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey // subkey name
);
6. 刪除一個鍵值
LONG RegDeleteValue(
HKEY hKey, // handle to key
LPCTSTR lpValueName // value name。值名稱,不是打開的那個指針,是查詢到的指針,如果為空RegSetValueEx創建的值將被刪除
);
7. 刷新注冊表
LONG RegFlushKey(
HKEY hKey // handle to key to write。寫入所有的值,在給定的指針
);
//這個函數是將改變數據直接寫到硬盤上,不要頻繁使用,會影響性能
8. 導入一個注冊表文件到指定的鍵下
LONG RegLoadKey(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey, // subkey name
LPCTSTR lpFile // registry file name
);
//沒有用
9. 關閉打開的注冊表
LONG RegCloseKey(
HKEY hKey // handle to key to close
);
轉載:
http://blog.csdn.net/huangzhtao/archive/2008/08/04/2763638.aspx