make my program start on startup.

This is a discussion on make my program start on startup. within the Windows Programming forums, part of the Platform Specific Boards category; My program got an option to start it every time you turn on the pc. The problem is that I ...

  1. #1
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90

    make my program start on startup.

    My program got an option to start it every time you turn on the pc.
    The problem is that I got no idea how to do it.
    I know you can put a .job file in C:\WINDOWS\Tasks, but how would I go on making a .job file?
    Any other easier way?

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    There are some registry start-up options that you can do. Check out how programs like Sub7 and the like start up and you'll see what I mean.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > Check out how programs like Sub7
    Next time you feel like mentioning trojans as a way of doing things, forget it

  5. #5
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Alright why I get errors here?
    I did exactly as it said.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        char x;
        
        cout << "A) Add this program to startup";
        cout << "B) Exit \n\n";
        
        cin >> x;
        
        if(x == 'a')
        {
            extern "C" BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription, LPCTSTR szArguments, int IconResourceID);
            xAddSelfAsStartupShortcut(TEXT("My Program"), TEXT("My description"), NULL, -1);
    
            return 0;
        }
        
        if(x == 'b')
        {
             return 0;
        }
        
        else
        {
            cout << "Wrong input! \n\n";
            main();
        }
        
        return 0;
    }
    Pictures:
    I got the startup code added to my project as a .c file
    I got the linker options added to the project
    The errors

    Whats wrong?
    I used anonytmouse's code from his link.
    Last edited by XunTric; 01-11-2006 at 06:51 AM.

  6. #6
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Alright I did something wrong.
    I needed this at the top instead:
    Code:
    extern "C" BOOL xAddSelfAsStartupShortcut bla bla bla...
    But Im still getting errors!
    How I fix this?
    Picture
    (From the .c file)

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    something like this.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    #define RUN_KEY "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
    
    
    void addRun(char *valueName, char *valueData);
    
    int main(void)
    {
        addRun("ProgName", "c:\\myprog.exe");
        
        return 0;
    }
    
    void addRun(char *valueName, char *valueData)
    {
        HKEY key;
    
        
        if (RegOpenKeyEx(HKEY_CURRENT_USER, RUN_KEY, 0, KEY_WRITE, &key) 
                != ERROR_SUCCESS) {
            /* Unable to open registry key */
            return;
        }
        
        if (RegSetValueEx(key, valueName, 0, REG_SZ, valueData, strlen(valueData) + 1)
                != ERROR_SUCCESS) {
            /* Unable to set registry value */
            RegCloseKey(key);
            return;
        }
        
        RegCloseKey(key);  
    }

  8. #8
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    That adds it to the registry?
    Well, how I remove it again then?

    And for my program:
    "c:\\myprog.exe"
    How I know where the ones using my program having it installed there?

    Just tell me how to fix the startup folder code instead.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    That startup code doesn't need fixing. It's in C. C++ is just an overly hyped addition to C, with object oriented programming and some nifty STL things.

    For such a simple group of functions, the addition of OOP does nothing for you. This is why using C is as good, if not better than using C++ for these functions. You might want to utilize the string class, but aside from that I see no reason to convert anything in that program to C++.

    And for my program:
    "c:\\myprog.exe"
    How I know where the ones using my program having it installed there?
    As for this... I'm not sure what you're asking. Do you want to know if users will be installing it to c:\myprog.exe ? Or do you want to know what the current location of your file will be?

    If you want to know the current location of the file, it'll be a parameter of int main(), called argv[0].
    Last edited by Tronic; 01-11-2006 at 03:37 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by XunTric
    Alright I did something wrong.
    I needed this at the top instead:
    Code:
    extern "C" BOOL xAddSelfAsStartupShortcut bla bla bla...
    But Im still getting errors!
    How I fix this?
    Picture
    (From the .c file)
    Dev-C++ sometimes insists on compiling .c files as C++. This can be easily fixed. Goto Project->Project Options->Files->Click on the .c file->Make sure the 'Compile file as C++' option is not ticked.
    Attached Images Attached Images  

  11. #11
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Thanks!
    Compiles and works!
    First time I got a really good answer. =P
    But how would I remove it from starting when my pc starts up?

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    There is a function in the linked thread (below). It should be added to the bottom of the .c file.
    Code:
    // In the .cpp file
    extern "C" BOOL xDeleteSelfAsStartupShortcut(LPCTSTR szLinkTitle);
    Code:
    // At the bottom of the .c file.
    BOOL xDeleteSelfAsStartupShortcut(LPCTSTR szLinkTitle)
    {
    	TCHAR szLinkName[MAX_PATH + MAX_PATH + 10];
    
    	if (FAILED(xGetFolderPath(szLinkName, CSIDL_STARTUP)))
    	{
    		return FALSE;
    	}
    
    	lstrcat(szLinkName, TEXT("\\"));
    	lstrcat(szLinkName, szLinkTitle);
    	lstrcat(szLinkName, TEXT(".lnk"));
    
    	return DeleteFile(szLinkName);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make a program run at startup
    By cookie in forum Tech Board
    Replies: 2
    Last Post: 06-08-2007, 08:36 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. how to make my program run on windows start up
    By kantze in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-24-2007, 11:14 AM
  4. Programmer Cannot Make Any Program!
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2002, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21