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 Kb
Download source - 14 Kb


Comments

  • Neutral write-up reveals Five brand-new stuff regarding mizuno that absolutely no one is covering.

    Posted by BobHotgloff on 05/16/2013 06:33pm

    A powerful solid double change on swimwear [url=http://plus-size-swimwear.webnode.com/]women s swimwear[/url] Abnormal piece of content provide you with the key points on the swimwear that just a couple of guys know. [url=http://modest-swimwear.webnode.com/]speedo swimwear[/url] Unprejudiced review unwraps 3 new things around bikini that no one is covering. [url=http://spanx-swimwear.webnode.com/]vintage swimwear[/url] As to the reasons so many people are extremely wrong on the subject off swimwear and also the reasons why you should ought to see this study. [url=http://vitamin-a-swimwear.webnode.com/]vitamin a swimwear[/url] For what reason almost everything you might have read about bikini is truly entirely wrong and exactly what you need be aware of. Innovative new swimwear Book Presents Best Ways To Rule The swimwear Marketplace [url=http://girls-swimwear.webnode.com/]womens swimwear[/url] Hot questions regarding swimwear addressed and therefore the reasons why you should really take a look at each concept within this document. [url=http://freya-swimwear.webnode.com/]gottex swimwear[/url] The things everybody else does for the swimwear and furthermore things that you might want to perform totally different. [url=http://victoria-secret-swimwear.webnode.com/]victoria secret swimwear[/url] Innovative swimwear E-book Explains Ideal Way To Dominate The swimwear Market [url=http://cheap-swimwear.webnode.com/]cheap swimwear[/url] Innovative document tells the low down of swimwear together with the reason why you have got to take action right away.[url=http://women-swimwear.webnode.com/]tan through swimwear[/url] Unique report offers the important points on swimwear which experts claim just one or two persons are conscious of.

    Reply
  • Java developer

    Posted by Legacy on 07/15/2003 12:00am

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

    • Java Startup

      Posted by hazle on 11/13/2005 12:50am

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

      Reply
    Reply
  • Use better way to store the Data

    Posted by Legacy on 11/30/2002 12:00am

    Originally posted by: Jibesh

    if( ERROR_SUCCESS == lnRes )
    
    {
    lnRes = RegSetValueEx(hKey,
    LPCTSTR( sKeyName ),
    0,
    REG_SZ,
    szFilePath, //value data
    REG_SZ );
    }

    and as it should be. This should now show the correct value when written to the registry.

    if( ERROR_SUCCESS == lnRes )
    {
    lnRes = RegSetValueEx(hKey,
    LPCTSTR( sKeyName ),
    0,
    REG_SZ,
    szFilePath, //value data
    sizeof(szFilePath) );//May Cause pbm
    }

    its Better way to keep
    the buffer length that the size of the Varible in this case szFilePath

    if( ERROR_SUCCESS == lnRes )
    {
    lnRes = RegSetValueEx(hKey,
    LPCTSTR( sKeyName ),
    0,
    REG_SZ,
    szFilePath, //value data
    strlen(szFilePath) );// This will work Better
    }

    Reply
  • You can do this...

    Posted by Legacy on 10/16/2002 12:00am

    Originally 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

    Reply
  • Can I run a program at each shutdown?

    Posted by Legacy on 04/22/2002 12:00am

    Originally posted by: Kotireddy

    Hi, Can I run a backup program at each shutdown of my computer?

    Reply
  • What about Windows NT?

    Posted by Legacy on 01/02/2002 12:00am

    Originally posted by: JN

    This program run under Windows 9x what about Windows NT ?

    Reply
  • works except.....

    Posted by Legacy on 12/31/2001 12:00am

    Originally posted by: Brian D

    the value defaults to the first item in the list. In the SetRegValueEx you have given the length field a value of REG_SZ.... see below for what I mean:
    
    

    if( ERROR_SUCCESS == lnRes )
    {
    lnRes = RegSetValueEx(hKey,
    LPCTSTR( sKeyName ),
    0,
    REG_SZ,
    szFilePath, //value data
    REG_SZ );
    }

    and as it should be. This should now show the correct value when written to the registry.

    if( ERROR_SUCCESS == lnRes )
    {
    lnRes = RegSetValueEx(hKey,
    LPCTSTR( sKeyName ),
    0,
    REG_SZ,
    szFilePath, //value data
    sizeof(szFilePath) );
    }

    Hope this helps...

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Open source and cloud computing have long promised cost efficiencies, yet many organizations have shied away from these technologies due to security and reliability concerns. Now, open source has proven itself stable, and the cloud has become as secure—or even more secure—than on-premises implementations. Read this white paper to learn how you can get the accessibility of open source and the flexibility and affordability of cloud computing combined in a compelling storage option for companies …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds