how to run some exe file by using standard C++ code?

P

Pawel_Iks

I have some program in the executable form (for example notepad.exe in
windows) and I want to run it in C++ code. How to do it?
 
G

Guest

I have some program in the executable form (for example notepad.exe in
windows) and I want to run it in C++ code. How to do it?

You cannot. Or to be exact, your really should not, the only way to do
it in standard C++ is to use system(), but using system() is generally a
bad idea. To run another program you need to use code specific to your
platform, on both Windows and POSIX systems (UNIX and LINUX) there is a
family of functions with names starting with exec which can be used. For
more information please consult a group discussing programming on your
system: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
 
D

duane hebert

I have some program in the executable form (for example notepad.exe in
your answer is mysterious ... why I should not do it?

Not sure what he means either. Maybe he's saying that
you'd be better using a platform specific way (like
createprocess() on windows or whatever it's called.)

If you just want to launch the exe, then
std::system("notepad.exe");

should do it as long as notepad.exe is in your path
or you specify the path.

The thing is, that once you load whatever you want, you'll
likely want to hook it in some way and that's a different
story.
 
G

Guest

Not sure what he means either. Maybe he's saying that
you'd be better using a platform specific way (like
createprocess() on windows or whatever it's called.)

The problem with system() is that the consequences of using it is not
well specified in the C standard (which the C++ standard refers to). The
C standard says that calling system() with a non null-pointer as
argument will pass "the string pointed to by [the argument] to [the
system command processor] to be executed in a manner with the
implementation shall document; this might then cause the program calling
system to behave in a non-conforming manner or to terminate."

As to what system returns it says "If the argument is not a null
pointer, and the system does return, it returns an implementation-
defined value."

Notice that it is not guaranteed to return, and this is not just a
theoretical problem, not long ago sdDirtySox had a problem where he was
using system() and the program he ran froze and consequently froze his
program too.

Further more, usually when you run another program you want to do
something more than just run the program, you might want to interact
with it in some way, this is not possible using system().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top