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;
}