windows 如何创建自动启动 C++ 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/557466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an auto startup c++ program
提问by Toon Krijthe
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?
我正在用 C++ 创建一个程序,我希望能够选择让用户让它在 Windows 中自动启动。所以当用户启动他的电脑时,windows 会自动启动这个程序。我已经阅读了有关修改注册表或将其放入启动文件夹的内容,但是执行此操作的最佳“干净”方法是什么?
回答by Toon Krijthe
Startup folder is clean enough.
启动文件夹足够干净。
Gives the user the possibility to remove it if needed.
使用户可以在需要时将其删除。
回答by Treb
There are many ways to autostart an application, but the easiest, most common and IMO best are:
有很多方法可以自动启动应用程序,但最简单、最常见和 IMO 最好的是:
- Put a shortcut in the autostart folder
- 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.
一般来说,最好使用每个用户选项,因为您可以确定在这些区域具有写入权限;并且计算机上的每个用户都可以有自己的设置。
回答by MSalters
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, anyprogram can be an auto-startup program. Doesn't need to be C++ at all.
不过,您实际上不必为此做任何事情。用户可以自己将您的常规快捷方式复制到“启动”菜单。因此,任何程序都可以是自动启动程序。根本不需要是 C++。
回答by Qubeuc
You can register it as a windows service.You can use "Qt Solutions" for easily making an application as windows service.
您可以将其注册为 windows 服务。您可以使用“Qt Solutions”轻松地将应用程序制作为 windows 服务。
回答by ENDLESS
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);
}