简体   繁体   English

如何创建自动启动的C ++程序

[英]How to create an auto startup c++ program

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. 我正在用c ++创建一个程序,希望该程序可以让用户让它在Windows中自动启动。 So when a user starts his computer, windows will auto start this program. 因此,当用户启动计算机时,Windows将自动启动该程序。 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? 我已经阅读了有关修改注册表或将其放入启动文件夹的内容,但是什么是最好的“干净”方法呢?

Startup folder is clean enough. 启动文件夹足够干净。

Gives the user the possibility to remove it if needed. 如果需要,使用户可以删除它。

There are many ways to autostart an application, but the easiest, most common and IMO best are: 有许多方法可以自动启动应用程序,但是最简单,最常见和最适合IMO的是:

  1. Put a shortcut in the autostart folder 在自动启动文件夹中放置一个快捷方式
  2. Add an autostart entry to the registry (Software\\Microsoft\\Windows\\CurrentVersion\\Run) 将自动启动条目添加到注册表(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). 使用这两个选项,您可以为所有用户(“所有用户”启动文件夹,或在注册表中的HKLM项下)或特定于用户的(用户启动文件夹或HKCR项下)使用一种设置。

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. 您在菜单中看到的Startup文件夹是两者的合并,但是非管理员用户不能删除来自All-user部分的条目。

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. 完全不需要是C ++。

您可以将其注册为Windows服务。您可以使用“ Qt解决方案”轻松将应用程序制作为Windows服务。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM