How to Determine the Type of Handle Retrieved from OpenPrinterLast reviewed: September 29, 1995Article ID: Q126258 |
The information in this article applies to:
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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |