您的位置:首页 > 其它

关于WinCE调用RegDeleteKey不能删除注册表项的问题

2014-03-28 23:43 225 查看
在写一个WinCE注册表操作的时候,调用RegDeleteKey函数却不能删除注册表项,始终返回5的错误(ACCESS DENY)。查了一下,有的说是什么权限的问题,操作的时候都是先打开注册表,如果打开成功,即调用RegDeleteKey删除此键,其实这都是不对的,经过反复查看其文档介绍,终于在一个不起眼的句子中找到了答案。下面先介绍一个此函数,直接贴原文。


RegDeleteKey (Windows CE 5.0)

Windows CE 5.0

This topic has not yet been rated - Rate
this topic

Send
Feedback

This function recursively deletes all subkeys of a named subkey of a specified registry key.

A remote application interface (RAPI) version of this function exists, and it is called CeRegDeleteKey (RAPI).

LONG RegDeleteKey(
HKEY hKey,
LPCWSTR lpSubKey
);



Parameters

hKey[in] Handle to a currently open key or one of the following predefined reserved handle values:

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS

The key specified by the lpSubKey parameter must be a subkey of the key identified by hKey.

The access rights of this key do not affect the delete operation.

lpSubKey[in] Pointer to a null-terminated string specifying the name of the key to delete. This parameter cannot be NULL.


Return Values

ERROR_SUCCESS indicates success. A nonzero error code defined in Winerror.h indicates failure. To get a generic description of the error, call FormatMessage with
the FORMAT_MESSAGE_FROM_SYSTEM flag set. The message resource is optional; therefore, if you callFormatMessage it could fail.


Remarks

If the function succeeds, RegDeleteKey removes the specified key from the registry. The entire key, including all of its values, is removed. An application cannot call RegDeleteKey for a key that an application currently has
open.

To open the key, use the RegCreateKeyEx or RegOpenKeyEx function.

注意到文中的三句话:The access rights of this key do not affect the delete operation.

Handle
to a currently open key or one of the following predefined reserved handle values

An application cannot call RegDeleteKey for
a key that an application currently has open.

就是说,键的访问权限不影响RegDeleteKey的删除操作,对于预先定义好的键值,不能在一个已经打开的键上调用RegDeleteKey,否则会报5的错误,即无需打开再删除,而是直接调用此函数删除键值。如下为例子:

要删除的键值为:

HKEY_LOCAL_MACHINE/Services/FinServ

代码为:

HKEY RootKey = HKEY_LOCAL_MACHINE;

CString strSubKey = _T("\\Services\\FinServ");

long
nRet = 0;

nRet
= RegDeleteKey(RootKey,strSubKey);

if(nRet== ERROR_SUCCESS)

{

//删除成功

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: