Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

executing other programs

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

604 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