Solved

executing other programs

Posted on 2003-11-21
4
2,014 Views
Last Modified: 2010-04-01
i think this is easy. i just don't know how to do it...

i work in java and distribute my apps with a batch file in which i execute the java interpreter from my app's dir (and provide it with my java classes). i want to make that batch file an exe. so, the question is: how do i execute/call another program to run from a c++ prog. all i want is something like

void main() {
   execute("jre\\bin\\java.exe -my parameters -bla -bla");
}

and to return immediately after calling that (not wait for it to finish)

,thanks
0
Comment
Question by:hapciu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 30 total points
ID: 9799000
In Windows you can use CreateProcess API:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp


void main( VOID )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( NULL, // No module name (use command line).
        "MyChildProcess param1 param2", // Command line with parameters.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        FALSE,            // Set handle inheritance to FALSE.
        0,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi )             // Pointer to PROCESS_INFORMATION structure.
    )
    {
        ErrorExit( "CreateProcess failed." );
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}
0
 
LVL 2

Expert Comment

by:antoinebf
ID: 9800605
Also in windows:
You are probably thinking about the CRT system() call. It takes a string as param and will try to execute it just like if you had entered it in the "Run" dialog in the start menu.

However, it will wait for the process to terminate before continuing execution.

example:
#include <process.h>
...
   system("notepad.exe");
   system("jre\\bin\\java.exe -my parameters -bla -bla");
...

Using this, and I'm stretching it a bit, you can launch a batch file that uses the "start" call:
in C/C++:
   system("run.bat");
Inside "run.bat":
   start jre\\bin\\java.exe -my parameters -bla -bla
   rem "start /wait notepad.exe" would wait until completion

But AlexFM's solution is simply better! :)

hth
A.
0
 

Expert Comment

by:rvnistelrooy_10
ID: 10370528
antoinebf, may i know y u say AlexFM's solution is better than urs? Thx.
0
 
LVL 2

Expert Comment

by:antoinebf
ID: 10372866
Because it basically does the same thing, i.e. launch a process, without a major drawback: it is not a blocking call (as required by Q author)...

I am not saying my solution is not good, but in this case, AlexFM's is more appropriate. :)

A.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question