Win 95及NT的注冊數(shù)據(jù)庫(Registry) 是系統(tǒng)中非常重要的組成部分。在Win32 API中有一組Reg函數(shù)來處理這些問題。其一般的讀寫過程如下:
1、使用RegOpenKeyEx或RegCreateKeyEx函數(shù)打開或創(chuàng)建一個鍵;
2、如果上一步成功,使用RegQueryValueEx讀取子鍵的值,使用RegSetValueEx設(shè)置子鍵值,使用RegEnumKey獲得所有子鍵,使用RegDeleteKey刪除一個鍵;
3、完成操作后使用RegCloseKey關(guān)閉鍵。
下面這段程序打開HKEY_CURRENT_USER\Software\Zeal SoftStudio\AskPro FTP\LastTime鍵,然后讀取WOL子鍵的值。
HKEY hkey;
char sz[256];
DWORD dwtype, sl = 256;
RegOpenKeyEx(HKEY_CURRENT_USER,
"Software\\Zeal SoftStudio\\AskPro FTP\\LastTime",
NULL, KEY_ALL_ACCESS, &hkey);
RegQueryValueEx(hkey, "WOL", NULL, &dwtype, (LPBYTE)sz, &sl);
RegCloseKey(hkey);
MFC程序可以使用CRegKey類讀寫注冊表。VB中調(diào)用API的辦法可以參考QA000226 "如何訪問Windows系統(tǒng)注冊表"。
打開注冊鍵
LONG RegOpenKeyEx( HKEY hKey, // handle to open key
LPCTSTR lpSubKey, // address of name of subkey to open
DWORD ulOptions, // reserved =0
REGSAM samDesired, // security access mask
PHKEY phkResult // address of handle to open key
);
例:
HKEY hd;
hd=HKEY_LOCAL_MACHINE;
char* Regkeyname="SoftWare\\Xy123\\Poker\\";
LONG a=RegOpenKeyEx(hd,Regkeyname,0,KEY_READ, &hd); //成功返回ERROR_SUCCESS,否則返回錯誤代碼
關(guān)閉注冊鍵
LONG RegCloseKey( HKEY hKey // handle to key to close );
例:
RegCloseKey(HKEY_LOCAL_MACHINE);
OR: RegCloseKey(hd);
建立注冊鍵
LONG RegCreateKeyEx( HKEY hKey, // handle to an open key
LPCTSTR lpSubKey, // address of subkey name
DWORD Reserved, // reserved =0
LPTSTR lpClass, // address of class string
DWORD dwOptions, // special options flag
REGSAM samDesired, // desired security access
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // address of key security structure
PHKEY phkResult, // address of buffer for opened handle
LPDWORD lpdwDisposition // address of disposition value buffer );
例:
char *sclass=""; //類名指定為空
DWORD nbf=0; //接受返回值,指明是建立新鍵還是打開已有的鍵.(經(jīng)試驗總是返回REG_OPENED_EXISTING_KEY.
LONG II=RegCreateKeyEx(hd,Regkeyname,0,sclass,REG_OPTION_NON_VOLATILE,
KEY_READ|KEY_WRITE,NULL,&hd,&nbf);
//REG_OPTION_NON_VOLATILE 指明鍵永久保留.安全結(jié)構(gòu)指明NULL,自動獲得一默認(rèn)值
//成功返回ERROR_SUCCESS,否則返回錯誤代碼
枚舉鍵值
LONG RegEnumValue( HKEY hKey, // handle to key to query
DWORD dwIndex, // index of value to query
LPTSTR lpValueName, // address of buffer for value string
LPDWORD lpcbValueName, // address for size of value buffer
LPDWORD lpReserved, // reserved =NULL
LPDWORD lpType, // address of buffer for type code
LPBYTE lpData, // address of buffer for value data
LPDWORD lpcbData // address for size of data buffer);
例:
DWORD dinx=0;
char valuename[70]; //分配數(shù)值名稱緩沖區(qū)
strcpy(valuename,"DeskPattern"); //隨便指定哪個鍵值名
DWORD nsize=69; //數(shù)值名稱緩沖區(qū)大小
DWORD k=REG_SZ; //指明數(shù)據(jù)類型
unsigned char vari[70]; //分配數(shù)值緩沖區(qū)
DWORD ncbvari=69; //數(shù)值緩沖區(qū)大小
dinx=0; //從0開始
while((II=RegEnumValue(hd,dinx,valuename,&nsize,NULL,&k,vari,&ncbvari))
!= ERROR_NO_MORE_ITEMS)
{
dinx++;//索引 +1,準(zhǔn)備取下一個值
nsize=69; //恢復(fù)原來大小
ncbvari=69;
}
成功后返回值0,各變量返回后設(shè)置如下:
valuename=數(shù)值名稱,以0結(jié)尾;如 : DeskColor
nsize=數(shù)值名稱長度, 9
k=REG_SZ DeskColor 的類型為 REG_SZ
vari=鍵值,32768 DeskColor="32768",
ncbvari=鍵值長度 REG_SZ包括結(jié)尾0,=6,
讀取鍵值
LONG RegQueryValueEx( HKEY hKey, // handle to key to query
LPTSTR lpValueName, // address of name of value to query
LPDWORD lpReserved, // reserved
LPDWORD lpType, // address of buffer for value type
LPBYTE lpData, // address of data buffer
LPDWORD lpcbData // address of data buffer size );
例:
RegQueryValueEx(hd,valuename,NULL,&k,vari,&ncbvari);
變量定義及成功后各變量設(shè)置值同RegEnumValueEx
寫鍵值
LONG RegSetValueEx( HKEY hKey, // handle to key to set value for
LPCTSTR lpValueName, // name of the value to set
DWORD Reserved, // reserved
DWORD dwType, // flag for value type
CONST BYTE *lpData, // address of value data
DWORD cbData // size of value data );
例:
strcpy(valuename,"Hello");
unsigned char vari[10];
DWORD k=REG_SZ;
strcpy((char*)vari,"1234567")
RegSetValueEx(hd,valuename,0,k,vari,7);
成功后在Poker下增加一個鍵值 Hello : REG_SZ : 1234567
寫整型變量:
int hi=8;
RegSetValueEx(pj,valuename,0,REG_BINARY,(unsigned char*)&hi,sizeof(int));
成功后在Poker下增加一個鍵值 Hello2 : REG_BINARY :08 00 00 00
void AddEventSource()
{
HKEY hk;
DWORD dwData;
UCHAR szBuf[80];
// Add your source name as a subkey under the Application
// key in the EventLog registry key.
if (RegCreateKey(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\
\\EventLog\\Application\\SamplApp", &hk))
ErrorExit("Could not create the registry key.");
// Set the name of the message file.
strcpy(szBuf, "%SystemRoot%\\System\\SamplApp.dll");
// Add the name to the EventMessageFile subkey.
if (RegSetValueEx(hk, // subkey handle
"EventMessageFile", // value name
0, // must be zero
REG_EXPAND_SZ, // value type
(LPBYTE) szBuf, // pointer to value data
strlen(szBuf) + 1)) // length of value data
ErrorExit("Could not set the event message file.");
// Set the supported event types in the TypesSupported subkey.
dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
EVENTLOG_INFORMATION_TYPE;
if (RegSetValueEx(hk, // subkey handle
"TypesSupported", // value name
0, // must be zero
REG_DWORD, // value type
(LPBYTE) &dwData, // pointer to value data
sizeof(DWORD))) // length of value data
ErrorExit("Could not set the supported types.");
RegCloseKey(hk);
}
以下代碼把注冊表自啟動shell的鍵值改寫為C:\DK1\ATM\HARP\ExAtmShell.exe:
HKEY hkey;
LONG res;
DWORD datatype=REG_SZ;
unsigned char szvalue[_MAX_PATH];
strcpy((char*)szvalue,"C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");
res =::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\", 0,
KEY_WRITE|KEY_READ, &hkey);
if(res!=ERROR_SUCCESS)
{
AfxMessageBox("aaa");
return;
}
res = ::RegSetValueEx(hkey, "Shell", 0, datatype, szvalue, strlen(LPCSTR(szvalue)));
RegCloseKey(hkey);
if(res==ERROR_SUCCESS)
::AfxMessageBox("你已經(jīng)成功地將注冊表自啟動shell的鍵值設(shè)置為C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");
else
::AfxMessageBox("設(shè)定失敗:目標(biāo)位置不存在這樣的鍵!");