Visual Basic System Services

AddPrinter: Add/Delete Local/Remote Printers using Existing Drivers
Posted:   Tuesday June 22, 2004
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch


AddPort: Adding and Deleting Application-Defined Ports
AddPrinter: Add/Delete Local/Remote Printers using Existing Drivers
EnumPorts: Identify Windows' Available Ports

Windows' AddPrinter, OpenPrinter and DeletePrinter APIs provide for easily adding new printers using drivers already installed on the target machine. The code is straightforward and well commented, but some discussion of installing on remote machines is required.

AddPrinter uses a UDT to pass the parameters for the new printer. One of these parameters is the pPrinterName member, which can be thought of as the printer's 'friendly name' ("vbnet test printer" in this demo). AddPrinter's parameter list also sports a server name member which accepts either vbNullString (for installation on the local machine), or a remote machine name in the usual server format "\\servername". 

In order to delete a printer the printer must first be opened using OpenPrinter.  While this API has a printer name parameter, it does not have a server name parameter as AddPrinter does.  Therefore, to open a remote printer the format of the printer name parameter is similar to that for a remote file ... "\\servername\printername".  In my demo, where the printer name used is "vbnet test printer" and my remote machine is named "laptopxp", OpenPrinter's printer name parameter would be "\\laptopxp\vbnet test printer".

Considerable additional work (along with several other API calls) is required to install a printer and printer driver from scratch, and is thus not covered in this demo.

 BAS Module Code

 Form Code
On a form add two command buttons (Command1, Command2) and a label (Lable1), along with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const PRINTER_ATTRIBUTE_LOCAL As Long = &H40
Private Const PRINTER_LEVEL2 As Long = &H2

Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const PRINTER_ACCESS_USE As Long = &H8
                                             PRINTER_ACCESS_ADMINISTER Or _

   pDatatype As String
   pDevMode As Long  'DEVMODE
   DesiredAccess As Long
End Type

Private Type PRINTER_INFO_2
   pServerName As String
   pPrinterName As String
   pShareName As String
   pPortName As String
   pDriverName As String
   pComment As String
   pLocation As String
   pDevMode As Long  'DEVMODE
   pSepFile As String
   pPrintProcessor As String
   pDatatype As String
   pParameters As String
   pSecurityDescriptor As Long 'SECURITY_DESCRIPTOR
   Attributes As Long
   Priority As Long
   DefaultPriority As Long
   StartTime As Long
   UntilTime As Long
   Status As Long
   cJobs As Long
   AveragePPM As Long
End Type

Private Declare Function AddPrinter Lib "winspool.drv" _
   Alias "AddPrinterA" _
  (ByVal pServerName As String, _
   ByVal Level As Long, _
   pPrinter As Any) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" _
   Alias "OpenPrinterA" _
  (ByVal pPrinterName As String, _
   phPrinter As Long, _
   pDefault As Any) As Long
Private Declare Function DeletePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long

Private Sub Form_Load()

   Command1.Caption = "Add Printer"
   Command2.Caption = "Delete Printer"
   Label1.Caption = ""
End Sub

Private Sub Command1_Click()

   Dim hPrinter As Long
   Dim sServer As String
   Dim pi2 As PRINTER_INFO_2
   With pi2
      'local machine
      .pServerName = vbNullString
      'display name for new printer
      .pPrinterName = "vbnet test printer"
      'name for share - must be upper-case
      .pShareName = "HPLJ601"
      'printer port (NT or later can
      'have multiple separated with
      'commas, e.g. "LPT1:,LPT2:")
      .pPortName = "LPT1:"
      'the name of an existing driver
      .pDriverName = "HP LaserJet 5Si MX"
      'a comment, if desired
      .pComment = "Test printer - can delete"
      'the location, if desired
      .pLocation = "Bldg 7, Room 1321, NW601"
      'the print processor name - manditory
      .pPrintProcessor = "WinPrint"
      'type of data supported, e.g. RAW or EMF
      'this must match type used by the driver
      '- manditory
      .pDatatype = "RAW"
      'flags - local printer, with share created
      .Attributes = PRINTER_ATTRIBUTE_LOCAL Or _
   End With
  'install on the local machine by passing
  'vbNullString as the server name. If
  'installation on another machine is
  'to be made, pass the machine name
  'in the usual 'server name format', 
  'eg: sServer = "\\laptopxp"
   sServer = vbNullString
   hPrinter = AddPrinter(sServer, PRINTER_LEVEL2, pi2)
   Debug.Print hPrinter, Err.LastDllError
   If hPrinter <> 0 Then
      Label1.Caption = "Printer added successfully"
      ClosePrinter hPrinter
   End If
End Sub

Private Sub Command2_Click()

   Dim hPrinter As Long
   Dim sPrinterName As String
  'we're deleting the just-added local printer,
  'so specify the same printer name as used
  'for pi2.pPrinterName during AddPrinter
   sPrinterName = "vbnet test printer"

  'if the printer to be deleted is on another
  'machine, the machine and printer are passed
  'together as sPrinterName in the format
  '\\computername\printername, e.g.
  '   sPrinterName = sServer & "\" & sPrinterName
  'or for our demo,
  '   sPrinterName = "\\laptopxp\vbnet test printer"

  'set up a printer_defaults structure
   With pd
     'no devmode, so UDT member is
     'declared Long and a null is passed
      .pDevMode = 0&
      'must be datatype of printer driver
      'as set in AddPrinter
      .pDatatype = "RAW"
      'this is the access level for delete
      .DesiredAccess = PRINTER_ALL_ACCESS
   End With
   If OpenPrinter(sPrinterName, hPrinter, pd) <> 0 Then
      If hPrinter <> 0 Then
         If DeletePrinter(hPrinter) <> 0 Then
            Label1.Caption = "Printer deleted"
         End If  'DeletePrinter

      End If  'hPrinter

      ClosePrinter hPrinter
   End If  'OpenPrinter
   Debug.Print hPrinter, Err.LastDllError
End Sub


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter