registry start up code

Hello,
I have a question I have a project with a portion of code that I don't understand because I don't have big knowledge of the windows registry , the app try to put itself on startup (see the code bellow) with the run registry key

What I don't understand is why even if the app can write into the run HKEY_LOCAL_MACHINE it also write into HKEY_USERS and HKEY_CURRENT_USER

My comprehension for now is if I put my app into HKLM I don't have to put into HKEY_USERS and HKEY_CURRENT_USER because it will execute any way, right ?

Can I write into HKEY_USERS if I don't have admin rights ?

does HKEY_LOCAL_MACHINE are copied to HKCU/HKU or something ?

if someone could help me for the global comphrehension of the code that would be nice

//Create the main registry key
KeyOption = 0;
RegCreateKeyEx(HKEY_CURRENT_USER,SoftwareName,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&KeyOption);
LenInBytes = lstrlen(Uniq) * sizeof(char);
RegSetValueEx(hKey,UniqName,0,REG_SZ,Uniq,LenInBytes); //store the UID
RegCloseKey(hKey);
//////////////////////////////


/*Create start-up keys*/
//try first to create admin key
SetLastError(ERROR_SUCCESS);
RegOpenKeyEx(HKEY_LOCAL_MACHINE,RunPath,0,KEY_ALL_ACCESS,&hKey);
if(GetLastError()==ERROR_SUCCESS) { //we are admin or system

LenInBytes = lstrlenW(CurrentLocation) * sizeof(WCHAR);
ret = RegSetValueExW(hKey,wUniq,0,REG_SZ,(const BYTE *)CurrentLocation,LenInBytes); //store the startup key
if(ret!=0) { RegCloseKey(hKey); goto UserPrivs; }
RegCloseKey(hKey);

//Write to key for all new user accoutns
RegOpenKeyEx(HKEY_USERS,AllUsersRunPath,0,KEY_ALL_ACCESS,&hKey);
LenInBytes = lstrlenW(CurrentLocation) * sizeof(WCHAR);
RegSetValueExW(hKey,wUniq,0,REG_SZ,(const BYTE *)CurrentLocation,LenInBytes); //store the startup key
RegCloseKey(hKey);

} //we are regular user

//try second to create user key
UserPrivs:
RegOpenKeyEx(HKEY_CURRENT_USER,RunPath,0,KEY_ALL_ACCESS,&hKey);
LenInBytes = lstrlenW(CurrentLocation) * sizeof(WCHAR);
RegSetValueExW(hKey,wUniq,0,REG_SZ,(const BYTE *)CurrentLocation,LenInBytes); //store the startup key
RegCloseKey(hKey);
////////////////////////////////////////


thank you !
Last edited on
if you write a registry entry inside HKEY_LOCAL_MACHINE then your program will be started for all users on the system once they log in. Changing it obviously requires admin rights.
Keys under HKEY_CURRENT_USER only apply to the currently logged in user. And I don't think you should ever modify HKEY_USERS directly.
Topic archived. No new replies allowed.