[英]Why isn't my change to the registry persisting in C++?
我正在尝试使用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;
}
}
我究竟做错了什么?
您似乎正在将注册表项设置为与读取时相同的值。
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);
我认为这:
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) &autorun, dwSize);
应该是这样的:
errorCode = RegSetValueEx(hkey, TEXT("NoDriveTypeAutoRun"), 0, dwType, (PBYTE) &newAutorun, dwSize);
(仔细观察第二至最后一个参数)
尝试更改此:
errorCode = RegSetValueEx(hkey,TEXT(“ NoDriveTypeAutoRun”),0,dwType,(PBYTE)&autorun,dwSize);
对此:
errorCode = RegSetValueEx(hkey,TEXT(“ NoDriveTypeAutoRun”),0,dwType,(PBYTE)&newAutorun,dwSize);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.