CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: CreateProcess()

  1. #1
    Join Date
    Oct 1999
    Location
    CA,USA
    Posts
    22

    CreateProcess()

    Can I get the return code from a DOS Command executed from a CreateProcess() function?


  2. #2
    Join Date
    Nov 1999
    Location
    NY
    Posts
    906

    Re: CreateProcess()

    Search for Q190351 on your MSDN CD or at http://msdn.microsoft.com.

    Kailash


  3. #3
    Join Date
    Oct 1999
    Location
    CA,USA
    Posts
    22

    Re: CreateProcess()

    Thanks for the information, however, that just tells me how to get the output back - which I already do.

    Unfortunately, there is not a reliable way to determine from the output whether the command failed or not. I need the return code issued from the command to be able to do that.


  4. #4
    Guest

    Re: CreateProcess()

    You CAN do that provided that the DOS command is a win32 exe....so there is actually a great way to cheat....you use CreateProcess() to run cmd.exe ( or command.com in win95 ) and pass the exe you want to run to that.....if you were to run a 16bit app this way directly...you cannot get its return code.

    Like this:

    [ccode]

    int CBuild::Spawn( int nMode, char * cmdproc, char * buff )
    {

    STARTUPINFO si = {0};
    PROCESS_INFORMATION pi = {0};

    si.cb = sizeof( STARTUPINFO );
    si.lpReserved = NULL;
    si.lpReserved2 = NULL;
    si.cbReserved2 = 0;
    si.lpDesktop = NULL;
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;

    BOOL CreateProcessStatus;
    ULONG dosretval;
    DWORD retval;
    MSG msg;

    CBuildApp* pApp = (CBuildApp*)AfxGetApp();

    _doserrno = errno = 0; // set to zero first so we can see if we go wrong somewhere

    CreateProcessStatus = CreateProcess( cmdproc, buff, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi );
    dosretval = GetLastError();

    if (!CreateProcessStatus)
    {
    return -1;
    }

    if ( nMode == _P_WAIT ) /* SYNCHRONOUS */
    {
    WaitForSingleObject( pi.hProcess, (DWORD)(-1L) );
    GetExitCodeProcess( pi.hProcess, &retval );
    CloseHandle(pi.hProcess);

    // so the system doesnt appear to freeze
    while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    if (msg.message == MM_STOP)
    {
    pApp->bBusy = FALSE;
    pApp->bStopProcess = TRUE;
    break;
    }

    if (!AfxGetApp()->PreTranslateMessage(&msg))
    {
    ::TranslateMessage(&msg);
    :ispatchMessage(&msg);
    }
    AfxGetApp()->OnIdle(0); // updates user interface
    AfxGetApp()->OnIdle(1); // frees temporary objects

    }

    }
    CloseHandle( pi.hThread );
    return retval;
    }






  5. #5
    Join Date
    Nov 1999
    Location
    NY
    Posts
    906

    Re: CreateProcess()

    GetExitCodeProcess() will tell you whether the process has exited as well as give you the exit (return code) code of the command, if any.


    // piProcInfo is the ptr to PROCESS_INFORMATION structure returned by CreateProcess()
    GetExitCodeProcess (piProcInfo->hProcess, &dwExitCode);




    Kailash


  6. #6
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: CreateProcess()

    If the other suggestions don't work, there is always this hack: use a batch file to run your DOS app, set an environment variable, then have a Win32 console app (run in the same batch file) read the environment variable with getenv() and store the value wherever it is easiest for your main app to get to (text file, registry, whatever).


    REM Batch file to run a DOS app and get its return code
    MyApp
    if errorlevel 5 SET RETCODE=5
    if errorlevel 5 goto GetRC
    if errorlevel 4 SET RETCODE=4
    if errorlevel 4 goto GetRC
    REM etc.
    :GetRC
    REM run console app to read RETCODE with getenv(), store result
    GetCode





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured