943,822 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4566
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 13th, 2009
0

program that starts another program.

Expand Post »
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.
Similar Threads
Reputation Points: 18
Solved Threads: 4
Junior Poster
Bladtman242 is offline Offline
163 posts
since Dec 2008
Feb 13th, 2009
0

Re: program that starts another program.

in windows can you use ShellExecute, which is an API call that will allow you to spawn an application.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 13th, 2009
0

Re: program that starts another program.

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?
Last edited by Bladtman242; Feb 13th, 2009 at 12:33 pm.
Reputation Points: 18
Solved Threads: 4
Junior Poster
Bladtman242 is offline Offline
163 posts
since Dec 2008
Feb 13th, 2009
0

Re: program that starts another program.

well, it's included in the windows.h, so try this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <windows.h>
  3. int main(int argc, char **argv)
  4. {
  5. ShellExecute(NULL, "open", "C:\\windows\\system32\\calc.exe","", "C:\\", SW_NORMAL);
  6. return 0;
  7. }

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.
Last edited by Comatose; Feb 13th, 2009 at 12:39 pm.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 13th, 2009
0

Re: program that starts another program.

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
Last edited by Bladtman242; Feb 13th, 2009 at 1:00 pm.
Reputation Points: 18
Solved Threads: 4
Junior Poster
Bladtman242 is offline Offline
163 posts
since Dec 2008
Feb 13th, 2009
1

Re: program that starts another program.

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.
Last edited by death_oclock; Feb 13th, 2009 at 10:34 pm.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 14th, 2009
0

Re: program that starts another program.

Or use.. execlp(...)
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Feb 14th, 2009
0

Re: program that starts another program.

Click to Expand / Collapse  Quote originally posted by cikara21 ...
Or use.. execlp(...)
Which I believe is *nix only.....
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 14th, 2009
0

Re: program that starts another program.

Thank you death_oclock, that was really helpfull

Cikara21, could you wxplain that?

Okay, Comatose: i tried it out, made a simple .bat called test.bat located on C:\, containing
C++ Syntax (Toggle Plain Text)
  1. msg * Hi %username% u rule
(rediculus i know)
and then a c++ "program":
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ShellExecute(NULL, "open", "C:\\test.bat","", "C:\\", SW_NORMAL);
  9. return 0;
  10. }

Why does that not work? :/
Reputation Points: 18
Solved Threads: 4
Junior Poster
Bladtman242 is offline Offline
163 posts
since Dec 2008
Feb 14th, 2009
0

Re: program that starts another program.

Cikara21, could you wxplain that?
Quote originally posted by Comatose ...
Which I believe is *nix only.....
^^ Okay. beaten by seconds by Comatose

So, does anyone know what i am doing wrong here?
Last edited by Bladtman242; Feb 14th, 2009 at 8:39 am.
Reputation Points: 18
Solved Threads: 4
Junior Poster
Bladtman242 is offline Offline
163 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: linked list help
Next Thread in C++ Forum Timeline: how i can to add for microsoft visual c++ mfc option





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC