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.
2 comments  
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

Dale Johnson 9/11/2025 11:52:32 AM
Hello Stefan,
You need to first insert the kerner32.dll library in the Libraries folder, then declare the GetExitCodeProcess() procedure in the kerner32 procedures sub-folder. It's the same process as declaring other procedures. Then define the procedure parameters and types as per the article:
HANDLE hProcess
LPDWORD lpExitCode

See the ATEasy Help; Declaring DLL Procedures and DLL Procedure Properties
1 | 0
Stefan Wübbeke 9/11/2025 8:16:46 AM
Hello,
I'm currently using ATEasy 10 and tried this:
hNotePad=WinExec("C:\\Tool\\ConsoleApp8.exe -get A0002")
WaitForSingleObject(hNotePad)
GetExitCodeProcess(hNotePad, pdwCode)
print pdwCode

But there ist Compiler error #785: Undefined symbol GetExitCodeProcess
hNotePad = AHandle
pdwCode = Any

I don´t know how to define GetExitCodeProcess. I think it should no longer be a problem in 2025 to open an .exe in ATEasy and read the data
0 | 0

Login to rate article

Read Prior Article Read Next Article