Solved

executing other programs

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

Industry Leaders: 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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