My C++ application running under SYSTEM
is unable to create new values or keys in the HKEY_CURRENT_USER
registry the code seems to work fine when run as the current user but it doesn't work when running under SYSTEM. I'm not sure why this is happening but if I had to guess it's because it's not running as the current user. Is there any way to make it write to the HKEY_CURRENT_USER
registry without having the application run as the current user?
Here's the code that works when run as the current user but not as SYSTEM
void WriteToCurUsrReg()
{
TCHAR ExplorePath[256], RunPath[256];
TCHAR notepadexe[50] = {L"notepad.exe"};
StringCchPrintf(ExplorePath, sizeof(ExplorePath), L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
StringCchPrintf(RunPath, sizeof(RunPath), L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer\\Run");
DWORD runvalue = 1;
HKEY hKey = NULL;
LONG lErrorCode = 0;
lErrorCode = RegOpenKeyEx(HKEY_CURRENT_USER, ExplorePath, 0, KEY_ALL_ACCESS, &hKey);
if (lErrorCode == ERROR_SUCCESS)
{
RegSetValueEx(hKey,L"Run",0,REG_DWORD,(LPBYTE)&runvalue,sizeof(runvalue));
lErrorCode = RegCloseKey(hKey);
if (lErrorCode != ERROR_SUCCESS)
{
printf("RegCloseKey Failed :%d %s %d",GetLastError() ,__FILE__ , __LINE__);
}
}
lErrorCode = RegOpenKeyEx(HKEY_CURRENT_USER, RunPath, 0, KEY_ALL_ACCESS, &hKey);
if (lErrorCode == ERROR_SUCCESS)
{
RegSetValueEx(hKey,L"1",0,REG_SZ,(LPBYTE)notepadexe,128);
lErrorCode = RegCloseKey(hKey);
if (lErrorCode != ERROR_SUCCESS)
{
printf("RegCloseKey Failed :%d %s %d",GetLastError() ,__FILE__ , __LINE__);
}
}
else
{
hKey = NULL;
LONG lError = RegCreateKey(HKEY_CURRENT_USER, RunPath, &hKey);
DWORD dwLength = 0;
if(lError == ERROR_SUCCESS)
{
RegSetValueEx(hKey,L"1", 0, REG_SZ,(LPBYTE)notepadexe, 128);
lErrorCode = RegCloseKey(hKey);
if (lErrorCode != ERROR_SUCCESS)
{
printf("RegCloseKey Failed :%d %s %d",GetLastError() ,__FILE__ , __LINE__);
}
}
}
}
There are no errors that occur when I run the program under SYSTEM, the program successfully runs and exits without issue but the function the program is supposed to perform is not complete ie: the program doesn't create any keys or values in the HKEY_CURRENT_USER registry.