OpenPrinter BOOL OpenPrinter(


  
  LPTSTR   pPrinterName,      /* address of printer or server name   */
  LPHANDLE phPrinter,         /* address of printer or server handle */
  LPPRINTER_DEFAULTS pDefault /* address of printer defaults         */
                              /* structure                           */
);

Returns a handle identifying the specified printer or print server.

pPrinterName

Points to a null-terminated string that specifies the friendly name of the printer or the network path name, such as a UNC name, for the print server. If you specify a friendly name, the handle returned by OpenPrinter refers to data structures on the local machine that describe the printer (even though the printer may be located on the network). If you specify a network path name, such as a UNC name, the returned handle refers to the data structures on the print server and all subsequent function calls (for example, a SetPrinter call) are passed directly to the specified server over the network. The choice of the printer name type (friendly name or network path name) has important implications, which are described in the comments below.

phPrinter

Points to a value that receives the handle identifying the opened printer or print server object.

pDefault

Points to a PRINTER_DEFAULTS structure. This value can be NULL.


The following is an example of the difference between specifying a friendly printer name and a network path name to a printer server in the pPrinterName parameter of an OpenPrinter call. If you specify the friendly name of a printer on the network, then you can use the returned handle in a subsequent SetPrinter call to change the port on the local machine that accesses that printer. However, if you specify a UNC name of a Windows NT print server in the OpenPrinter call, then a subsequent SetPrinter call to change the printer port will result in an error because the SetPrinter call is not acted upon on the local machine. The call is passed directly to the server over the network and will not succeed there because a Windows 95 client will never have sufficient access privileges to change a printer port on the Windows NT print server.

The pDefault parameter enables 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 the pDefault parameter specifies the types of access 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.

Note that Windows 95 does not attempt to validate security at OpenPrinter time if you specify a friendly printer name, so whatever access flags are set in the PRINTER_DEFAULTS structure are ignored by the OpenPrinter function in this case. This is different than the way the OpenPrinter function works on Windows NT in this case; on Windows NT the access flags are used.

If a user does not hold permission to open the specified print server (specified by a network path name in pPrinterName parameter of the OpenPrinter call) with the desired access, the OpenPrinter call will fail, and GetLastError will return the value ERROR_ACCESS_DENIED.