I have written a program which creates the following subkey in the Windows Registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList
I have written a second program which undoes what my first program does. The second program has to check if the subkey contains further subkeys or values before the subkey is then deleted. If the subkey doesn't contain further subkeys or values, the subkey will be deleted.
How can I check if the subkey contains any subkeys or values?
Use RegQueryInfoKey()
:
Retrieves information about the specified registry key.
Amongst many different parameters that it can output, it has the following two parameters that are useful to you:
lpcSubKeys [out, optional]
A pointer to a variable that receives the number of subkeys that are contained by the specified key. This parameter can be NULL.lpcValues [out, optional]
A pointer to a variable that receives the number of values that are associated with the key. This parameter can be NULL.
With that said, do note that RegDeleteKey()
will fail if the specified key has any subkeys, but not if it has any values. So you really do not need to check for the existence of subkeys at all, only for values. Let the function fail normally if there are no values present.
An even simpler solution is to use SHDeleteEmptyKey()
instead, which will fail if the key has any subkeys or values. Then you don't need to check manually at all.