Verifying the Printer Connection

Last reviewed: August 12, 1997
Article ID: Q77467

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

The PC ROM BIOS printer services function Get Printer Status (INT 17H Function 2H) can be used to determine if a printer is connected to a specific port. To save time and potential data loss, an application can use INT 17H Function 2H to determine if a printer is present before entering the application's printing code. This article contains code to make this determination.

NOTE: This same functionality may be obtained in Windows 3.1 using the OpenComm(), WriteComm(), and GetCommError() functions.

Sample Code

The following code defines a function IsPrinter():

 /***************************************************************
  *                                                             *
  *  Name:     IsPrinter - tests if printer is connected        *
  *                                                             *
  *  Parameters:   AX = 02h                                     *
  *                DX = printer number (LPT1 = 0)               *
  *                                                             *
  *  Return    AH = status bits                                 *
  *                 01h    time out                             *
  *                 02h    unused                               *
  *                 04h    unused                               *
  *                 08h    I/O error                            *
  *                 10h    printer selected                     *
  *                 20h    out of paper                         *
  *                 40h    printer acknowledgment               *
  *                 80h    printer not busy                     *
  *                                                             *
  * Function returns 1 if printer connected                     *
  *          returns 0 otherwise                                *
  *                                                             *
  ***************************************************************/

int IsPrinter(void)
{
   unsigned char iRetVal = 0;

   _asm
      {
         mov   ah, 02h
         mov   dx, 0            /* 0 is number for LPT1: */
         int   17h
         mov   iRetVal, ah
      }

   if (iRetVal == 0x90)
      return (1);
   else
      return (0);
}

For more information, please refer to "The MS-DOS Encyclopedia" (Microsoft Press). Pages 163 and 164 contain an example of interrupt driven communication with the printer. Appendix O details the ROM BIOS functions and their parameters.

Keywords          : GdiPrn kb16bitonly kbcode kbfasttip kbhowto
Version           : 3.0 3.1
Platform          : WINDOWS
Issue type        : kbhowto
Solution Type     : kbcode


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 12, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.