You end the current process by calling DosExit. When you exit, the system stops the process and frees any existing resources the process owns.

In the following code fragment, DosExit is used to end the process if the given file does not exist:

    #define INCL_DOSPROCESS     /* Process and thread values */
    #include <os2.h>

    #define HF_STDERR 2         /* Standard error handle     */

    HFILE   hf;
    ULONG   ulAction, ulWritten;
    APIRET  rc;

    rc = DosOpen("SAMPLE.TXT",
                 &hf,
                 &ulAction,
                 0,
                 FILE_NORMAL,
                 FILE_OPEN,
                 OPEN_ACCESS_WRITEONLY |
                 OPEN_SHARE_DENYWRITE,
                 (PEAOP2) NULL);

    if (rc) {
        DosWrite(HF_STDERR,
                 "Cannot open file\r\n",
                 18,
                 &ulWritten);

        DosExit(EXIT_PROCESS,
                rc);
    }

EXIT_PROCESS directs DosExit to end all the threads in a process including the calling thread, thus ending the process. DosExit includes an error value that is returned to the parent process through the RESULTCODES structure specified in the DosExecPgm call that started the process. If you started the application from the command line, the command processor, CMD.EXE, makes this value available through the ERRORLEVEL variable. If another process started the application, that process can call DosWaitChild to determine the error value.

If you want to exit only from a given thread, you can call DosExit with the EXIT_THREAD constant. This will end only the calling thread; other threads in the process are not affected. If the thread you end is the last thread in the process, the process also ends. If the thread consists of a function, the thread ends when the function returns.


[Back] [Next]