我正在尝试使用C ++编辑注册表,这是我第一次尝试这样做,而且我失败了。我没有收到任何错误代码,一切都说成功完成,但实际上并没有改变注册表项。
以下是我正在使用的代码:
HKEY hkey;
DWORD dwDisposition, dwType, dwSize;
int autorun = 0x00;
int CD_AUTORUN_DISABLED = 0x20;
long errorCode;
errorCode = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"), 0, KEY_ALL_ACCESS, &hkey);
if(errorCode == ERROR_SUCCESS) {
dwType = REG_DWORD;
dwSize = sizeof(dwType);
errorCode = RegQueryValueEx(hkey, TEXT("NoDriveTypeAutoRun"), NULL, &dwType,
(PBYTE)&autorun, &dwSize);
cout << "Autorun value: " << autorun << endl;
if((autorun & CD_AUTORUN_DISABLED) == 0x20){
int newAutorun = (autorun - CD_AUTORUN_DISABLED);
cout << "New value: " << newAutorun << endl;
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) &autorun, dwSize);
if(errorCode == ERROR_SUCCESS){
errorCode = RegCloseKey(hkey);
if(errorCode == ERROR_SUCCESS){
cout << "Value changed." << endl;
}
}else{
cout << "Value change failed, error code: " << errorCode << endl;
}
}else{
cout << "Keep current value." << endl;
}
}else{
if(errorCode == ERROR_ACCESS_DENIED){
cout << "Access denied." << endl;
}else{
cout << "Error! " << errorCode << " : " << ERROR_SUCCESS << endl;
}
}
我做错了什么?
答案 0 :(得分:2)
您似乎将注册表项设置为您读取的相同值。
int newAutorun = (autorun - CD_AUTORUN_DISABLED);
cout << "New value: " << newAutorun << endl;
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) **&autorun**, dwSize);
应该是
int newAutorun = (autorun - CD_AUTORUN_DISABLED);
cout << "New value: " << newAutorun << endl;
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) &newAutorun, dwSize);
答案 1 :(得分:0)
我想这个:
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) &autorun, dwSize);
应该是这样的:
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) &newAutorun, dwSize);
(仔细看第2到最后一个参数)
答案 2 :(得分:0)
尝试更改此内容:
errorCode = RegSetValueEx(hkey,TEXT(“NoDriveTypeAutoRun”),0,dwType,(PBYTE)&amp; autorun,dwSize);
到此:
errorCode = RegSetValueEx(hkey,TEXT(“NoDriveTypeAutoRun”),0,dwType,(PBYTE)&amp; newAutorun,dwSize);