[C/C++][Windows] Editing the registry

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C/C++][Windows] Editing the registry

Post by ayu »

I wrote a small program that would simply add an entry in the registry, it worked fine when adding to HKEY_CURRENT_USER, but not when I did it with HKEY_LOCAL_MACHINE. According to the return value the value was created successfully, yet it doesn't appear.

First I thought it was due to registry virtualization, but then I read that it doesn't apply to the subkeys that I am using.

Anyone have any experience with this, or maybe can spot the error? Too many experiments right now, so hard to keep all the balls in the air at the same time ^^

Code: Select all

#include <windows.h>

int main()
{
	HKEY		phkResult;
	DWORD		retVal;

	//Open registry key and add file path as a new value
	retVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCWSTR)L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &phkResult);

	if(retVal == ERROR_SUCCESS)
	{
		retVal = RegSetValueEx(phkResult, (LPCWSTR)L"Something", 0, REG_SZ, (LPBYTE)L"C:\\Something.exe", MAX_PATH + 1); 
		RegCloseKey(phkResult);
	}

        return 0;
}
"The best place to hide a tree, is in a forest"

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Post by Lundis »

Try running it as admin. That's why I asked if you ran vista/7 on irc ^^

HKEY_CURRENT_USER is userdata so any app should be able edit it, but HKEY_LOCAL_MACHINE is certainly not.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Yeah tried it now, didn't work ^^

EDIT:

I can add that it doesn't seem to be anything wrong with the code. It worked perfectly on another computer with Windows XP and without any Anti Virus. Going to do some more tests to find out exactly what it is.

EDIT 2:

It's the UAC in Vista/7 that does it :roll:

Either I have to skip adding my stuff to HKEY_LOCAL_MACHINE, or I have to bypass UAC somehow, OR add a manifest file that co-operates with UAC, giving the user one of those "are you sure you want to do this?" windows, which I want to avoid.


Gonna try with the manifest file anyway though, just for fun.

EDIT 3: ... or...is it?
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Ok so a little update on this ...

It worked from the very start, I simply didn't add my 64bit registry structure into the calculation. So the value was added from the beginning.

Now, the new question is, why the hell DID it work? Why didn't UAC stop me?
"The best place to hide a tree, is in a forest"

Post Reply