954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

program that starts another program.

Is there a simple way to run another program, say IE, FF or just a simple .bat file sith c++? ive searched around a bit but all ive found is rocket science and didn't seem to be what im looking for. i dont wish to run it "trough" my program, just as if i had found the destination and doubleclick'ed it.

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

in windows can you use ShellExecute, which is an API call that will allow you to spawn an application.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Thanks for the quick answer:)
Is shellexecute a astandard function? like shellexecute();
or?

EDIT: perhaps i misunderstood, does ShellExecute have anything to do with the c++ language?

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

well, it's included in the windows.h, so try this:

#include <iostream>
#include <windows.h>
int main(int argc, char **argv)
{
     ShellExecute(NULL, "open", "C:\\windows\\system32\\calc.exe","", "C:\\", SW_NORMAL);
     return 0;
}


EDIT: Negative. ShellExecute is NOT C++.... ShellExecute is a windows function that can be called from any language with the ability to use windows API's (C++ is such a language). This is windows specific, and won't work in *nix or Mac.... and is not a standard.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Ahh thanks :)
Now i tryed to look up the parameters (not that i couldn't find them, i just didn't understand ;-) )
Could you please explain the parameters you are using? just briefly, i hope its not too much trouble

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

The first parameter, hWnd (passed NULL) is the window to associate the new process with (NULL means you aren't using this feature).

The second, lpOperation, is what you want to do with the file specified in the next parameter. Here we are "open"ing it.

Speaking of which, the third is lpFile: the file you want to perform the operation on.

The fourth, lpParameters is a string of the arguments you want to pass into the new process. These are not used in this situation.

Next is lpDirectory. This is the path you want the new process to think it was started from.

Finally, we have nShowCommand: this just specifies the starting state of the window for the new process. SW_NORMAL specifies that, what do ya know, it should start in normal mode!

You can look at specifics including other possible arguments at msdn's documentation for this function.

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

Or use.. execlp(...)

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 
Or use.. execlp(...)

Which I believe is *nix only.....

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Thank you death_oclock, that was really helpfull:)

Cikara21, could you wxplain that? :D

Okay, Comatose: i tried it out, made a simple .bat called test.bat located on C:\, containing

msg * Hi %username% u rule

(rediculus i know)
and then a c++ "program":

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    ShellExecute(NULL, "open", "C:\\test.bat","", "C:\\", SW_NORMAL);
    return 0;
}


Why does that not work? :/

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 
Cikara21, could you wxplain that? :D
Which I believe is *nix only.....


^^ Okay. beaten by seconds by Comatose :D

So, does anyone know what i am doing wrong here?

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 
Why does that not work? :/


It works for me. Did you put test.bat in C's root dir? Here's a rewrite that starts it from the desktop.

#include <windows.h>

int main() {
    char path[MAX_PATH];
    GetEnvironmentVariable ("HOMEPATH", path, MAX_PATH);
    strcat (path, "\\Desktop");
    ShellExecute (0, "open", "test.bat", 0, path, SW_NORMAL);
    return 0;
}
nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 

[Why cant i edit my post?]

Well, it seems the problem is that i am using vista. i read something about setting ipverb to "runas". but honestly, i have no idea how to doo that? i tried googling it but.. anyone?

Link for the "runas" thingy here

@Nucleon: yeah i put it at the root: -) so i dont think your script would make a difference? (correct me if wrong ; -) )

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

Since you are just using ShellExecute (not ShellExecuteEx) you could just change "open" to "runas". I think.

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

Changing "open" to "runas" doesn't help:/ and if i copy-paste comatose's examle with calc.exe it just works? both with "runas" AND "open"? i don't get it?

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

you wouldn't change open to runas..... open is the "command" basically, that you are doing. What do you want to do with this file? Open it. Just like if I double clicked on it. calc.exe is a program in windows (all versions passed like 2.0). Runas is a program in vista. So replacing open with runas isn't going to work.... it's the wrong parameter. You need to replace calc.exe with runas, and somehow pass like calc.exe (and all the switches) to the parameters argument of shellexecute. It may be easier to use shellexecuteex as in the provided link above... so I guess something like this (not tested):

ShellExecute (NULL, "open", "runas", "test.bat", "c:\\", SW_NORMAL);

But based on the fact that you are in vista, and require elevated privs, I suggest using the shellexecuteex.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Okay. So, what is this ShellExecuteex? again, google didn't help me much (no related results really).
I wont do to just write ShellExecuteex and the the same params, i know. but..

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

it's actually on the link that you provided . If you look carefully, they are using ShellExecuteEx there. So, do that, and adjust it to your .bat file.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Ahh, i see.
Hmm, when i try to compile it my IDE gives me alot of errors, first one is:
stdafx.h : no such file or directory.

Is that because i am missing a library or?

What really makes me wonder, is that the first sollution

#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

int main()
{
    ShellExecute(NULL, "open", "C:\\ny test.bat","", "", SW_NORMAL);
    cin.get();
    return 0;
}

Works PERFECTLY with .exe files, but not with .bat files?
So ShellExecute(NULL, "open", "C:\\ny test.exe","", "", SW_NORMAL);
Works, presuming i knew how to solve my problem with an .exe, wich i dont.
BUT ShellExecute(NULL, "open", "C:\\ny test.bat","", "", SW_NORMAL);
Does not work?!?

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

Above code works fine in windows XP, so perhaps some sort of vista thing?

The more important question is: Why would you want to execute a bat file? What's in it that you can't do with c++?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

^^ i think you are right it's a vista thing. however its still a problem ;-)

Well for one thing, when something is not working, and there is no appearent reason why. i think one should try to either solve the problem, or at least find out whats causing the problem.

But you are absolutely right, its nothing one couldn't do with C++, it's just that I dont know how ;-)

I would like to know how to do this but to solve my actual problem:
What i need is actually just a way to determin the user's username, and ,if possible, eventually the password. but then i came across this problem and then.. i just wanted to know how to solve it nomatter what i can use it for, but I'm open to suggestions about another way of doing this. Hope no one died from trying to read this looong post, if so R.I.P

Bladtman242
Junior Poster
176 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You