I was asked to build a program that monitors employees' actions (e.g. which website is he viewing), it's used to send user actions logs, network requests logs, etc. to center logging server. But I cannot ask user to run a program which will log their actions, so to start program automatically when Windows starts is necessary.
In summary, there are three methods to achieve this:
1. Add program to Run or RunOnce Key
2. Add program shortcut to Start Menu/Startup Folder
3. Register program as a service (But our program is a GUI application and Windows Service doesn't allow GUI, so we ignore this method)
If the program path is written into Run key, then this program will start every time when System boots.
We can write it into HKLM(HKEY_LOCAL_MACHINE) or HKCU(HKEY_CURRENT_USER), HKLM will apply settings to ALL users, while HKCU only applies settings to current user.
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "Monitor" "X:\Monitor.exe"
WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "Monitor" "X:\Monitor.exe"
RunOnce key will only run program automatically when the system boots first time. And after Windows is loaded, entries under RunOnce will be removed (It's usually used when application needs configuration after system reboots).
Start Menu/Startup Folder
Actually, programs under Start Menu/Startup Folder will be started after those defined in "Run" key.
CreateShortCut "$SMSTARTUP\Monitor.lnk" "$INSTDIR\Monitor.exe"
Loading order of Run, RunOnce, StartUp folder
Programs defined in Run, RunOnce or StartUp will be loaded in following order: