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;
constchar 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?
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;
constchar 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;
}