The OpenPrinter function retrieves a handle identifying the specified printer or print server.
BOOL OpenPrinter(
LPTSTR pPrinterName, |
// pointer to printer or server name |
LPHANDLE phPrinter, |
// pointer to printer or server handle |
LPPRINTER_DEFAULTS pDefault |
// pointer to printer defaults structure |
); |
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
The pDefault parameter allows an application to specify the data type and device mode values that are used for printing documents submitted by the StartDocPrinter function. However, these values can be overridden by using the SetJob function once a document has been started.
The DesiredAccess member of the PRINTER_DEFAULTS data structure pointed to by pDefault specifies the accesses that an application wishes to perform using the handle returned by OpenPrinter. If an application wishes to open a printer to perform administrative tasks, such as the SetPrinter function, it should open the printer with PRINTER_ALL_ACCESS access. If an application wishes only to perform basic printing operations, an access value that includes PRINTER_ACCESS_USE is sufficient.
Windows 95: OpenPrinter ignores the DesiredAccess member of PRINTER_DEFAULTS.
An application can determine what permissions a client has on a print server, or obtain a handle to a print server. To do so, it should call OpenPrinter with pPrinterName set to the name of the server and should specify a server access mask value such as SERVER_ALL_ACCESS. The pDatatype and pDevMode members of the PRINTER_DEFAULTS data structure should be set to NULL. The handle returned by such a call may be passed to the ClosePrinter function.
If a user does not hold permission to open a specified printer or print server with the desired access, the OpenPrinter call will fail, and GetLastError will return the value ERROR_ACCESS_DENIED.