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”。

解决方案 »

  1.   

    查看所改键的权限时发现每次打开不成功的时候都是以一个不继承与current user的账号执行的操作。那么如何在工程中继承current user的权限呢?
      

  2.   

    估计你的注册表写入的方式不对,造成注册表权限乱了,第二次就写入不了了.
    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.
      

  3.   

    第二次写入的时候也并不是所有的键都写入不成功,我想改.htm文件的关联程序,所以要修改HKEY_CURRENT_USER\Softerware\Microsoft\Windows\CurrentVersion\Explorer\FileExtends\.htm\UserChoice这个键下Prodid的值。
    如果是写入方式不对的话,应该怎样写才能够修改UserChoice下的Progid的值呢?是写的地方有问题么?
    这个程序里我改了很多地方,但是出问题的全部都是和UserChoice有关的键值。