Visual Basic System Services
SetPrinter: Rename Local and Remote Printers /2
     
Posted:   Saturday August 06, 2005
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   See Prerequisites
Author:   VBnet - Randy Birch
     

Related:  

AddPrinter: Add/Delete Local/Remote Printers using Existing Drivers
EnumPrinters: Enumerating Local and Network Printers
EnumPrinterDrivers: Enumerate Print Drivers on Specific Printer Server
SetDefaultPrinter: Changing Windows' Default Printer
SetPrinter: Rename Local and Remote Printers
WriteProfileString: Changing Windows' Default Printer
     
 Prerequisites
For this demo as coded, Windows NT/2000/XP or later. Earlier Windows versions must use a PRINTER_INFO_2 structure where the string members are defined as strings. Application of this structure would be similar to that shown below for the PRINTER_INFO_4 structure.

This code duplicates the functionality shown in SetPrinter: Rename Local and Remote Printers, but utilizes a different definition for the PRINTER_INFO_4 structure (and therefore a different set of preparation steps) to rename a local or remote printer. 

Rather than repeat info here, please see SetPrinter: Rename Local and Remote Printers for the full description.

 BAS Module Code
None.

 Form Code
Add two command buttons (Command1, Command2) to a form 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 SW_SHOWNORMAL As Long = 1
Private Const PRINTER_LEVEL4 As Long = &H4
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const PRINTER_ACCESS_ADMINISTER As Long = &H4
Private Const PRINTER_ACCESS_USE As Long = &H8

Private Type PRINTER_DEFAULTS
   pDatatype As String
   pDevMode As Long
   DesiredAccess As Long
End Type

Private Type PRINTER_INFO_4
   pPrinterName As String
   pServerName As String
   Attributes As Long
End Type

Private Declare Function OpenPrinter Lib "winspool.drv" _
   Alias "OpenPrinterA" _
  (ByVal pPrinterName As String, _
   phPrinter As Long, _
   pDefault As Any) As Long
      
Private Declare Function ClosePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long
      
Private Declare Function GetPrinter Lib "winspool.drv" _
   Alias "GetPrinterA" _
  (ByVal hPrinter As Long, _
   ByVal Level As Long, _
   pPrinter As Any, _
   ByVal cbBuf As Long, _
   pcbNeeded As Long) As Long
   
Private Declare Function SetPrinter Lib "winspool.drv" _
   Alias "SetPrinterA" _
  (ByVal hPrinter As Long, _
   ByVal Level As Long, _
   pPrinter As Any, _
   ByVal Command As Long) As Long

Private Declare Function ShellExecute Lib "shell32.dll" _
   Alias "ShellExecuteA" _
  (ByVal hwnd As Long, _
   ByVal lpOperation As String, _
   ByVal lpFile As String, _
   ByVal lpParameters As String, _
   ByVal lpDirectory As String, _
   ByVal nShowCmd As Long) As Long

Private Declare Function lstrlen Lib "kernel32" _
   Alias "lstrlenA" _
  (ByVal lpString As Any) As Long

Private Declare Function lstrcpy Lib "kernel32" _
  Alias "lstrcpyA" _
 (ByVal lpString1 As Any, _
  ByVal lpString2 As Any) As Long




Private Sub Form_Load()

   Command1.Caption = "Rename Printer"
   Command2.Caption = "Open Printers Folder"
   
End Sub


Private Sub Command1_Click()

   Debug.Print RenamePrinter("Lexmark Z42 Color Jetprinter", "LMColourZ42")
  'Debug.Print RenamePrinter("LMColourZ42", "Lexmark Z42 Color Jetprinter") 
   
End Sub


Private Sub Command2_Click()

  'just opens the printers folder
   Dim sParams As String
   Dim sDirectory As String

   sParams = vbNullString
   sDirectory = vbNullString
   
   sParams = "/e,::{2227A280-3AEA-1069-A2DE-08002B30309D}"
   
   Call ShellExecute(0&, "Open", "explorer.exe", sParams, sDirectory, SW_SHOWNORMAL)

End Sub


Private Function RenamePrinter(sCurrentPrinterName As String, sNewPrinterName As String) As Boolean

   Dim hPrinter As Long
   Dim cbRequired As Long
   Dim cbBuffer As Long
   Dim pi4 As PRINTER_INFO_4
   Dim pd As PRINTER_DEFAULTS
   Dim buff() As Long
   
   pd.DesiredAccess = PRINTER_ACCESS_USE Or PRINTER_ACCESS_ADMINISTER
   
   If OpenPrinter(sCurrentPrinterName, hPrinter, pd) <> 0 Then
   
      If GetPrinter(hPrinter, PRINTER_LEVEL4, 0&, 0&, cbRequired) = 0 Then
      
         If cbRequired <> 0 Then
   
            ReDim buff(cbRequired \ 4) As Long
            cbBuffer = cbRequired
           
            If GetPrinter(hPrinter, PRINTER_LEVEL4, buff(0), cbBuffer, cbRequired) <> 0 Then
                        
              'assign to a PRINTER_INFO_4 structure
               pi4.pPrinterName = sNewPrinterName
               pi4.pServerName = GetStrFromPtrA(buff(1))
               pi4.Attributes = buff(2)

              'and rename
               RenamePrinter = SetPrinter(hPrinter, PRINTER_LEVEL4, pi4, 0&) <> 0
   
            End If  'GetPrinter
         End If  'cbRequired
      End If  'GetPrinter
   
      ClosePrinter hPrinter
      
   End If  'OpenPrinter
   
End Function


Private Function GetStrFromPtrA(ByVal lpszA As Long) As String

   GetStrFromPtrA = String$(lstrlen(ByVal lpszA), 0)
   Call lstrcpy(ByVal GetStrFromPtrA, ByVal lpszA)
   
End Function
 Comments
 

 
 

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