Win 95及NT的注册数据库(Registry)是系统中非常重要的组成部分。在Win32 API中有一组Reg函数来处理这些问题。其一般的读写过程如下:
1、使用RegOpenKeyEx或RegCreateKeyEx函数打开或创建一个键;
2、如果上一步成功,使用RegQueryValueEx读取子键的值,使用RegSetValueEx设置子键值,使用RegEnumKey获得所有子键,使用RegDeleteKey删除一个键;
3、完成操作后使用RegCloseKey关闭键。
下面这段程序打开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中调用API的办法可以参考QA000226 "如何访问Windows系统注册表"。
打开注册键
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,否则返回错误代码
关闭注册键
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; //接受返回值,指明是建立新键还是打开已有的键.(经试验总是返回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 指明键永久保留.安全结构指明NULL,自动获得一默认值
//成功返回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]; //分配数值名称缓冲区
strcpy(valuename,"DeskPattern"); //随便指定哪个键值名
DWORD nsize=69; //数值名称缓冲区大小
DWORD k=REG_SZ; //指明数据类型
unsigned char vari[70]; //分配数值缓冲区
DWORD ncbvari=69; //数值缓冲区大小
dinx=0; //从0开始
while((II=RegEnumValue(hd,dinx,valuename,&nsize,NULL,&k,vari,&ncbvari))
!= ERROR_NO_MORE_ITEMS)
{
dinx++;//索引 +1,准备取下一个值
nsize=69; //恢复原来大小
ncbvari=69;
}
成功后返回值0,各变量返回后设置如下:
valuename=数值名称,以0结尾;如 : DeskColor
nsize=数值名称长度, 9
k=REG_SZ DeskColor 的类型为 REG_SZ
vari=键值,32768 DeskColor="32768",
ncbvari=键值长度 REG_SZ包括结尾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);
变量定义及成功后各变量设置值同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("你已经成功地将注册表自启动shell的键值设置为C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");
else
::AfxMessageBox("设定失败:目标位置不存在这样的键!");