Add your program as a startup item

hahanottelling 0 Tallied Votes 2K Views Share

If you want your program to startup automatically when the user logs in, just paste this code into it.

To add as a HKLM (For all users) startup item instead of HKCU (Current User), change HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE.

I'm posting this because I had a hard time figuring this out myself, and hopefully this will save some people a lot of searches and time.

[CODE]TCHAR exepath[MAX_PATH];
GetModuleFileName(0, exepath, MAX_PATH);
    HKEY hKey;
    LONG lnRes = RegOpenKeyEx(
           HKEY_CURRENT_USER,
           "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
           0,KEY_WRITE,
           &hKey
       );
if( ERROR_SUCCESS == lnRes )
{
   lnRes = RegSetValueEx(hKey,
                         "YourProgramsName",
                         0,
                         REG_SZ,
                         exepath,
                         strlen(exepath));
}[/CODE]
amit_barnwal08 0 Newbie Poster

i think it will work but i am not sure

hahanottelling 0 Newbie Poster

I know it works. I use Dev-C++, so in VC++ it may need a few changes but in Dev it works great. What makes you "not sure"?

Denstrom 0 Newbie Poster

On this line:
strlen(exepath));

I get an error stating:

"invalided conversion from 'TCHAR*' to 'const BYTE*'

any ideas?

hahanottelling 0 Newbie Poster

I haven't been using C++ in the year+ since posting this. That said, I don't know how to fix that error. Perhaps someone more knowledgeable of C++ here could help?

I did find that the error doesn't happen when compiling as a C project in the latest version of Dev-C++. Compilable source below. (Will compile as C, will not as C++).

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>

int main(int argc, char *argv[])
{
    TCHAR exepath[MAX_PATH];
    GetModuleFileName(0, exepath, MAX_PATH);
    HKEY hKey;
    LONG lnRes = RegOpenKeyEx(
           HKEY_CURRENT_USER,
           "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
           0,KEY_WRITE,
           &hKey
       );
       if( ERROR_SUCCESS == lnRes )
       {
          lnRes = RegSetValueEx(hKey,
                         "YourProgramsName",
                         0,
                         REG_SZ,
                         exepath,
                         strlen(exepath));
                         }
  
  system("PAUSE");
  return 0;
}
hahanottelling 0 Newbie Poster

Having now made myself look like a fool, here's how to fix that and any other error I can find when compiling in the latest version of Dev-C++:

Replace

exepath,
strlen(exepath));

With this:

(BYTE*) exepath,
_tcslen(exepath));

Revised snippet:

TCHAR exepath[MAX_PATH];
    GetModuleFileName(0, exepath, MAX_PATH);
    HKEY hKey;
    LONG lnRes = RegOpenKeyEx(
           HKEY_CURRENT_USER,
           "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
           0,KEY_WRITE,
           &hKey
       );
       if( ERROR_SUCCESS == lnRes )
       {
          lnRes = RegSetValueEx(hKey,
                         "YourProgramsName",
                         0,
                         REG_SZ,
                         (BYTE*) exepath,
                         _tcslen(exepath));
                         }

Although there is no error when compiling as a C project, this would probably be better to use. Also make sure you

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
cyther 0 Newbie Poster

i can't put it in HKEY_LOCAL_MACHINE, but in HKEY_CURRENT_USER it is fine, why is that?

hahanottelling 0 Newbie Poster

HKEY_LOCAL_MACHINE affects the whole computer - all user accounts. As such, it requires administrative permission to set keys in it. If you are running windows Vista or 7, you will need to right-click, "Run as Administrator". If you are using Windows XP as a standard user, you will need to either use "Run As" or logout and then login as an Administrator.

HKEY_CURRENT_USER is specific only to the current user. As such, it can be expected that except in a few unusual circumstances, the program will have permission to create and modify keys within it.

There is a way to make your program request extra permissions when it runs (the UAC prompt), though I do not know of how to implement it. Perhaps there is a thread here about it.

If any mod or such is reading this, would you please edit the snippet at the top with my revised snippet in my post above this? Thanks.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Why not just make an icon for the program and put the in the STARTUP folder of the program list?

This way you won't destroy your registry file if you get the code wrong.

hahanottelling 0 Newbie Poster

For me personally, there are a number of reasons why using the registry is preferable.

one of them is that the registry location is consistent. In order to use the startup folder properly, you must read it's location out of the registry anyways. (Or mess with system/environment variables or use a windows api call.) The location of the startup folder is easily changed, and it may be somewhere on the disk your program doesn't have permission to write to.

Also, it's not that easy to "destroy your registry file". Look at the code. It opens a key, checks if there was an error, and then sets a value. That's it. There is no magic, "oh I messed up 1 character and now my registry is destroyed".

I've written a complete registry editor, in C++. The registry is very useful, something you should learn about rather than shy away from.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I agree -- for experienced users that understand the O/S. I myself edit the registry often. But for new programmers, it's extremely dangerous. If it's so safe, why does every registry hack include a disclaimer... (objection sustained)

Yes, your code checks for errors. 99% of posts on these forums asking for help don't. I would not suggest registry programming except to experts that already utilize safe coding practices.

My 2 cents...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.