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?