View Full Version : How to check existance of a registry value?


Jerry
10-28-2005, 06:57 AM
I need to delete a Registry value (not a key). I called RegDeleteValue
expecting it returns ERROR_SUCCESS either if the value gets deleted or
it doesn't exist there at all. However, if the value doesn't exist
there, RegDeleteValue returns an error of "Invalid handle", which may
something failure cases too.

I wonder if there's way to check the existance of a registry value?
Therefore I can call RegDeleteValue only if the value exists there.

In addition, I had thought RegDeleteValue would return something like
"Value doesn't exist". If so, I wouldn't have to check the value's
existance in advance.

Thanks!

Scherbina Vladimir
10-28-2005, 07:56 AM
Hello Jerry.

HKEY hRootKey = NULL;
if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, strRegPath,
0, KEY_READ, &hRootKey ) == ERROR_SUCCESS )
{
char strVal[ MAX_PATH ];
DWORD dwLen = sizeof(strVal);
if( ::RegQueryValueEx( hRootKey, "ValueName", NULL,
NULL, reinterpret_cast< BYTE* >( strVal ), &dwLen ) ==
ERROR_SUCCESS )
{
/// value is present, call RegDeleteValue here
}
RegCloseKey(hRootKey);
}

--
[Scherbina Vladimir]

"Jerry" <rrrshop@hotmail.com> wrote in message
news:1130479042.516454.73470@f14g2000cwb.googlegroups.com...
>I need to delete a Registry value (not a key). I called RegDeleteValue
> expecting it returns ERROR_SUCCESS either if the value gets deleted or
> it doesn't exist there at all. However, if the value doesn't exist
> there, RegDeleteValue returns an error of "Invalid handle", which may
> something failure cases too.
>
> I wonder if there's way to check the existance of a registry value?
> Therefore I can call RegDeleteValue only if the value exists there.
>
> In addition, I had thought RegDeleteValue would return something like
> "Value doesn't exist". If so, I wouldn't have to check the value's
> existance in advance.
>
> Thanks!
>

Tom Serface
10-28-2005, 07:59 AM
You could just try to read the value from the registry and see if you get an
error trying to read it. Of course, if you are not an admin user you will
fail unless you have permission for the key with, most likely, a 5 (can't
access) error, but you'll have to watch for that on limited user logins
anyway. This class has a KeyExists() function that might be useful to you
to:

http://www.codeproject.com/system/CRegistry.asp

Tom

"Jerry" <rrrshop@hotmail.com> wrote in message
news:1130479042.516454.73470@f14g2000cwb.googlegroups.com...
>I need to delete a Registry value (not a key). I called RegDeleteValue
> expecting it returns ERROR_SUCCESS either if the value gets deleted or
> it doesn't exist there at all. However, if the value doesn't exist
> there, RegDeleteValue returns an error of "Invalid handle", which may
> something failure cases too.
>
> I wonder if there's way to check the existance of a registry value?
> Therefore I can call RegDeleteValue only if the value exists there.
>
> In addition, I had thought RegDeleteValue would return something like
> "Value doesn't exist". If so, I wouldn't have to check the value's
> existance in advance.
>
> Thanks!
>

sunit
10-28-2005, 11:56 AM
"Jerry" <wrote> I wonder if there's way to check the existance of a registry value?
> Therefore I can call RegDeleteValue only if the value exists there.
>
you can MFC

CRegKey::QueryValue

sunit
10-28-2005, 12:04 PM
"Jerry" < wrote in message
> In addition, I had thought RegDeleteValue would return something like
> "Value doesn't exist". If so, I wouldn't have to check the value's
>

The error return by RegDeleteValue is defined in Winerror.h.
To get a generic description of the error, call FormatMessage with the
FORMAT_MESSAGE_FROM_SYSTEM flag set.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecoreos5/
html/wce50lrfFormatMessage.asp

Jerry
10-28-2005, 04:40 PM
Thanks for everyone's reply! It seems RegQueryValue is the way to go.
But since both RegDeleteValue and RegQueryValue return confusing
errors, why not just use RegDeleteValue with one call instead of using
both functions with two calls?

Thanks!