hi,all
我在win7+VS2008下写了一个修改注册表的程序,相关部分如下:void SetRegValue(HKEY hk, TCHAR *szKey, TCHAR *szSubKey, TCHAR *szValue)
{
DWORD dwDisp, dwLength;
if(ERROR_SUCCESS == RegCreateKeyEx(hk, szKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hk, &dwDisp))
{
cout << "opened!" << endl;
RegCloseKey(hk);
}
dwLength = (DWORD)((lstrlen(szValue)+1)*sizeof(TCHAR));
if(ERROR_SUCCESS == RegSetValueEx(hk, szSubKey, 0, REG_SZ, (BYTE*)szValue, dwLength))
{
cout << "setted!" << endl;
RegCloseKey(hk);
}
}当我运行该程序时,第一次能够修改注册表成功,但是当我把注册表恢复之后再运行该程序的时候,却往往不能成功修改注册表项,即使是用手动修改系统也会提示“写入新值时出错”。
请问这是为什么?有什么办法能在我的工程里解决么?
PS:我已将manifest的权限改为“requireAdministrator”。
我在win7+VS2008下写了一个修改注册表的程序,相关部分如下:void SetRegValue(HKEY hk, TCHAR *szKey, TCHAR *szSubKey, TCHAR *szValue)
{
DWORD dwDisp, dwLength;
if(ERROR_SUCCESS == RegCreateKeyEx(hk, szKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hk, &dwDisp))
{
cout << "opened!" << endl;
RegCloseKey(hk);
}
dwLength = (DWORD)((lstrlen(szValue)+1)*sizeof(TCHAR));
if(ERROR_SUCCESS == RegSetValueEx(hk, szSubKey, 0, REG_SZ, (BYTE*)szValue, dwLength))
{
cout << "setted!" << endl;
RegCloseKey(hk);
}
}当我运行该程序时,第一次能够修改注册表成功,但是当我把注册表恢复之后再运行该程序的时候,却往往不能成功修改注册表项,即使是用手动修改系统也会提示“写入新值时出错”。
请问这是为什么?有什么办法能在我的工程里解决么?
PS:我已将manifest的权限改为“requireAdministrator”。
RegCreateKeyEx()的 参数
http://msdn.microsoft.com/en-us/library/ms724844(v=vs.85).aspxlpSecurityAttributes [in, optional]
A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited. The lpSecurityDescriptor member of the structure specifies a security descriptor for the new key. If lpSecurityAttributes is NULL, the key gets a default security descriptor. The ACLs in a default security descriptor for a key are inherited from its direct parent key.
如果是写入方式不对的话,应该怎样写才能够修改UserChoice下的Progid的值呢?是写的地方有问题么?
这个程序里我改了很多地方,但是出问题的全部都是和UserChoice有关的键值。