Hi.

I was wondering about how you might go about executing a file as you log in to your account. For instance, I want to run a file that prompts the user of the date, and any other information as you log-in. Might I need a registry entry or such?
EDIT: I would like this to be performed after a complete restart. i'm using XP


Thanks,

Use The Scheduled Tasks Option in the Control Panel to start executing the file(program) at startup.

What happnens if I want to execute this on another computer, which hasn't got this enabled? What should I do then?

just throw the file in the startup folder. or make a shortcut to your executable in the startup folder.

(The startup folder is located in the start menu: C:\Documents and Settings\All Users\Start Menu\Programs\Startup)

Ok, so how would I copy the file across to the folder using code?

Thanks

You could use system("copy ...") you can find the name of the executable within your code also: argv[0]

What does argv[0] do? And where should I use it? Also, what declaration do you need for a system command?

argv[0] is command line argument #0 when your program is run. It is always the name of your executable. If you use that you should be able to copy your file to a new location without hard-coding the file name into your program.
And you shouldn't need to declare anything for system. I assume that you have included iostream, yes?

alternatively you could open the executable as a stream and write that to a new file. However, that is more complicated.

Do you have any coding I could use to carry out a simple task of copying the .exe into the startup folder?

Thanks

use the schtasks command to setup the program to be executed at startup.

You know, cosmos22, playing around with another person's computer really isn't funny. In fact, it is the kind of crap warez dorks and wannabes do.

Why don't you do something that will be both appreciated and cool?

Like write a simple video game.
Or write a cool screen saver you and your friends can use.

Both of these are impressive tasks.

the syntax for schtasks command is--

Schtasks /change /tn TaskName [/s Computer [/u [Domain\]User [/p
Password]]] [/ru {[Domain\]User | System}] [/rp Password] [/tr TaskRun] [/st
StartTime] [/ri Interval] [{/et EndTime | /du Duration} [/k]] [/sd StartDate]
[/ed EndDate] [/{ENABLE | DISABLE}] [/it] [/z]

Maybe cosmos22 does not have a malicious intent. There are legitimate reasons to execute a file on startup. I've certainly done it before.

I didn't say he had malicious intent. (In fact I don't really think he does.)

What I said was he'd have more fun doing something constructive.

I certainly don't intend on releasing my code in any way, I purely do it for educational purposes, for my own interests. I'm not stupid enough to release these files over the internet.

prushik, you say you've done this sort of thing before, could I please take a look at an example of your source code, as I'm still not sure.

Thanks

EDIT oh, and Duoas, I have written several flash based room escape style games before. I have also made money from sponsorship for each one.

If you are still interested, here is working code, I just wrote it.

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char cp[500]; 
    sprintf(cp, "copy %s \"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\\"", argv[0]);
    system(cp);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

What I said was he'd have more fun doing something constructive.

My point was, maybe his purposes are constructive, you don't know. I wrote useful programs that do similar things.

Thank you, I will test it out now. :) I'll let you know quickly if I come across an error... And yes, I have only constructive purposes alongside my programs.

Oh, by the way cosmos22: It would be a good idea to check to make sure that your file is not already in the startup folder before copying. You wouldn't want to try to copy the file every time you start up your computer or log on.

Wow, it works brilliantly. Thank you! :)
From then on, when you boot the computer up, will it run the program?
For instance, if I made a program that displayed a message saying "Hello" that was inside the folder, it would boot it up?

Thanks again, :)

Ok, cool. So everytime I boot my computer, it will copy itself again into the same folder? I ran it inside the folder, and it said it couldn't find the file specified. If, for example, I wanted it to say hello, how should I prevent it from running again, once inside the folder?

Thank you

EDIT: also, what does the line char cp[500]; mean?

cp[500] is a char array that will hold 500 characters.

char cp[500]; declares cp


give me like 5 minutes and I'll write some code to check if the file is in the startup folder.

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char cp[500];

    if (strstr(argv[0],"Startup")==NULL)
    {
       sprintf(cp, "copy %s \"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\\"", argv[0]);
       system(cp);
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

That will almost always work. The only time it won't work is if the path is not the startup folder but the path does have the word "Startup" in it. To fix that, change the line: if (strstr(argv[0],"Startup")==NULL) to the entire path of the startup folder.

Thanks, works very well!
So you're saying that if the folder name isn't "Startup" then it won't work?

I'm saying that if you did something like:
create a folder - c:\Startup\test\
put your exe in there
run it.
then it will think it is already in the correct place and it won't copy. However, this is easy to fix like I said before.

Yeah, I have a problem...
When I run it from the desktop it can't find the folder. Why is it that if I run it in the Compiled folder it works?

Thanks

Its because the path to your desktop has spaces in it, while the path to your compiled folder does not.

Easy to fix:

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char cp[500];

    if (strstr(argv[0],"Startup")==NULL)
    {
       sprintf(cp, "copy \"%s\" \"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\\"", argv[0]);
       system(cp);
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Give that a shot

This article has been dead for over six months. Start a new discussion instead.