Wiki Home

Exit Code


Namespace: VB
Exit Codes are usually numbers passed from one executable process to the calling process. They usually indicate whether the process terminated correctly or not. Visual FoxPro has no native method of passing back an Exit Code.

You can find the Process Exit Code when running an external program from within VFP.

My only practical use of this is when using the Windows NT Task Scheduler which schedules a VFP executable at specified times or dates, runs the process and records any return codes in its Log file. Practical example would be off-hour file maintenance or backup.

NB - there are more uses than just with the task scheduler! It's the best way to pass results back to things like batch files or scripts from a VFP executable so that the command processor can conditionally execute something based on the termination code. The Exit Code gets passed to a batch file as the ErrorLevel value, which can be checked in an IF, for example

I don't know if the seemingly special "HRESULT" is the same thing as an "Exit code" (at least in terms of success or not) but I do know that while trying out the publisher/subscriber COM examples in VFP7 a VFP return of .T. gave a HRESULT back as 'success' and a VFP return of .F. gave a HRESULT back of 'failed'.
While that was encouraging, it turned out that the HRESULT returned to the PUBLISHER was the first one completed and not the 'worst" one completed (I had 2 subscribers when testing this).
In any case it **may** be that a VFP return code is transformed to an HRESULT under more (all?) circumstances. -- Jim Nelson
Ed Rauh has a FAQ describing how to do this in the UT site as follows:

DECLARE ExitProcess in Win32API ;
INTEGER ExitCode

After issuing a CLEAR EVENTS and FLUSH, call Exit ProcessOffsite link to http://www.news2news.com/vfp/?function=133
with an integer value in the range -(2^31) to (2^31)-1

Works like a charm. The NT Task Scheduler picks up the Exit code correctly, but the Log shows:
Result: Unable to find a message for task exit code(3).
In other words, the Exit Code is picked up, but there seems to be an option to Exit with a Text message.

Anyone have any ideas on this ?

Peter Somers

Yep - the Windows Task Scheduler associates an error code with an exit code, and there isn't one defined for the exit code you picked! You might want to take a look at the topic Platform SDK: Task Scheduler API in the MSDN - there is a mechanism to associate text with a termination event, but it's a bit hairy to implement on the VFP side of things; the good news is that there is sample C code in the MSDN showing how to manipulate the Task Scheduler API in detail, and it uses a COM-style interface.

Ed Rauh
Category Windows OS
( Topic last updated: 2004.06.07 12:42:14 PM )