Thread: how to run an exe command in c++ and get back the results?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Unhappy how to run an exe command in c++ and get back the results?

    how to run an exe command in c++ and get back the results?
    I am writting a Gui for a device, in order to run tests on this device I have to run an exe file from the command window :
    something like the following: >>c:\upgrade.exe -i 10.10.1.69 -f c:\file.txt -a

    as u can see the exe file has a few switches as well, and the ip address and the location of the file is controlled by the user.
    I should also get back the result of running the above command and analyse it and show the result in my Gui.

    They want a windows application Gui, so I am using windows forms applications. I don't know how to make my program execute the above command and return back the results. I have looked at ShellExecute() and Creatprosess() so far, but they can only start up an exe file, once the file is started I have no control over it.
    Can you please help me on this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use _popen() in win32
    Code:
    FILE *fp = _popen("c:\\upgrade.exe -i 10.10.1.69 -f c:\\file.txt -a","r");
    // use fgets() to read each line, just like you would a regular file
    _pclose(fp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    EDIT: beaten to the cockles. And with a better answer, too.

    You could use pipes. I'm not well versed in the intricacies of pipe manipulation, but a quick search should find you what you need.

    A simplistic solution is to use the built-in file piping commands in Windows (and most Unix platforms if I'm not mistaken) and pipe input and output for the program into temporary disk files. An example, assuming print.exe will take lines of input and print them to output:

    Code:
    print.exe <in.txt >out.txt
    Should (if my memory hasn't failed me) print whatever is in in.txt to out.txt. So your program can handle these files, exec the program, then read the output from the files. Of course, this may not be economical for programs which generate large amounts of output or need to be run many times in a short period. Direct piping is the best solution.
    Last edited by jafet; 09-21-2006 at 12:24 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    25
    If by 'result' you mean the process exit code, you can obtain it like this:
    Code:
    HANDLE hProcess = /* Handle to your process */;
    DWORD exitCode = 0;
    
    ::WaitForSingleObject(hProcess, INFINITE);
    ::GetExitCodeProcess(hProcess, &exitCode);
    To get the process handle, check out the LPPROCESS_INFORMATION field in CreateProcess.

    If you want to hook onto the child process's console, take a look at this.

    Anyway, this sort of questions is better asked in the Windows programming board.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Boost.Process, a cross-platform process control library, was developed as part of the Google Summer of Code. It can do exactly what you need on POSIX and Win32 systems with uniform syntax.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    thanks guys
    it was really help fullI have tried the piping and it works fine.

Popular pages Recent additions subscribe to a feed