Pergunta

I am creating a program in c++, which I want to be able to have the option to have users let it auto start in windows. So when a user starts his computer, windows will auto start this program. I have read stuff about modifying the registry or putting it in the startup folder, but what would be the best "clean" way to do this?

Foi útil?

Solução

Startup folder is clean enough.

Gives the user the possibility to remove it if needed.

Outras dicas

There are many ways to autostart an application, but the easiest, most common and IMO best are:

  1. Put a shortcut in the autostart folder
  2. Add an autostart entry to the registry (Software\Microsoft\Windows\CurrentVersion\Run)

The end result is the same for both. I believe the registry way is executed earlier in the logon process than the startup way, but I am not certain. It does not make any difference for most cases anyway. I prefer the registry, but that is personal taste. You can create and delete the registry key or the shortcut programatically in your app.

With both options you can use either one setting for all users (All User startup folder, or under HKLM key in the registry) or user specific (user startup folder or under HKCR key).

In general it is better to use the per user options, because you can be certain to have writing privileges in those areas; and every user on the computer can have his/her own setting.

Depending on whether you're executing an all-user or per-user install, put it in the Startup folder for All Users or the per-user Startup folder. The Startup folder you see in the menu is the merger of both, but non-admin users cannot remove the entries coming from the All-user part.

You actually don't have to do anything for this, though. Users can copy your normal shortcut to the Startup menu themselves. Hence, any program can be an auto-startup program. Doesn't need to be C++ at all.

You can register it as a windows service.You can use "Qt Solutions" for easily making an application as windows service.

With this code you can do it

procedure TForm1.Button1Click(Sender: TObject);
var
   Reg:TRegistry;
begin
   Reg := TRegistry.Create;
   try
      Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True);
      Reg.WriteString('Program name',ParamStr(0));
   finally
     Reg.Free;
   end;

end;

or this:

using Microsoft.Win32;
private void AddStartUpKey(string _name, string  _path) {
     RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Micros  oft\Windows\CurrentVersion\Run", true);
     key.SetValue(_name, _path);
}
private void RemoveStartUpKey(string _name) {
     RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Micros  oft\Windows\CurrentVersion\Run", true);
     key.DeleteValue(_name, false);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top