Thread: Registry Startup Key

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Registry Startup Key

    Hey,

    Can someone tell me how i can add a key to the regisitry telling my app to start with windows. I cant remember the location in the regisitry for this, but the apps path is "c:\program files\Swapper\swapa.exe"

    Thanks alot,
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    www.regedit.com

    The key is

    (I think "windows" below should be "winnt" on an NT machine)

    [under local machine or current user whichever suits your needs mostly use current user if you don't want everyone on the machine to load the program.

    Software/microsoft/windows/current version/run

    Software/microsoft/windows/current version/runservices

    Software/microsoft/windows/current version/runounce

    you should use "run" as "run services" executes first but should be used sparingly. "runonce" is as it says runs the app once.

    what to do is create a string value in these keys called whatever you like abd the data of the string is your programs full path.

    Check out www.regedit.com for the best guide to windows registry

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Or far easier, add a shortcut to the All Users\startup dir

  4. #4
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey thanks,

    Fordy i would use that method, but its just OSs like win95/98 dont have allusers/startup, and it could be running on any OS, so i assumed i should use the registry.

    Do you have any code to add the key? I think i need to use RegCreateKeyEx() but i am not sure how to use it.

    Thanks alot,
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I sat down tonight and did the following....The proram works out the OS version, and then creates a shortcut in the startup folder....the example given is to a simple shell script (test.wsf) that doesnt do much interesting stuff...so substitute this with your proggie

    MAJOR DISCLAIMER - I was unable to test this on any other system apart from Win2K....though it should work ok with XP,95,98 and I guess NT, though some tweaking by someone with those systems may remove some bugs.

    I started by using the GetVersionEx() API to see what OS I am working on...I wrapped it in a Is2000Plus() function..this shoulddo what we need, but may need further tweaking....If its 2000 or XP, a shortcut will be created in "C:\Documents and Settings\All Users\Start Menu\Programs\Startup"....if its Win98 it will be "C:\Windows\Start Menu\Programs\Startup"......So with a bit of luck it should run on both systems at startup!

    The I use 2 COM interfaces to allow me to create a shell link (shortcut) in memory and then save it to disk....dont get too worried about the COM.....

    Have a play with it!

    Code:
    #include <windows.h>//Essential stuff
    #include <objbase.h>//get COM up and running
    #include <Shlobj.h>//for interfaces
    
    BOOL Is2000Plus(void){
    	OSVERSIONINFO ovi = {0};
    
    	ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    
    	GetVersionEx(&ovi);
    	if(ovi.dwMajorVersion >= 5)
    	/*Win 2000 & XP are Major Version 5, 95 & 98 are Major Version 4
    	I have not taken into account ME or NT as I am unfamiliar with them*/
    		return TRUE;
    	return FALSE;
    }
    
    int WINAPI WinMain(HINSTANCE hThisInstance, 
    				   HINSTANCE hPrevInstance,
    				   LPSTR lpszArgument, 
    				   int nFunsterStil)
    {
    	CHAR lpstrTargetPath[] = "c:\\test.wsf",
    		 lpstrDescription[] = "Phew...this is easy in VBScript!!!",
    		 lpstrLinkPath[MAX_PATH+1];
    	WCHAR lpwstrLinkPath[MAX_PATH+1]; //to store shell link path in Unicode
    	IShellLink* Isl; //Needed to create shortcut in memory
    	IPersistFile* Ipf; //Needed to store that shortcut on disk
    
    	
    	if(Is2000Plus()){ //Which OS???!?
    		wsprintf(lpstrLinkPath,//2000
    			"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\Run.lnk");
    	}
    	else{
    		wsprintf(lpstrLinkPath,//98
    			"C:\\Windows\\Start Menu\\Programs\\Startup\\Run.lnk");
    	}
    	
    	
    	HRESULT hRes = CoInitialize(NULL); //Essential COM nonsense
    	if (!SUCCEEDED(hRes)){
    		MessageBox(HWND_DESKTOP,"Could not initialise COM",
    			"Error",MB_OK);
    		return 1;
    	}
    
    	hRes = CoCreateInstance(CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &Isl);
    							//Get interface to shell links
    	if (!SUCCEEDED(hRes)){
    		MessageBox(HWND_DESKTOP,"Could not get shell link interface",
    			"Error",MB_OK);
    		return 1;
    	}
    
    	Isl->SetPath(lpstrTargetPath); //target
        Isl->SetDescription(lpstrDescription); //weak description
    	
    	hRes = Isl->QueryInterface(IID_IPersistFile, 
                (LPVOID*)&Ipf); //Jump up to the persistant file interface
    	if (!SUCCEEDED(hRes)){
    		MessageBox(HWND_DESKTOP,"Could not get pers. file interface",
    			"Error",MB_OK);
    		return 1;
    	}
    
    	MultiByteToWideChar(CP_ACP, 0, lpstrLinkPath, -1, 
                    lpwstrLinkPath, MAX_PATH+1); //COM uses Unicode for this stuff
    
    	hRes = Ipf->Save(lpwstrLinkPath, TRUE);//save the link to disk
    		if (!SUCCEEDED(hRes)){
    		MessageBox(HWND_DESKTOP,"Could not get save to disk",
    			"Error",MB_OK);
    		return 1;
    	}
        Ipf->Release(); //release interfaces
        Isl->Release(); 
    	CoUninitialize(); //bye bye COM!
    
    	return 0;
    }
    Last edited by Fordy; 06-14-2002 at 06:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM