How to check existance of a registry value?

J

Jerry

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!
 
S

Scherbina Vladimir

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);
}
 
T

Tom Serface

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
 
J

Jerry

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top