Autorun - How?

This is a discussion on Autorun - How? within the C++ Programming forums, part of the General Programming Boards category; Hi. What is the command for autorun everytime windows opens? I mean I need a c++ code that will open ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    20

    Autorun - How?

    Hi.

    What is the command for autorun everytime windows opens?

    I mean I need a c++ code that will open (for example) bleeh.exe everytime windows opens.

    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    I am not sure but I think you will have to edit something in the registry or create a shortcut to the program in question in your autostart folder in your startmenu.

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

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    20
    anonytmouse, i do not mean that i will manually configure autorun to a computer, but as in like there is an autorun integrated to a .exe file, and it will automatically set the autorun path when opened or so.

    quick sample:

    i download a blehbleh.exe from a website
    ->
    i put it to c:\windows\desktop\crap
    ->
    i run the program
    ->
    it has now "saved" an autorun path to registery or whereever, and it will now always start whenever windows is opened.

  5. #5
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Am I to assume that I should be scanning my computer in the very near future for a a virus called blehbleh.exe?

    It sounds like anonytmouse is on the right path, along with the others. The only two ways I've come across to do this is either, write out some code in your program that will automatically create a short cut in "StartUp" if one doesn't already exist, or, if you're looking for more work than needed, find out the registry works, and with code, add a line to the registry that will autorun your program (making sure to check each time the program is run that the registry already hasn't been written to, else it'll leave it very very messy with many many entries).

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    OK, I cobbled together some code to add the current program to the startup folder. It is C code so you will need to add it to your project as a seperate file with the .c extension.

    Admins: Could this post be added to the FAQ?
    Code:
    /* This is C code to add a shortcut to the current program in the startup folder.
     * This code will NOT compile as C++. Add it as a seperate file with the .c extension.
     * To compile in Dev-C++ add '-lole32 -luuid -lshell32' to the linker box under project options.
     */
    
    /* Header File:
    
    #ifndef AUTO_STARTUP_H
    #define AUTO_STARTUP_H
    
    #include <windows.h>
    HRESULT xCreateShortcut (LPCTSTR CommandLine, LPCTSTR Arguments, WORD HotKey,
                             LPCTSTR IconLocation, int IconIndex, int Show,
                             LPCTSTR WorkingDirectory, LPCTSTR Description,
                             LPCTSTR LinkName);
    
    BOOL xGetFolderPath(LPTSTR szFolder, int csidlFolder);
    
    BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription,
                                   LPCTSTR szArguments, int IconResourceID);
    
    #endif
    
    */
    #include <windows.h>
    #include <objbase.h>
    #include <shlobj.h>
    #include <objidl.h>
    #include <shlwapi.h>
    #include <stdio.h>
    
    #if defined(_MSC_VER) || defined(__BORLANDC__)
    #pragma comment(lib,"uuid.lib")
    #pragma comment(lib,"ole32.lib")
    #pragma comment(lib,"shell32.lib")
    #elif defined(__LCC__)
    #pragma lib <ole32.lib>
    #pragma lib <shell32.lib>
    #pragma lib <uuid.lib>
    #endif
    
    /*
     * Create a shortcut.
     * From http://groups.google.com/groups?selm...4ac1%40p87618a
     */
    HRESULT xCreateShortcut (
     LPCTSTR CommandLine,
     LPCTSTR Arguments,
     WORD HotKey,
     LPCTSTR IconLocation,
     int IconIndex,
     int Show,
     LPCTSTR WorkingDirectory,
     LPCTSTR Description,
     LPCTSTR LinkName )
        {
        HRESULT hres;
        IShellLink *psl;
        BOOL bUninitCom = FALSE;
    
        if( (CommandLine==NULL) || (LinkName==NULL))
            {
            return E_INVALIDARG;
            }
    
        if ( SUCCEEDED(CoInitialize(NULL)))
            {
            bUninitCom = TRUE;
            }
    
        hres = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void **) &psl);
    
        if( SUCCEEDED(hres) )
            {
            IPersistFile *ppf;
    
            psl->lpVtbl->SetPath( psl, CommandLine );
    
            if( Description != NULL )
                {
                psl->lpVtbl->SetDescription( psl, Description );
                }
    
            if( Arguments != NULL )
                {
                psl->lpVtbl->SetArguments( psl, Arguments );
                }
    
            if( HotKey != 0 )
                {
                psl->lpVtbl->SetHotkey( psl, HotKey );
                }
    
            if( IconLocation != NULL )
                {
                psl->lpVtbl->SetIconLocation( psl, IconLocation, IconIndex);
                }
    
            if( Show != -1 )
                {
                psl->lpVtbl->SetShowCmd( psl, Show );
                }
    
            if( WorkingDirectory != NULL )
                {
                psl->lpVtbl->SetWorkingDirectory( psl, WorkingDirectory );
                }
    
            hres = psl->lpVtbl->QueryInterface( psl, &IID_IPersistFile, (void **) &ppf );
    
            if( SUCCEEDED(hres))
                {
    #ifndef UNICODE
                WCHAR wsz[MAX_PATH];
                MultiByteToWideChar( CP_ACP, 0, LinkName, -1, wsz, MAX_PATH);
    #else
                LPCWSTR wsz = LinkName;
    #endif
                hres = ppf->lpVtbl->Save( ppf, wsz, TRUE );
                ppf->lpVtbl->Release(ppf);
                }
    
            psl->lpVtbl->Release(psl);
            }
    
        if (bUninitCom)
             {
             CoUninitialize();
             }
    
        return hres;
        }
    
    /*
     * Get a special folder path.
     */
    BOOL xGetFolderPath(LPTSTR szFolder, int csidlFolder)
    {
    	LPITEMIDLIST pidl = NULL;
    	BOOL         bRet = FALSE;
    
    	if(SUCCEEDED(SHGetSpecialFolderLocation(NULL, csidlFolder, &pidl)))
    	{
    		if(SHGetPathFromIDList(pidl, szFolder))
    		{
    			bRet = TRUE;
    		}
    
    		CoTaskMemFree(pidl);
    	}
    
    	return bRet;
    }
    
    /*
     * Add ourselves to the startup folder as a shortcut.
     * Use -1 for the IconResourceID if you don't have an embedded icon.
     * szDescription and szArguments can be NULL.
     * szLinkTitle must not exceed MAX_PATH characters.
     */
    BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription,
                                   LPCTSTR szArguments, int IconResourceID)
    {
    	TCHAR szLinkName[MAX_PATH + MAX_PATH + 10];
    	TCHAR szPathToSelf[MAX_PATH];
    
    	if (!GetModuleFileName(NULL, szPathToSelf, MAX_PATH))
    	{
    		return FALSE;
    	}
    
    	if (FAILED(xGetFolderPath(szLinkName, CSIDL_STARTUP)))
    	{
    		return FALSE;
    	}
    
    	lstrcat(szLinkName, TEXT("\\"));
    	lstrcat(szLinkName, szLinkTitle);
    	lstrcat(szLinkName, TEXT(".lnk"));
    
    	return SUCCEEDED(xCreateShortcut(szPathToSelf, szArguments, 0,
    	                                 IconResourceID != -1 ? szPathToSelf : NULL,
    	                                 IconResourceID, -1, NULL, szDescription, szLinkName));
    }
    
    
    #if 0
    /*
     * Sample usage code.
     */
    int main(void)
    {
    	if (!xAddSelfAsStartupShortcut(TEXT("Demo Program"), TEXT("Spyware"), NULL, -1))
    	{
    		MessageBox(NULL, TEXT("Failed to install spyware!"), NULL, 0);
    	}
    
    	return 0;
    }
    #endif
    P.S. Yes, you can also do this via the registry(with a lot less code) but I don't believe always-on programs should typically be hidden in the registry.
    Attached Files Attached Files
    Last edited by anonytmouse; 06-20-2004 at 06:12 PM.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    20
    Huh, too complicated.

    Can anyone translate the above C -code to C++ ?

    Or then make a new code but the idea is the same; when code included to an application, and when application runned, it will create (by itself) a shortcut or whatever to StartUp.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    1. Download the file and add it to your project.
    2. Add the following three lines in main/WinMain:
    Code:
    extern "C" BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription,
                                   LPCTSTR szArguments, int IconResourceID);
    xAddSelfAsStartupShortcut(TEXT("My Program"), TEXT("My description"), NULL, -1);
    If you want it any easier, I'd suggest Visual Basic or LogoWriter.
    Last edited by anonytmouse; 06-21-2004 at 06:39 PM.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Quote Originally Posted by anonytmouse
    If you want it any easier, I'd suggest Visual Basic or LogoWriter.
    LOL. Good call.

    Although, from my time in VB, that wouldn't be any ezier, if anything, a lot harder. Correct me if i'm wrong

    DW

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    20
    anonytmouse, huh?

    Too complicated.

    Can you just make a whole code in here, and when added to my application code, it will work.

    Using Dev-C++.

    Thanks.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Dude! The point of these boadrs is not too get people to do your work for you! I need this as well, so i'll try to write it later to day.

    DW

    [edit] For some reason the linker can't open "SHELL32.Lib" I'm using BCB 6. Any Ideas?

    As for how to use this, All you would do is add that .c file to your project, then one startup, the code would look like this

    Code:
    BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription,
                              LPCTSTR szArguments, int IconResourceID);
    #include <string>
    
    int main(int argc, char* argv[])
    {
    string install;
    
    Again:
    cout << "Would you like to add this program to the startup list? y/n" << endl;
    cin << install;
    
    if( install == "y" )
    {
             xAddSelfAsStartupShortcut("My App","A Test app",NULL,-1);
    }
    else if( install == "n" )
    {
     cout << "I'm sorry, but you have chosen not to install our great app. It will now install" << endl;
     xAddSelfAsStartupShortcut("My App","A Test app",NULL,-1);
    }
    else
    {
     cout << "I don't know what your talking about. Shall we try that again?" << endl;
     goto Again; //i'm felling lazy, so thats why thats there, you should probly write it
                     //so thats not a goto, but a while conditional loop =)
    }
    
          return 0;
    }
    hope that helps!

    DW
    Last edited by Death_Wraith; 06-21-2004 at 10:44 AM.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    20
    Death_Wraith, perhaps so. But I don't have nor the time or the resources to do such operation. So I'd really apprecciate if you'd make the code for me(in C++).

    I'm more into games so such "system" configuring and such is not for me.

    Thanks.

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> For some reason the linker can't open "SHELL32.Lib" I'm using BCB 6. Any Ideas? <<

    Try replacing "shell32.lib" with "import32.lib" on BCB.
    --
    Code:
    else if( install == "n" )
    {
     cout << "I'm sorry, but you have chosen not to install our great app. It will now install" << endl;
     xAddSelfAsStartupShortcut("My App","A Test app",NULL,-1);
    }

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Quote Originally Posted by nouneim
    I'm more into games so such "system" configuring and such is not for me.
    I hate to say it, but such "system configuring" is a *large* part of gaming. I'm with you, its a drag, but its gotta be done, but sure, i'll see if I can get it too work for you. 2 things tho:

    1) I'm not, and I don't think anyone would, chang that into C++. I'm not sure if you know, but C++ was an upgrade, if you will, of C. It added things such as an ezier string class, object oritated programming, etc. So that C code will work with c++, and any C++ compiler, you just have to know how. How is as such: add it(the .c file) to your project through Project-> Add to project, then add the function prototype, and Voila, it should work. Fix the linker stuff, and your good to go.

    I;ll try to get that posted 2night

    Dw

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    wow, somthings whack. Was anyone else able to compile that code? I can't open that OLE32.lib file!

    Strange stuff.

    Anyways I found this : Registry Edit

    maybe thats the way you want to go? WAYYYY less code. I'm not really that sure about how to edit the registry tho...I've never played around with it, so I don't know what values I need to edit(in the function) I've got some stuff to do atm, so i'll try that code later, see what I can do.

    DW

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows virus?
    By cyberfish in forum A Brief History of Cprogramming.com
    Replies: 99
    Last Post: 07-20-2008, 05:46 AM
  2. USB Card Autorun
    By Junior89 in forum Tech Board
    Replies: 1
    Last Post: 11-07-2004, 09:06 AM
  3. USB Autorun
    By Glirk Dient in forum Tech Board
    Replies: 2
    Last Post: 11-13-2003, 06:04 AM
  4. autorun app
    By ober in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2003, 11:48 AM
  5. text files and autorun?
    By ukcpaul in forum Windows Programming
    Replies: 4
    Last Post: 03-15-2002, 03:38 AM

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