开发者

C++ application autorun on windows boot

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:

  1. How to detect startup folder. I am not happy with SHGetSpecialFolderLocation()'s output. It se开发者_运维问答ems it returns my "HOME" directory in testing WinXP platform (C:\Documents and settings\%username%).
  2. 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

  1. 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.

  2. 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜