Solved

executing other programs

Posted on 2003-11-21
4
2,011 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now