Unable to write to #HKEY_LOCAL_MACHINE on Windows 10, 11
Posted: Wed Dec 20, 2023 9:51 am
I'd like to run my background process automatically when a user logs in and I found that by far the most convenient way for me, was to set the appropriate registry key in #HKEY_LOCAL_MACHINE during the first instance that the programme is executed. This worked well on Windows Server 2016, but Windows 10 and 11 ignore it. They do however allow me to create the key in #HKEY_CURRENT_USER.
Is this now a restriction in Windows that we cannot get around? The reason I'd like to be able to do this with #HKEY_LOCAL_MACHINE is because if the software is installed for a user, IT-department staff will need to remember to re-run it and re-create the key, if the machine is later reconfigured for a new member of staff. This isn't a big thing — I can live with this anyway — but I thought it worthy of mention here, in case (a) others have a workaround for this or (b) it helps anyone else in future (because this took me a while to realise).
As an aside to this, I've been struggling a bit to find a convenient way to auto-run a background process that doesn't require users to go to a lot of trouble to configure it manually. During development, I used the Windows Task Scheduler, set to "At logon", which works very well but isn't easy for users to set up.
A service would be great but it seemed too complex. I saw there's an sc.exe that can be used to create services, but haven't had time to try it. Has anyone else used this successfully?
Is this now a restriction in Windows that we cannot get around? The reason I'd like to be able to do this with #HKEY_LOCAL_MACHINE is because if the software is installed for a user, IT-department staff will need to remember to re-run it and re-create the key, if the machine is later reconfigured for a new member of staff. This isn't a big thing — I can live with this anyway — but I thought it worthy of mention here, in case (a) others have a workaround for this or (b) it helps anyone else in future (because this took me a while to realise).
Code: Select all
#APPNAME = "ClientA"; ; Application name (used in setting registry key)
; **
; ** Write auto run key
; **
; ** Pass parameters key.l : #HKEY_CURRENT_USER for start at user logon
; ** #HKEY_LOCAL_MACHINE for start with all users
; ** value.s : Application name (not necessarily the same as the executable)
; ** state.b : 1 = Set, 0 = Unset
; **
Procedure StartWithLogin(key.l, value.s, state.b)
Protected path.s = "Software\Microsoft\Windows\CurrentVersion\Run" ; Use RunOnce to run once only
Protected string.s = Chr(34) + ProgramFilename() + Chr(34) ; Full path to the executable
Protected curkey.l
If state.b ; If setting true
RegCreateKey_(key, @path, @curkey)
RegSetValueEx_(curkey, @value, 0, #REG_SZ, @string, Len(string) * 2)
Else ; Setting false, so remove key
RegOpenKey_(key, @path, @curkey)
RegDeleteValue_(curkey, @value)
EndIf
RegCloseKey_(curkey) ; Close the key
EndProcedure
StartWithLogin(#HKEY_LOCAL_MACHINE, #APPNAME, 1) ; Write registry key to autostart
; Change above line to #HKEY_CURRENT_USER and it works
A service would be great but it seemed too complex. I saw there's an sc.exe that can be used to create services, but haven't had time to try it. Has anyone else used this successfully?