关于RegDeleteKeyEx()的问题

dccrazyboy 2009-10-05 04:20:09
我想做一个开机自动运行,自动运行的代码写好了,可以写注册表,但删除注册表项错了,具体如下。

BOOL stopAutoRun()
{
//删除注册表键,取消启动
HKEY hKey;
//找到系统的启动项
LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
//打开启动项Key
long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey);
if(lRet == ERROR_SUCCESS)
{
char pFileName[1024] = {0};
//得到程序自身的全路径
DWORD dwRet = GetModuleFileName(NULL, pFileName, 1024);
//添加一个子Key,并设置
lRet = RegDeleteKeyEx(hKey, "NotepadLock", KEY_WOW64_32KEY ,0);

//在这里调试时,lRet 的值是2,查了下,#define ERROR_FILE_NOT_FOUND 2L
//为什么会提示不存在??我用regedit看的都是存在的啊?

//关闭注册表
RegCloseKey(hKey);
if(lRet!= ERROR_SUCCESS )
{ if (lRet == ERROR_ACCESS_DENIED)
::MessageBox(NULL,"操作失败,请以管理员权限重新运行此程序!","错误",0);
else
::MessageBox(NULL,"操作失败,无法取消开机自动运行","错误",0);
return FALSE;
}
return TRUE;
}
::MessageBox(NULL,"打开注册表错误!","错误",0);
return FALSE;
}




请问下,这是怎么回事?难道是第二个参数传的不对?
...全文
1223 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlz66668888 2012-06-20
  • 打赏
  • 举报
回复
刚才实验了一下,即使 其下 有键值,也可以用 RegDeleteKey()删除子键。
zlz66668888 2012-06-20
  • 打赏
  • 举报
回复
另外删除子键 用 RegDeleteKeyEx() 或RegDeleteKey(),必须先删除 其下的 所有键值 , 然后才能删除 子键。
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
额……
能否理解为key就是左边的文件夹,value就是右边的项?
arong1234 2009-10-05
  • 打赏
  • 举报
回复
因为他是value,不是key,value是指有值的注册表项,key指的是有子节点和value的注册表项
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
十分感谢。能否简单的介绍下Key和Value有什么区别?
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
弄好了,换成这个就行了

lRet = RegDeleteValue(hKey, "NotepadLock");


但是为什么


lRet = RegDeleteKeyEx(hKey, "NotepadLock", KEY_WOW64_32KEY ,0);

这个不行??
arong1234 2009-10-05
  • 打赏
  • 举报
回复
估计你需要的不是DeleteKey,而是RegDeleteValue
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
也不行……
lRet还是2……
arong1234 2009-10-05
  • 打赏
  • 举报
回复
samDesired 设置为0看看?
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 arong1234 的回复:]
你在regedit找到你要删除的键没有?他的full path是什么?
[/Quote]
regedit里面看了,fullpath就是HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run

不知是怎么回事。
delphiwcdj 2009-10-05
  • 打赏
  • 举报
回复
KEY_WOW64_32KEY
arong1234 2009-10-05
  • 打赏
  • 举报
回复
你在regedit找到你要删除的键没有?他的full path是什么?
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
根据MSDN看,
RegDeleteKeyEx
The RegDeleteKeyEx function deletes a subkey from the specified platform-specific view of the registry. Note that key names are not case sensitive.

LONG RegDeleteKeyEx(
HKEY hKey,
LPCTSTR lpSubKey,
REGSAM samDesired,
DWORD Reserved
);


samDesired
[in] An access mask the specifies the platform-specific view of the registry.Value Meaning
KEY_WOW64_32KEY
0x0200 Delete the key from the 32-bit registry view.
KEY_WOW64_64KEY
0x0100 Delete the key from the 64-bit registry view.

这里只有2个可选参数啊,我用64位那个试了试,也不行……
arong1234 2009-10-05
  • 打赏
  • 举报
回复
你这个是KEY_WOW64_32KEY 键么?我估计不是
dccrazyboy 2009-10-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 arong1234 的回复:]
你的消息中应该显式出错的错误码,否则很难判断原因
[/Quote]

我现在就是说在这种情况下,会不会是我对函数的理解不对?
在这里调试时,lRet 的值是2,查了下,#define ERROR_FILE_NOT_FOUND 2L
错误我在中间注释中加了
arong1234 2009-10-05
  • 打赏
  • 举报
回复
下面几条挨个查一下:

A deleted key is not removed until the last handle to it is closed.

On WOW64, 32-bit applications view a registry tree that is separate from the registry tree that 64-bit applications view. This function enables an application to delete an entry in the alternate registry view.

The subkey to be deleted must not have subkeys. To delete a key and all its subkeys, you need to enumerate the subkeys and delete them individually. To delete keys recursively, use the RegDeleteTree or SHDeleteKey function.

If the function succeeds, RegDeleteKeyEx removes the specified key from the registry. The entire key, including all of its values, is removed.
arong1234 2009-10-05
  • 打赏
  • 举报
回复
你的消息中应该显式出错的错误码,否则很难判断原因

65,190

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧