C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Run a program and read Return-code (solved) Posted by MEGAG3EK on 13 Sept 2005 at 11:55 AM
This message was edited by MEGAG3EK at 2005-9-14 5:51:34

If you make a batch-file with the text...:

App.exe
echo The program returned %ERRORLEVEL%
pause

...then it will print what App.exe returned. I want to do this in a c++ program. Thanks for the help!


Report
Re: Run a program and read Return-code Posted by dwccgc on 13 Sept 2005 at 12:54 PM
: If you make a batch-file with the text...:
:
: App.exe
: echo The program returned %ERRORLEVEL%
: pause
:
: ...then it will print what App.exe returned. I want to do this in a c++ program. Please help!
:

One way is to use one of the spawn fuctions.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__spawn.2c_._wspawn_functions.asp
Report
Re: Run a program and read Return-code Posted by stober on 13 Sept 2005 at 1:04 PM
another way is
CreateProcess(/* blabla */)
WaitForSigneObject()
GetExitCode();
Report
Re: Run a program and read Return-code Posted by BitByBit_Thor on 13 Sept 2005 at 1:07 PM
: If you make a batch-file with the text...:
:
: App.exe
: echo The program returned %ERRORLEVEL%
: pause
:
: ...then it will print what App.exe returned. I want to do this in a c++ program. Please help!
:

This is as close as I could get, browsing through the MSDN Library online:
#include <windows.h>
#include <stdio.h>

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). 
        TEXT("MyChildProcess"), // Command line. 
        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.
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

Now I think you can call GetExitCodeProcess just before the CloseHandle() statement to find out the exit code of the process created.
Here's the MSDN Page on it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getexitcodeprocess.asp

The code example I gave you can be found at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_processes.asp

The MSDN article on CreateProcess: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp

At the remarks there is a comment about running DOS programs. You need to set the Filename paramater to NULL and make the entire call in the Commandline...

Hope this is of any assistance to you...

Greets...
Richard




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.