(2)查询某一个键值
(3)设置一个键值
(4)新建指定键
(5)删除注册表指定键下的值
删除注册表项
RegDeleteKey 也可用来删除一个键值。在 Win 95/98 平台下,也可用来删除整个子键和键值。但是在Windows NT/2000平台下,只能用来删除没有子键的键。
工具需要以管理员方式启动
+由于修改注册表后,需要重启电脑后才能生效。所以代码中加入了询问“是否立刻重启”的语句
#include
#include
#include
using namespace std;
void write_dword();
//测试成功,读取到start的当前值//读取操作表,其类型为DWORD
void read_dword(){
HKEY hKEY;//定义有关的键,在查询结束时关闭
//打开与路径data_Set相关的hKEY
char i,x;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt);
//访问注册表,hKEY则保存此函数所打开的键的句柄
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_READ, &hKEY)){
DWORD dwValue;//长整型数据,如果是字符串数据用char数组
DWORD dwSize = sizeof(DWORD);
DWORD dwType = REG_DWORD;
if (::RegQueryValueEx(hKEY, _T(Start), 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
cout << 错误:无法查询有关的注册表信息 << endl;
switch (dwValue){
case 3:
cout << 当前笔记本键盘状态为:启用 << ( << hex << dwValue << ) << endl << endl << 若需要禁用请输入字母:y或Y << endl;
cin >>i;
if (i == 'y'|| i == 'Y') {
write_dword();
}
break;
case 4:
cout << 当前笔记本键盘状态为:禁用 << ( << hex << dwValue << ) << endl << endl << 若需要启用请输入字母:y或Y << endl;
cin >>x;
if (x == 'y' || x == 'Y') {
write_dword();
}
break;
default:
cout << 当前笔记本键盘状态获取未知 << ( << hex << dwValue << ) << endl << endl;
break;
}
}
::RegCloseKey(hKEY);
}
//测试成功,读取到start设备名displayname//读取操作表,其类型为REG_SZ
void read_reg_sz(){
HKEY hkey;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt);
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_READ, &hkey)){
char dwValue[256];
DWORD dwSzType = REG_SZ;
DWORD dwSize = sizeof(dwValue);
if (::RegQueryValueEx(hkey, _T(DisplayName), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS){
cout << 无法查询有关的注册表信息 << endl << endl;
}
cout << 当前设备名: << dwValue << endl << endl;
}
::RegCloseKey(hkey);
}
//测试成功,更改(创建)到start键值//在SYSTEM\CurrentControlSet\Services\i8042prt文件夹下读取Start的子键,设置其值为dwValue
void write_dword(){
HKEY hkey;//定义有关的hkey,在查询结束时要关闭
HKEY hTempKey;
char ans;
int flag;
DWORD dwValue;
DWORD dwSize = sizeof(DWORD);
DWORD dwType = REG_DWORD;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt);
cout << 请设置当前笔记本键盘状态,输入数字:3,表示开启键盘;输入数字4,表示禁用键盘; << endl;
cin >>flag;
switch (flag) {
case 3:
dwValue = flag; break;
case 4:
dwValue = flag; break;
default:
cout << 状态值输入错误 << endl; break;
}
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_SET_VALUE, &hkey)){
//if (ERROR_SUCCESS == ::RegCreateKey(hkey, _T(解开该段落注释,则可更改为:在SYSTEM\\CurrentControlSet\\Services\\i8042prt下,创建以'该文本内容'为名称的子健,并创建一个名为“Start”的子健,其值为:dwValue), &hTempKey)){
if (ERROR_SUCCESS != ::RegSetValueEx(hkey, _T(Start), 0, REG_DWORD, (CONST BYTE*) & dwValue, sizeof(DWORD))){
cout << 写入注册表失败 << endl;
}
cout << 修改成功! << endl << 是否立刻重启电脑,使设置立即生效? << endl << 输入y或Y,以立刻重新启动电脑;输入n或N退出程序 << endl;
cin >>ans;
if (ans == 'y' || ans == 'Y') {
cout << 再次确认,要立刻重启电脑吗?\n\n输入y或Y,以立刻重新启动电脑;输入n或N退出程序 << endl;
cin >>ans;
if (ans == 'y' || ans == 'Y')
system(shutdown -r -t 0);
}
else
exit(1);
//}
}
::RegCloseKey(hkey);
}
//测试成功,更改(创建)子键test111及其键值项Name到start键值
void write_binary(){
HKEY hkey;
HKEY hTempKey;
BYTE m_name[10];
memset(m_name, 0, sizeof(m_name));
m_name[0] = 0xff;
m_name[1] = 0xac;
m_name[2] = 0x05;
m_name[3] = 0x4e;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt);
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_SET_VALUE, &hkey)){
if (ERROR_SUCCESS == ::RegCreateKey(hkey, _T(test111), &hTempKey)){
if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, _T(Name), 0, REG_BINARY, (unsigned char*)m_name, 5)){
cout << 写入错误 << endl;
}
}
}
::RegCloseKey(hkey);
}
//测试成功,更改(创建)子键test111及其键值项Name到start键值
void write_reg_sz(){
HKEY hkey;
HKEY hTempKey;
char m_name_set[256] = China;
DWORD len = strlen(m_name_set) + 1;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt);
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_SET_VALUE, &hkey)){
if (ERROR_SUCCESS == ::RegCreateKey(hkey, _T(test111), &hTempKey)){
if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, _T(Name), 0, REG_SZ, (const BYTE*)m_name_set, len)){
cout << 写入错误 << endl;
}
}
}
::RegCloseKey(hkey);
}
//测试成功,将键值项Name删除
void delete_value()
{
HKEY hkey;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt\\test111);
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_SET_VALUE, &hkey))
{
if (ERROR_SUCCESS != ::RegDeleteValue(hkey, _T(Name)))
{
cout << 删除错误 << endl;
}
}
::RegCloseKey(hkey);
}
//测试成功,将子键test111删除
void delete_key(){
HKEY hkey;
LPCTSTR data_set = _T(SYSTEM\\CurrentControlSet\\Services\\i8042prt);
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_SET_VALUE, &hkey)){
if (ERROR_SUCCESS != ::RegDeleteKey(hkey, test111)){
cout << 删除错误 << endl;
}
}
::RegCloseKey(hkey);
}
void menu(){
cout << 欢迎使用本工具 << endl << endl;
read_reg_sz();
cout << endl << 已为您查询到该笔记本当前键盘状态为: ;
read_dword();
}
int main(){
menu();
system(pause);
return 0;
}