RegDeleteKeyEx() function

Can someone please explain to me the first two parameters of RegDeleteKeyEx(HKEY hKey, LPCTSTR lpSubKey, REGSAM samDesired, DWORD Reserved) function, i.e. what's meant by SubKey in this case and is the hkey obtained from RegOpenKeyEx() function?
Yes, the first one is usually a key handle obtained via RegOpenKeyEx() or RegCreateKey() as explaind @ MSDN Online. The second parameter is a C string that represents the subkey to delete. Note that it has to be a child of the key specified by the key handle in parameter 1. But really, what else can I tell you that MSDN hasn't?

http://msdn.microsoft.com/en-us/library/ms724847(VS.85).aspx
Last edited on
Is LPCTSTR lpSubKey what is troubling you? That's just a pointer to either a const char or wchar_t (if UNICODE defined) buffer.
Hmm ok, I guess this code should run fine then... However it doesn't delete the "easy" registry. Why could this be? (btw I'm using 64 bit operating system)

1
2
3
4
5
6
7
8
	HKEY hkey1 = NULL;

	RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 
		0, KEY_CREATE_SUB_KEY, &hkey1);
	
	RegDeleteKeyEx(hkey1, "easy", KEY_WOW64_64KEY, 0);

	RegCloseKey(hkey1);
Just some thoughts...

Is your program running under an admin account? That's likely necessary. Also, when doing registry code I inevitably find I have to actually log return values from everything until I get it working. I'm always really careful about that with registry code. One time I blew a whole registry out with an error in my code. You would be amazed how little you can do with a Windows computer with the registry completely gone!
You're using the wrong access flags. Replace KEY_CREATE_SUB_KEY with KEY_ALL_ACCESS. You may also be getting locked out of the local machine registry section due to Windows 7 or Vista UAC restrictions. Change the manifest settings to requireAdministrator or right-click your EXE and "Run as Administrator"

Refer here:

http://msdn.microsoft.com/en-us/library/ms724878(v=vs.85).aspx

Yes, I am running under an admin accout... Thanks for the tip, but I still can't figure out what's wrong. I thought that I misinterpreted the lpSubKey in RegDeleteKeyEx() which in my case is the name of the registry, but now it seems right so I'm confused even more...
Last edited on
Your code looks fine to me. Make a call to GetLastError() after RegDeleteKeyEx() and tell me what the return value is.
If RegDeleteKeyEx() fails, you don't call GetLastError(). The error code is the return value of RegDeleteKeyEx().

OP: Collect the error code and pass it along to FormatMessage() to get the error message. Then post the error message here.
0 == ERROR_SUCCESS. Is "easy" a registry key, or a registry key value? Keys appear to the left in the treeview in the registry editor, while values only appear in the right hand side.

Also track the error codes returned by all registry function calls, like RegOpenKeyEx(). Also note that I don't know every return code's meaning. Use FormatMessage() to obtain error messages.

BTW, you are missing the TEXT() macro (or the _T() macro) around your string literals.
Last edited on
Sorry 0 was the return value to the GetLastError() function. But when I used FormatMessage() the message that I got is "The system cannot find the file specified.". And yes "easy" is the value name.
To delete a key value you have to use RegDeleteValue().

http://msdn.microsoft.com/en-us/library/ms724851(VS.85).aspx
Ok, thanks. That's what I've been looking for but now I get a message of access is denied even though I'm running the program under an administrator account.
I got it running by changing the Registry Key Security and Access Rights in RegOpenKeyEx() from what I had to KEY_ALL_ACCESS. Thanks for all the help!
Topic archived. No new replies allowed.