Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use RegDeleteValue

Status
Not open for further replies.

BobRodes

Instructor
May 28, 2003
4,215
US
I'm having a hard time figuring out RegDeleteValue. I have a registry key: HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MyApp\MySubKey

I would like to remove certain values from this folder location, while preserving others. So, let's say I have a value whose name is "SQLDataSource" and whose value is "MYSERVER".

The code line
RegDeleteValue (HKEY_LOCAL_MACHINE, "SOFTWARE\MyCompany\MyApp\MySubKey\SQLDataSource") fails. I've tried several other things, and any doc or examples that I've found are very vague about what the second argument needs to be. For example, is a "registry value" the name or the data?

Can someone enlighten me?

Thanks,

Bob
 

You first have to open the registry and retrieve a key to the value you want to modify. Look at RegOpenKeyEx API (Also you may have to close the key).

Good Luck

 
Hello BobRodes,

[1] The parameters fed into RegDeleteValue API
Declare Function RegDeleteValue Lib "advapi32.dll" _
Alias "RegDeleteValueA" ( _
ByVal hKey As Long, _
ByVal lpValueName As String) As Long
are actually
[1.1] hKey As Long : being the handle of your targetted registry Key
"HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MyApp\MySubKey"
(As a handle of long, you should not expect it be the key string itself.)
[1.2] lpValueName As String : being the value name itself, so it be "SQLDataSource"

[3] Now the hKey is returned when you Open the key by the API function
[3.1] Declare Function RegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" ( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long
[3.2] where the return by reference phkResult is fed into the hKey of [1].
[3.3] The hKey here is the root key constant &H80000002 for HKEY_LOCAL_MACHINE.
In practical terms, the function's return RegOpenKeyEx provides a test of the existence of the key itself =0& means successful/existing of the key.

[4] Once the open is successful (returning 0&), call the RegDeleteValue by feeding the so-obtained phkResult ([3.2]) and lpValueName ([1.2]), the deletion of value would be successful if RegDeleteValue returns True. If it returns False, the probable reason is the non-existence of the value name at all.

[5] If all the hundles are over, you have to call the API
[5.1] Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
[5.2] Here hKey is again the handle of the key "HKEY_...\...\SubKey", ie that from [3.2]

This is the complete rundown of the construction of this functionality. Hope you get the idea.

regards - tsuji
 
Thanks so much for your detailed replay, tsuji. Sorry not to have responded for so long; I've been out of commission with a bad motherboard.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top