12.5 Processing Errors During Printing

Although GDI and the spooler attempt to report all printing errors to the user, your application must be prepared to report and handle out-of-disk-space and out-of-memory conditions. When there is an error in processing the EndPage function, it returns a value less than zero. In this case, the return value includes an SP_NOTREPORTED bit. If the bit is zero, GDI has already notified the user. If the bit is set, the application must notify the user. The bit is typically set for general-failure, out-of-disk-space, and out-of-memory errors.

The following example processes unreported errors during printing:

int status;
 .
 .
 .

status = EndPage(hdcPrint);

if (status < 0) {  /* Any unreported errors? */
   if (status & SP_NOTREPORTED) {  /* Yes */
       switch (status) {

           case SP_OUTOFDISK:

               /* Inform user; perform any required processing. */

               break;

           case SP_OUTOFMEMORY:

               /* Inform user; perform any required processing. */

               break;

           default:

               /* Inform user; perform any required processing. */

               break;
       }
   }

   else   /* Reported, but may need further action */
       switch (status | SP_NOTREPORTED) {

           case SP_OUTOFDISK:

               /* Perform any required processing. */

               break;

           case SP_OUTOFMEMORY:

               /* Perform any required processing. */

               break;
       }
}

In this example, the first if statement determines whether the value that the EndPage function returns, status, is less than zero and the SP_NOTREPORTED bit is set. (When Windows sets the SP_NOTREPORTED bit, it indicates that this error has not been reported to the user.) If these two conditions are met, then the application must process the unreported error.

The application then uses a switch statement to provide special responses to the SP_OUTOFDISK and SP_OUTOFMEMORY errors. For all other unreported errors, the application simply provides a general-failure alert.

If the status variable is less than zero but SP_NOTREPORTED is not set, Windows has already reported the error to the user. However, the application can still process these reported errors.

In most cases, the correct response to an unreported error is to display a message box explaining the error and to terminate the print request. If the error has already been reported, you can terminate the request and then restart it after additional disk space or memory has been made available.