How to obtain a printer handle with attribute PRINTER_OPTION_NO_CACHE ?

09917186 96 Reputation points

Hi everybody,
my problem is: OpenPrinter2W(..., PRINTER_OPTION_NO_CACHE) fails with ERROR_NOT_SUPPORTED.

openprinter2 ("The printer data returned by OpenPrinter2 is retrieved from a local cache unless the PRINTER_OPTION_NO_CACHE flag is set in the dwFlags field of the PRINTER_OPTIONS structure referenced by pOptions.", "Accordingly, when opening a local printer, OpenPrinter2 provides no advantage over OpenPrinter.")
along with
printer-option-flags ("PRINTER_OPTION_NO_CACHE: The handle is not cached. All functions applied to a handle returned by OpenPrinter2 will go to the remote computer.")
seem to indicate that calling OpenPrinter2W(..., PRINTER_OPTION_NO_CACHE) is the correct way to obtain the required printer handle hPrinterNoCache, which to use in GetPrinterData( hPrinterNoCache, ... ) on print client computer (computer, which takes share of a printer shared by an other computer in network), so that Windows Print Subsystem would call pfnGetPrinterDataFromPort in my own Language Monitor (for bidirectional communication) on print server (computer, to which the printer is physically connected by cable, and which shares it to other computers in a network). But unfortunately, OpenPrinter2W fails with ERROR_NOT_SUPPORTED, no matter which input parameters I use in OpenPrinter2W. How to obtain hPrinterNoCache ?

C++ code snipplet:

DWORD dwLastError;
HANDLE hPrinterNoCache;
PRINTER_OPTIONS poPrinterOptions;
poPrinterOptions.cbSize = sizeof( PRINTER_OPTIONS );
poPrinterOptions.dwFlags = PRINTER_OPTION_NO_CACHE;
if ( !OpenPrinter2W( L"\\PrintServerName\PrinterName", &hPrinterNoCache, NULL, &poPrinterOptions ) )
// nor does "PrintServerName\PrinterName" work, nor any local printer, nor L"PrintServerName\,XcvMonitor Standard TCP/IP Port", nor using &pdfPrinterDefaults, nor running application on elevated rights, ...
dwLastError = GetLastError(); // ERROR_NOT_SUPPORTED

Calling OpenPrinter instead of OpenPrinter2 in same spot works without problem.

Dependency Walker shows that Winspool.drv and Spoolss.dll both export OpenPrinter2W, but Win32spl.dll and localspl.dll do not (referring to illustration in introduction-to-print-providers).

Any help is greatly appreciated !
Thanks in advance,

Windows Server Printing
Windows Server Printing
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Printing: Printer centralized deployment and management, scan and fax resources management, and document services
640 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. clcheles 271 Reputation points

    A couple of things that worth mentioned here:

    • check if you have turned off password protected sharing ON (if ON, you'll need to authenticate before acquiring the handle)
    • to get data from print server instead of local copy on print client, this was confirmed to work PRINTER_DEFAULTS pdfPrinterDefaults = { NULL, NULL, PRINTER_ALL_ACCESS };
      OpenPrinter2W( L"\\PrintServerName\PrinterName", &hPrinterNoCache, &pdfPrinterDefaults, &poPrinterOptions )

    Solution confirmed by post owner.

    0 comments No comments