Run At Startup Programmatically

Monday Dec 31st 2001 by Mohammad A. Salah
Share:

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.

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 Kb
Download source - 14 Kb
Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved