calling a program from within a program

This is a discussion on calling a program from within a program within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm trying to call a program from within another program using; Code: System(file name); The called program is ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    calling a program from within a program

    Hi all,
    I'm trying to call a program from within another program using;

    Code:
    System(file name);
    The called program is called and run perfectly, but after it's run is over, it does not return control back to the calling program. Is there a special code that I have to add to make the control go back to the calling program?

    Also, is the above code the best code to use for calling another program from within another program?

    I envision that by the time that I'm through, I'll be calling at least 4 other programs from within my calling program. . . . As usual, I thank you for your replies. . . . therry

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Therry View Post
    The called program is called and run perfectly, but after it's run is over, it does not return control back to the calling program.
    It should return to the calling program. What exactly are you doing?

    Quote Originally Posted by Therry View Post
    Also, is the above code the best code to use for calling another program from within another program?
    What OS are you using?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    I'm running windows 7 (64bit)

    With respect to: It should return to the calling program. What exactly are you doing?

    I need the result (the out-put while it's still in memory) of the first program as in-put for the second program. When I debug, the system_call runs the called program (I see it in the debug window) but in the debug window it does not return to the calling program. it just shows the called program.

  4. #4
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    You could try using the Win32 API's CreateProcess() and GetExitCodeProcess()

    CreateProcess function
    GetExitCodeProcess function

    respectively, using the parameters necessary in the call to CreateProcess(). A little bit more work but it may solve your problem. I've always heard that using system() is a horrible idea.
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    The fact of output appearing in a debug window does not mean control is not returning to the original program. Both your spawning and spawned programs might be writing to the same window. What that means is that, if you want to capture output from a spawned program, then you need to take specific steps to capture that output. The system() call does not magically do that for you.

    For example, you might redirect standard output from the spawned program to a file. Then your original program (when control returns to it) can read that file. The problem is, techniques to redirect output from a spawned program depend on a lot of things. Alternatively, you can modify the source code of the spawned program so it writes output directly to a file, rather than to standard output (which is console output by default).

    If you are willing to limit yourself to running under windows, look up the win32 API function named CreateProcess() and use it instead of the system() call. It accepts a slew of arguments, including some that specify how to redirect the standard output device used by the spawned program.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  2. calling one program from another program
    By arian in forum C# Programming
    Replies: 2
    Last Post: 09-19-2008, 08:25 AM
  3. Calling another program
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 12-15-2004, 03:16 PM
  4. calling a program from a program
    By Waldo2k2 in forum C Programming
    Replies: 2
    Last Post: 01-20-2003, 12:43 PM
  5. Calling an FTP from within a program
    By clancyPC in forum C Programming
    Replies: 2
    Last Post: 04-17-2002, 04:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21