Getting Return Code from External Process Started with WinExec

Knowledge Base Article # Q200069

Read Prior Article Read Next Article
Summary To access the code returned by an application launched with WinExec, you will need to use code similar to that described in this article.
  
Login to rate article
Solution:

WinExec() does not operate synchronously and will not wait until the called process is closed before returning to the calling application. Instead, it will return immediately and return a process handle indicating if the process was successfully created or not. To access the code returned by the application launched with WinExec(), you will need to use code similar to the following:

hProcess=WinExec("...")
if hProcess<21
    ! error creating the process
    return
endif
WaitForSingleObject(hProcess)
GetExitCodeProcess(hProcess, dwCode)


The GetExitCodeProcess function retrieves the termination status of the specified process, and is defined in kerner32.dll. You will need to define it under a kernel32.dll entry within your module libraries.  In ATEasy this function will be defined as:

GetExitCodeProcess(hProcess: Val AHandle, pdwCode : VAR DWord) : BOOL


A description of the function is provided below:

GetExitCodeProcess:

BOOL GetExitCodeProcess
(
HANDLE hProcess,     // handle to the process
LPDWORD lpExitCode   // termination status
);

Parameters:
  • hProcess [in] - Handle to the process. For Windows NT/2000/XP, the handle must have PROCESS_QUERY_INFORMATION access. For more information, see Process Security and Access Rights.
  • lpExitCode [out] - Pointer to a variable to receive the process termination status.
Return Values:
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks:
If the specified process has not terminated, the termination status returned is STILL_ACTIVE. If the process has terminated, the termination status returned may be one of the following:
  • The exit value specified in the ExitProcess or TerminateProcess function.
  • The return value from the main or WinMain function of the process.
  • The exception value for an unhandled exception that caused the process to terminate.
Article Date 4/24/2006
Keywords ATEasy, WinExec, WaitForSingleObject


Login to rate article

Read Prior Article Read Next Article
>