?
Solved

executing other programs

Posted on 2003-11-21
4
Medium Priority
?
2,017 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 90 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

770 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