1. 打開注冊表鍵
LONG RegOpenKeyEx(
HKEY hKey, // handle to open key主鍵
LPCTSTR lpSubKey, // subkey name子鍵
DWORD ulOptions, // reserved。必須是0
REGSAM samDesired, // security access mask讀寫標(biāo)識
PHKEY phkResult // handle to open key返回的HKEY類型的指針。以后,讀寫,關(guān)閉用這個指針
);
如:
// 打開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,則中止程序的執(zhí)行
{
AfxMessageBox("錯誤:無法打開有關(guān)的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。保存查詢結(jié)果的緩沖區(qū)
LPDWORD lpcbData // size of data buffer。緩沖區(qū)大小
);
如:
// hKEY是上面打開時得到的指針。
LPBYTE getValue = new BYTE[80];//得到的鍵值
DWORD keyType = REG_SZ;//定義數(shù)據(jù)類型
DWORD DataLen = 80;//定義數(shù)據(jù)長度
CString strUser = _T("Version");//要查詢的鍵名稱
long ret1=::RegQueryValueEx(hKEY,strUser,NULL,&keyType,getValue,&DataLen);
if(ret1!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法查詢有關(guān)的注冊表信息");
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 要寫入的數(shù)據(jù)
DWORD cbData // size of value data 。數(shù)據(jù)SIZE
);
如:
// 寫注冊表。修改Version為1.0.12
// 寫入CString類型的數(shù)據(jù)
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("錯誤:無法查詢有關(guān)的注冊表信息");
return;
}
[/code]
4. 創(chuàng)建一個新鍵
LONG RegCreateKeyEx(
HKEY hKey, // handle to open key。打開的注冊表指針
LPCTSTR lpSubKey, // subkey name。子鍵名稱
DWORD Reserved, // reserved。必須為0
LPTSTR lpClass, // class string。已經(jīng)存在時用,一般為NULL
DWORD dwOptions, // special options
//默認(rèn)值REG_OPTION_VOLATILE,保存在注冊表,下次開機仍然存在
//REG_OPTION_VOLATILE,保存在內(nèi)存
//REG_OPTION_BACKUP_RESTORE
REGSAM samDesired, // desired security access。操作權(quán)限。一般KEY_ALL_ACCESS,除非有特殊需要,請查閱MSDN
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance。繼承性。一般為NULL
PHKEY phkResult, // key handle 。返回該鍵值鎮(zhèn)。
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創(chuàng)建的值將被刪除
);
7. 刷新注冊表
LONG RegFlushKey(
HKEY hKey // handle to key to write。寫入所有的值,在給定的指針
);
//這個函數(shù)是將改變數(shù)據(jù)直接寫到硬盤上,不要頻繁使用,會影響性能
8. 導(dǎo)入一個注冊表文件到指定的鍵下
LONG RegLoadKey(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey, // subkey name
LPCTSTR lpFile // registry file name
);
//沒有用
9. 關(guān)閉打開的注冊表
LONG RegCloseKey(
HKEY hKey // handle to key to close
);
轉(zhuǎn)載:
http://blog.csdn.net/huangzhtao/archive/2008/08/04/2763638.aspx