How to Determine the Type of Handle Retrieved from OpenPrinter

Last reviewed: September 29, 1995
Article ID: Q126258
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.1, 3.5, and 3.51
        - Microsoft Windows 95 version 4.0
    

OpenPrinter returns a valid handle when a printer name or a server name is passed to it. Sometimes it may be necessary to determine if the returned handle is a handle to a printer because some Win32 spooler functions only accept printer handles and will fail on server handles. The following code determines if a handle is a printer handle:

BOOL IsPrinterHandle( HANDLE hPrinter) {

    DWORD       cbNeeded;
    DWORD       Error;
    BOOL        bRet = FALSE;
    LPPRINTER_INFO_2 pPrinter;
    DWORD       cbBuf;
    HANDLE      hMem = NULL;

    if( !GetPrinter(hPrinter, 2, (LPBYTE)NULL, cbBuf, &cbNeeded ))
    {
        Error = GetLastError( );

        if( Error == ERROR_INSUFFICIENT_BUFFER )
        {
            hMem = GlobalAlloc(GHND, cbNeeded);
            if (!hMem) return bRet;
            pPrinter = (LPPRINTER_INFO_2)GlobalLock(hMem);
            cbBuf = cbNeeded;
            if(GetPrinter(hPrinter, 2, (LPBYTE)pPrinter, cbBuf, &cbNeeded))
            {
                bRet = TRUE;
                GlobalUnlock(hMem);
                GlobalFree(hMem);
            }
            else SetLastError( ERROR_INVALID_PRINTER_NAME );
        }
        else if( Error == ERROR_INVALID_HANDLE )
        {
            SetLastError( ERROR_INVALID_PRINTER_NAME );
        }
    }
    return bRet;
}


Additional reference words: 3.10 3.50 4.00 95
KBCategory: kbprint kbcode
KBSubcategory: GdiPrn


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: September 29, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.