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.
Printable View
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.
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.
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.
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).
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?
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.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
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.
1. Download the file and add it to your project.
2. Add the following three lines in main/WinMain:
If you want it any easier, I'd suggest Visual Basic or LogoWriter.Code:extern "C" BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription,
LPCTSTR szArguments, int IconResourceID);
xAddSelfAsStartupShortcut(TEXT("My Program"), TEXT("My description"), NULL, -1);
LOL. Good call.Quote:
Originally Posted by anonytmouse
Although, from my time in VB, that wouldn't be any ezier, if anything, a lot harder. Correct me if i'm wrong
DW
anonytmouse, huh?
Too complicated. :p
Can you just make a whole code in here, and when added to my application code, it will work.
Using Dev-C++.
Thanks.
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
hope that helps!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;
}
DW
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.
>> 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.
--
:D :pCode: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);
}
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:Quote:
Originally Posted by nouneim
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
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
The code compiles in Dev-C++, Visual C++ and LCC-WIN32. I don't know the library system that Borland uses but try getting rid of all the "#pragma comment(lib, ...)" lines and using import32.lib instead.
Ya, my bad, I was just being dumb. Sry. How do I get a .CPP file to compile with a .c file? It cann't find the function, and if I put a prototype up, it says Undeclared external sonthing morother. So i'm trying to build a DLL, but since i've never built one, its going slowly!
i get an error here:
DWCode:extern "C" __declspec(dllexport)BOOL xAddSelfAsStartupShortcut(LPCTSTR szLinkTitle, LPCTSTR szDescription,
LPCTSTR szArguments, int IconResourceID);
Here is the Dev-C++ project. I don't know the details of the Borland compiler but I would check if it supports __declspec(dllexport). You should be able to just add the .c file to your project. Good pickup on the extern "C", which was missing.
I gave up on the DLL. I just got it to work with the extern "c" part. I can't believe i forgot it. I'll post the BCB project incase anyone needs it.
I have a question though, how do you remove it? :D
DW
Code: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);
}
"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."
- "the .c file" hmm. Can I just create this manually to "My projects" -folder? 'Cause I really didn't understand the above, except for the linker -part.
Or can you attach "the .c file" to this forum so I can just download it and add it to "My projects" -folder?
Thanks for any assistance.
Huh, didn't even notice there was a second page. :p
Well, so. I'll try to work something out but I still prob. need an attached project with the autorun and then I'll just edit it.
"need an attached project with the autorun and then I'll just edit it."
Yep, tried everything. So can somebody attach a project here which is a "complete example" that includes the whole project with an application of your own, and then I'll just change the application code and phuff. :>
Getting an error from this line in Dev-C++:
hres = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void **) &psl);
Reason:
71 C:\Program Files\Dev-C++\My Projects\AutoRun.c
could not convert `&CLSID_ShellLink' to `const CLSID&'
So what to do?
Alright, now I've done it with Borland. But I only have the autostartup, so how can i add my program there?
And i want to take that "add"/"exit" off.
I just want one program, and when started, it will first put a shortcut to StartUp, and then it will just open my own program.
I mean i want this shortcut-thing to be so-called function, and the main program is my program.
So how can I do this with borland?
5 concutive post! OMG! I think you beat my record! lol
I have a deal for you. What compiler are you using? Dev-C++ or Borland C++ Builder? If you send me your project, add it for you. PM me..
DW
Uh, huh, buuh. This forum is way too complicated for built/help instructions.
Come to IRC(internet relay chat) on Quakenet, channel: #bleehbleeh
If you do not have irc, we could also go to somekind of a chat on web.
How do I get this too work in dev-c++? I really don't have much experiance with it. It works fine in BCB.....
I add the .c file, add '-lole32 -luuid -lshell32' to the linker parameters, then extern "C" it.
i get a linker error as such:Fatal E:\MyDocs\Chris\AutoRun Example\Fatal
'all-before' does not exist - don't know how to make it
cheers
DW
PS: that removal code works fine too! :D thx!
Not to be harsh, but if you don't understand how to remove the yes/no question from the example program then this is way too advanced for you and you should go back into studying if-statements.Quote:
Originally Posted by nouneim
Heh.Quote:
Originally Posted by nouneim
"Not to be harsh, but if you don't understand how to remove the yes/no question from the example program then this is way too advanced for you and you should go back into studying if-statements."
To be exact, I compiled the program with BCB, which I had never used before, so I couldn't even get in touch with the file that had the code.
And maybe so, maybe I am a newbie, yet the best way to learn is to get a code and start editing it and look the commands from a c++ -book, which is a bit more difficult for me 'cause the book I have is finnish-translated and all the examples are in finnish. Also I have never before had in touch with system-settings nor anything like it.
But what am I defending myself for? So then I am a newbie, ohmigosh.
1) I have to admit, its pretty simple to edit! :D not *that* hard
2) to be fair, i've asked *wayyyy* dumber questions before, so I don't really care.
non: I'll get that to you tonight or 2morrow
DW