Need help with RegSetValueEx()

I have posted it in beginners forum already, but got no answer yet.
I am trying to add an application to run on startup in Windows. I am using RegSetValueEx() to do this:
1
2
3
4
5
6
7
8
9
10
11
12
int main()

{
	HKEY hkey;
	long regOpenResult;
	const char path[]="C:\\Users\\Gizda\\Documents\\Visual Studio 2010\\Projects\\stuffs\\Debug\\stuffs.exe";
	regOpenResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hkey);
	LPCSTR stuff = "stuffs";
	RegSetValueEx(hkey,stuff,0,REG_SZ,(BYTE*) path, strlen(path));
	RegCloseKey(hkey);
	return 0;
}


When I compile the code above, it runs fine, does not give any error. When I check for the return value of both ReturnOpenKeyEx and RegSetValueEx, they return ERROR_SUCCESS. But the key is not placed in the registry. Any ideas?

Btw I am running Windows 7 x64.
You should use RegDisableReflectionKey() API to disable registry reflection (certain registry keys are "redirected" for 32 bit processes in 64 bit versions of windows):
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724858(v=vs.85).aspx


For more information read here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724072(v=vs.85).aspx
In Windows 7 the registry reflection is deleted, so the RegDisableReflectionKey() doesn't help. Although with accessing an alternate registry view, you can access a 64-bit key either from a 32-bit or 64-bit application. So I had to add KEY_WOW64_64KEY flag to the samDesired parameter of RegOpenKeyEx().

The final code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
int main()

{
	HKEY hkey;
	long regOpenResult;
	const char path[]="C:\\Users\\Gizda\\Documents\\Visual Studio 2010\\Projects\\stuffs\\Debug\\stuffs.exe";
	regOpenResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hkey);
	LPCSTR stuff = "stuffs";
	RegSetValueEx(hkey,stuff,0,REG_SZ,(BYTE*) path, strlen(path));
	RegCloseKey(hkey);
	return 0;
}


And it works now. Thanks for helping!
Topic archived. No new replies allowed.