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
Printable View
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
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
Or far easier, add a shortcut to the All Users\startup dir
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
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;
}