Run At Startup Programmatically
Environment: VC6, Win9x
You can use this code to let your program run automatically at system startup, it shows you also how to use some of API registry functions. Before you start you must know that Windows knows what to run at startup by keeping a list of program's name and pathes under the following Key in the system registry:
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run.
All that you will do now is to add our program's path and name in this key by using "RegOpenKeyEx" and "RegSetValue" API registry functions. The algorithm is so simple:
FIRST : Open the "Run" key.
SECOND: Set it with new value (program's path and name.)
Here is the source code:
LONG lnRes = RegOpenKeyEx(
HKEY_LOCAL_MACHINE, // handle of open key
// The following is the address of name of subkey to open
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0L,KEY_WRITE,
&hKey // address of handle of open key
);
// now add program path to the RUN key
lstrcpy( (char *) szFilePath , LPCTSTR(m_strFileName) );
GetDlgItemText( IDC_KEYNAME, sKeyName ); //Get value name
if( ERROR_SUCCESS == lnRes )
{
lnRes = RegSetValueEx(hKey,
LPCTSTR( sKeyName ), // handle of the opened
// key to set value for
0,
REG_SZ,
szFilePath, //value data
REG_SZ );
}
Downloads
Download demo project - 106 KbDownload source - 14 Kb

Comments
Java developer
Posted by Legacy on 07/15/2003 12:00amOriginally posted by: Yacin Ibrahim
I just want to call your function from Java Code but i don't know how to do it since i've ever done it before.
Thanks in advence.
-
ReplyJava Startup
Posted by hazle on 11/13/2005 12:50amIm pretty sure that you can't call any windows function from java as its platform independent, but you code make a small C++ program that calles the function that you can call from java.
ReplyUse better way to store the Data
Posted by Legacy on 11/30/2002 12:00amOriginally posted by: Jibesh
ReplyYou can do this...
Posted by Legacy on 10/16/2002 12:00amOriginally posted by: Marc
... but please think very hard before doing so.
Practically every program that is installed now runs some kind of background agent, or task bar icon, or both in conjunction.
It's really "cool" and all that, but have a care - your program takes resources, just like all those others under the same key. It's not uncommon to see these 'services' using all physical memory on a machine, which means that any program the user executes will cause swapping.
As an example, I just got a new XP laptop. The base operating system itself [including the unnecessary services running by default, which I won't go into here ;-] took about 96MB.
The installed application taskbar icons and background processes, such as:
- Real Player
- AntiVirus AGent
- AntiVirus Update Agent
- Quicken Update Agent
- MSN Messenger
- AOL QUickLaunch
- [the list goes on]
... took up ~140MB additional memory.
This means that on my nice, new, out-of-the-box 256MB system, I had 20MB free, before I even started any programs!
And people wonder why faster machines don't seem any faster.
Anyway, my point is to make sure you really, REALLY need to do this before making the problem worse.
- Marc
ReplyCan I run a program at each shutdown?
Posted by Legacy on 04/22/2002 12:00amOriginally posted by: Kotireddy
Hi, Can I run a backup program at each shutdown of my computer?
ReplyWhat about Windows NT?
Posted by Legacy on 01/02/2002 12:00amOriginally posted by: JN
This program run under Windows 9x what about Windows NT ?
Replyworks except.....
Posted by Legacy on 12/31/2001 12:00amOriginally posted by: Brian D
Reply