我正在用 C++ 开发一个应用程序,它有一个选项“自动启动”。当用户设置该选项时,程序应更改 Windows 中的某些内容,以便在下次 Windows 启动和用户登录时由 Windows 启动。此外,当用户取消设置此选项时,程序必须将内容放回原处。
我不需要将应用程序作为服务运行,因此在用户登录后在特定用户的帐户下运行就足够了。那么让 Windows 运行我的应用程序的最佳方法是什么?下次启动和用户登录时?我在谷歌上搜索了使用启动文件夹的建议。这是最好的、无害的方式吗?
如果是这样,我有两个问题:
- 如何检测启动文件夹。我对 SHGetSpecialFolderLocation() 的输出不满意。似乎它返回了我在测试 WinXP 平台时的“HOME”目录(C:\Documents and settings\%username%)。
- 如何使用 C++ 创建 .lnk 文件?我认为复制整个 .exe 文件是个坏主意。
实际上我的程序是跨平台的,但选项“自动启动”将仅适用于Windows。
更新
-
尝试过 SHGetSpecialFolderLocation 并在 Windows7 中获得了不错的值:“C:\Users\Pavel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup”
注意:如果您使用 MinGW,则应在包含 ShlObj.h 之前将 _WIN32_IE 定义为 0x0400。
-
使用注册表 (HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run) 和启动文件夹之间的最佳解决方案是什么(从良好编程的角度来看)?
I am developing an application in C++ that has an option "autostart". When user set that option, program should change something in Windows to be started by Windows on next Windows boot and user logon. Also when user unset this option, the program must put things back.
I don't need to run app as a service, so it would enough to run under certain user's account after user logon. So what is the best way to make windows run my app. on next boot and user logon? I googled up an advice to use a startup folder. Is that most nice and harmless way?
If so, i have 2 questions:
- How to detect startup folder. I am not happy with SHGetSpecialFolderLocation()'s output. It seems it returns my "HOME" directory in testing WinXP platform (C:\Documents and settings\%username%).
- How to create .lnk file using C++ there? I think copying entire .exe file would be bad idea.
Actually my program is cross-platform, but an option "autostart" will be windows-only.
Update
-
Tried SHGetSpecialFolderLocation and got a good value in Windows7: "C:\Users\Pavel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
NOTICE: If you use MinGW, you should define _WIN32_IE to 0x0400, before including ShlObj.h.
-
What is the best solution (in point of view good programming) between using registry (HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run) and the startup folder?
发布评论
评论(3)
我建议使用
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
等注册表项。使用您的软件名称和主可执行路径的值在此处创建一个字符串值 - 这应该足够了。创建 .lnk 文件要容易得多 - 并且可以从 C++更新轻松访问注册表编辑 API:
正如@Ben 建议的那样,对于您的任务,最好使用 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 。这只会为当前用户自动启动您的应用程序,而我的第一个建议将为所有用户执行此操作。
UPDATE2:
另请注意,无论您选择哪种方法来实现自动启动,您都可能会遇到防病毒软件的问题,因为大多数具有主动防御功能的防病毒软件都会密切关注程序的自动运行。
I'd suggest using registry keys like
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
. Create a string value here with the name of your software and value of your main executable path - this should suffice. Much easier then creating a .lnk file - and registry editing API is easily accessible from C++UPDATE:
As @Ben suggested, for your task it's better to use
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
. This will autostart your app for current user only, while my first suggestion will do this for all users.UPDATE2:
Also note that whatever method you choose for implementing autostart, you might get problems with antivirus software as most of antiviruses with proactive defense are watching closely for making programs auto-running.
需要明确的是,启动文件夹项目发生在登录时,而不是启动时。这听起来正是你想要的,我只是想弄清楚条款。
我认为您实际上想要 SHGetSpecialFolderPath 和 CSIDL_STARTUP 常量。如果我正确阅读文档,那应该会给你你想要的路径。
Just to be clear, the startup folder items happen at login time, NOT boot. That sounds like what you want, I just want to get the terms straight.
I think you actually want SHGetSpecialFolderPath, and the CSIDL_STARTUP constant. If I'm reading the docs correctly that should give you the path you want.
使用 SHGetSpecialFolderPath 确定启动文件夹的路径(请参阅 Michael Kohne 的回答)。
然后使用 IShellLink 以便在此文件夹中创建快捷方式。
编辑:我认为使用注册表中的“运行”键更可靠。
Determine the path to the startup folder using SHGetSpecialFolderPath (see Michael Kohne's answer).
Then use IShellLink in order to create a shortcut in this folder.
Edit: I think using the Run key in the registry is more reliable.