#CS
UDF title : _printers.au3
Available functions :
_AddPrinter
_AddWindowsPrinterConnection
_AddPrinterDriver
_AddTCPIPPrinterPort
_EnumPrinterConfiguration
_EnumPrinterDriver
_EnumPrinterProperties
_RemovePrinter
_RemovePrinterDriver
_RemoveTCPIPPrinterPort
_SetDefaultPrinter
#CE
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
; #FUNCTION# ======================================================================================
; Name...........: _AddPrinter
; Description ...: Adds a Windows printer.
; Syntax.........: _AddPrinter($sPrinterName, $sDriverName, $sPortName, $sLocation = '', $sComment = '')
; Parameters ....: $sPrinterName - Unique identifier of the printer on the system
; $sDriverName - Name of the Windows printer driver.
; $sPortName - Port that is used to transmit data to a printer
; $sLocation - Physical location of the printer (Example: Bldg. 38, Room 1164)
; $sComment - Comment for a print queue
; Return values .: Success - Returns 1
; Failure - Returns 0
; =================================================================================================
Func _AddPrinter($sPrinterName, $sDriverName, $sPortName, $sLocation = '', $sComment = '')
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$objPrinter = $objWMIService.Get("Win32_Printer").SpawnInstance_
If NOT IsObj($objPrinter) Then Return 0
$objPrinter.DriverName = $sDriverName
$objPrinter.PortName = $sPortName
$objPrinter.DeviceID = $sPrinterName
$objPrinter.Location = $sLocation
$objPrinter.Comment = $sComment
$objPrinter.Put_
Return 1
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _AddWindowsPrinterConnection
; Description ...: Provides a connection to an existing printer on the network, and adds it to the list of available printers.
; Syntax.........: _AddWindowsPrinterConnection($sPrinterPath)
; Parameters ....: $sPrinterPath - Path to the printer connection (must be aa UNC path)
; Return values .: Success - Returns 1
; Failure - Set @error
; @error : 5 - Access Denied
; 1801 - Invalid Printer Name
; 1930 - Incompatible Printer Driver
; =================================================================================================
Func _AddWindowsPrinterConnection($sPrinterPath)
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$objPrinter = $objWMIService.Get("Win32_Printer")
$errReturn = $objPrinter.AddPrinterConnection($sPrinterPath)
If $errReturn = 0 Then Return 1
Return SetError($errReturn)
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _AddPrinterDriver
; Description ...: Adds a printer driver.
; Syntax.........: _AddPrinterDriver($sDriverName, $sDriverPlatform, $sDriverPath, $sDriverInfName, $sVersion)
; Parameters ....: $sDriverName - Driver name for this printer
; $sDriverPlatform - Operating environments that the driver is intended for (Example: "Windows NT x86")
; $sDriverPath - Path for this printer driver -Example: "C:\\drivers\\pscript.dll")
; $sDriverInfName - Name of the INF file being used
; $sVersion - Operating system version for the printer driver
; 0 = Win9x
; 1 = Win351
; 2 = NT40
; 3 = Win2k
; Return values .: Success - Returns 1
; Failure - Set @error
; @error : 5 - Access denied
; 1797 - Access The printer driver is unknown
; =================================================================================================
Func _AddPrinterDriver($sDriverName, $sDriverPlatform, $sDriverPath, $sDriverInfName, $sVersion = "3")
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
If NOT IsObj($objWMIService) Then Return 0
If $sVersion <> "0" AND $sVersion <> "1" AND $sVersion <> "2" AND $sVersion <> "3" Then Return 0
$objWMIService.Security_.Privileges.AddAsString ("SeLoadDriverPrivilege", True)
$objDriver = $objWMIService.Get("Win32_PrinterDriver")
If NOT IsObj($objDriver) Then Return 0
$objDriver.Name = $sDriverName
$objDriver.SupportedPlatform = $sDriverPlatform
$objDriver.Version = $sVersion
$objDriver.DriverPath = $sDriverPath
$objDriver.Infname = $sDriverInfName
$errReturn = $objDriver.AddPrinterDriver($objDriver)
If $errReturn = 0 Then Return 1
Return SetError($errReturn)
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _AddTCPIPPrinterPort
; Description ...: Adds a TCP printer port.
; Syntax.........: _AddTCPIPPrinterPort($sPortName, $sPortIP, $sPortNunber)
; Parameters ....: $sPortName - Name of the port to create
; $sPortIP - IP Address of the port
; $sPortNunber - Port number
; Return values .: Success - Returns 1
; Failure - Returns 0
; =================================================================================================
Func _AddTCPIPPrinterPort($sPortName, $sPortIP, $sPortNunber)
$objWMIService = ObjGet("winmgmts:")
$objNewPort = $objWMIService.Get ("Win32_TCPIPPrinterPort").SpawnInstance_
If NOT IsObj($objNewPort) Then Return 0
$objNewPort.Name = $sPortName
$objNewPort.Protocol = 1
$objNewPort.HostAddress = $sPortIP
$objNewPort.PortNumber = $sPortNunber
$objNewPort.SNMPEnabled = True
$objNewPort.Put_
Return 1
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _EnumPrinterConfiguration
; Description ...: Enumerates the configuration of printers
; Syntax.........: _EnumPrinterConfiguration([$sPrinterName])
; Parameters ....: $sPrinterName - Name of the printer to retrieve configuration.
; Defaut "" returns configuration for all printers
; $sPrinterName can be a part of a printer name like "HP*"
; Return values .: Success - Returns an array containing the configuration (see $aResult[$i - 1][X] in the function)
; Failure - Returns 0
; =================================================================================================
Func _EnumPrinterConfiguration($sPrinterName = "")
Local $nbCols = 33
Local $aResult[1][$nbCols]
Local $wbemFlagReturnImmediately = 0x10
Local $wbemFlagForwardOnly = 0x20
Local $filter = ""
If $sPrinterName <> "" Then $filter = " Where Name like '" & $sPrinterName & "'"
$filter = StringReplace($filter, '*', '%')
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colPrinters = $objWMIService.ExecQuery ("Select * from Win32_PrinterConfiguration" & $filter, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If NOT IsObj($colPrinters) Then Return 0
$i = 0
For $objPrinter in $colPrinters
$i += 1
Redim $aResult[$i][$nbCols]
$aResult[$i - 1][0] = $objPrinter.BitsPerPel
$aResult[$i - 1][1] = $objPrinter.Caption
$aResult[$i - 1][2] = $objPrinter.Collate
$aResult[$i - 1][3] = $objPrinter.Color
$aResult[$i - 1][4] = $objPrinter.Copies
$aResult[$i - 1][5] = $objPrinter.Description
$aResult[$i - 1][6] = $objPrinter.DeviceName
$aResult[$i - 1][7] = $objPrinter.DisplayFlags
$aResult[$i - 1][8] = $objPrinter.DisplayFrequency
$aResult[$i - 1][9] = $objPrinter.DitherType
$aResult[$i - 1][10] = $objPrinter.DriverVersion
$aResult[$i - 1][11] = $objPrinter.Duplex
$aResult[$i - 1][12] = $objPrinter.FormName
$aResult[$i - 1][13] = $objPrinter.HorizontalResolution
$aResult[$i - 1][14] = $objPrinter.ICMIntent
$aResult[$i - 1][15] = $objPrinter.ICMMethod
$aResult[$i - 1][16] = $objPrinter.LogPixels
$aResult[$i - 1][17] = $objPrinter.MediaType
$aResult[$i - 1][18] = $objPrinter.Name
$aResult[$i - 1][19] = $objPrinter.Orientation
$aResult[$i - 1][20] = $objPrinter.PaperLength
$aResult[$i - 1][21] = $objPrinter.PaperSize
$aResult[$i - 1][22] = $objPrinter.PaperWidth
$aResult[$i - 1][23] = $objPrinter.PelsHeight
$aResult[$i - 1][24] = $objPrinter.PelsWidth
$aResult[$i - 1][25] = $objPrinter.PrintQuality
$aResult[$i - 1][26] = $objPrinter.Scale
$aResult[$i - 1][27] = $objPrinter.SettingID
$aResult[$i - 1][28] = $objPrinter.SpecificationVersion
$aResult[$i - 1][29] = $objPrinter.TTOption
$aResult[$i - 1][30] = $objPrinter.VerticalResolution
$aResult[$i - 1][31] = $objPrinter.XResolution
$aResult[$i - 1][32] = $objPrinter.YResolution
Next
Return $aResult
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _EnumPrinterDriver
; Description ...: Enumerates all installed printer drivers.
; Syntax.........: _EnumPrinterDriver([$sPrinterName])
; Parameters ....: $sPrinterName - Name of the printer to retrieve informations.
; Defaut "" returns informations for all printers
; $sPrinterName can be a part of a printer name like "HP*"
; Return values .: Success - Returns an array containing all informations (see $aResult[$i - 1][X] in the function)
; Failure - Returns 0
; =================================================================================================
Func _EnumPrinterDriver($sPrinterName = "")
Local $nbCols = 8
Local $aResult[1][$nbCols]
Local $wbemFlagReturnImmediately = 0x10
Local $wbemFlagForwardOnly = 0x20
Local $filter = ""
If $sPrinterName <> "" Then $filter = " Where Name like '" & $sPrinterName & "'"
$filter = StringReplace($filter, '*', '%')
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colPrinters = $objWMIService.ExecQuery ("Select * from Win32_PrinterDriver" & $filter, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If NOT IsObj($colPrinters) Then Return 0
$i = 0
For $objPrinter in $colPrinters
$i += 1
Redim $aResult[$i][$nbCols]
$aResult[$i - 1][0] = $objPrinter.ConfigFile
$aResult[$i - 1][1] = $objPrinter.DataFile
$aResult[$i - 1][2] = $objPrinter.DriverPath
$aResult[$i - 1][3] = _StringUnSplit ( $objPrinter.DependentFiles )
$aResult[$i - 1][4] = $objPrinter.HelpFile
$aResult[$i - 1][5] = $objPrinter.MonitorName
$aResult[$i - 1][6] = $objPrinter.Name
$aResult[$i - 1][7] = $objPrinter.SupportedPlatform
Next
Return $aResult
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _EnumPrinterProperties
; Description ...: Enumerates all installed printers for the user executing the script.
; Syntax.........: _EnumPrinterProperties([$sPrinterName])
; Parameters ....: $sPrinterName - Name of the printer to retrieve informations.
; Defaut "" returns informations for all printers
; $sPrinterName can be a part of a printer name like "HP*"
; Return values .: Success - Returns an array containing all informations (see $aResult[$i - 1][X] in the function)
; Failure - Returns 0
; =================================================================================================
Func _EnumPrinterProperties($sPrinterName = "")
Local $nbCols = 86
Local $aResult[1][$nbCols]
Local $wbemFlagReturnImmediately = 0x10
Local $wbemFlagForwardOnly = 0x20
Local $filter = ""
If $sPrinterName <> "" Then $filter = " Where DeviceID like '" & $sPrinterName & "'"
$filter = StringReplace($filter, '*', '%')
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colPrinters = $objWMIService.ExecQuery ("Select * from Win32_Printer" & $filter, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If NOT IsObj($colPrinters) Then Return 0
$i = 0
For $objPrinter in $colPrinters
$i += 1
Redim $aResult[$i][$nbCols]
$aResult[$i - 1][0] = $objPrinter.Attributes
$aResult[$i - 1][1] = $objPrinter.Availability
$aResult[$i - 1][2] = _StringUnSplit ($objPrinter.AvailableJobSheets )
$aResult[$i - 1][3] = $objPrinter.AveragePagesPerMinute
$aResult[$i - 1][4] = _StringUnSplit ($objPrinter.Capabilities)
$aResult[$i - 1][5] = _StringUnSplit ($objPrinter.CapabilityDescriptions)
$aResult[$i - 1][6] = $objPrinter.Caption
$aResult[$i - 1][7] = _StringUnSplit ($objPrinter.CharSetsSupported)
$aResult[$i - 1][8] = $objPrinter.Comment
$aResult[$i - 1][9] = $objPrinter.ConfigManagerErrorCode
$aResult[$i - 1][10] = $objPrinter.ConfigManagerUserConfig
$aResult[$i - 1][11] = $objPrinter.CreationClassName
$aResult[$i - 1][12] = _StringUnSplit ($objPrinter.CurrentCapabilities)
$aResult[$i - 1][13] = $objPrinter.CurrentCharSet
$aResult[$i - 1][14] = $objPrinter.CurrentLanguage
$aResult[$i - 1][15] = $objPrinter.CurrentMimeType
$aResult[$i - 1][16] = $objPrinter.CurrentNaturalLanguage
$aResult[$i - 1][17] = $objPrinter.CurrentPaperType
$aResult[$i - 1][18] = $objPrinter.Default
$aResult[$i - 1][19] = _StringUnSplit ($objPrinter.DefaultCapabilities)
$aResult[$i - 1][20] = $objPrinter.DefaultCopies
$aResult[$i - 1][21] = $objPrinter.DefaultLanguage
$aResult[$i - 1][22] = $objPrinter.DefaultMimeType
$aResult[$i - 1][23] = $objPrinter.DefaultNumberUp
$aResult[$i - 1][24] = $objPrinter.DefaultPaperType
$aResult[$i - 1][25] = $objPrinter.DefaultPriority
$aResult[$i - 1][26] = $objPrinter.Description
$aResult[$i - 1][27] = $objPrinter.DetectedErrorState
$aResult[$i - 1][28] = $objPrinter.DeviceID
$aResult[$i - 1][29] = $objPrinter.Direct
$aResult[$i - 1][30] = $objPrinter.DoCompleteFirst
$aResult[$i - 1][31] = $objPrinter.DriverName
$aResult[$i - 1][32] = $objPrinter.EnableBIDI
$aResult[$i - 1][33] = $objPrinter.EnableDevQueryPrint
$aResult[$i - 1][34] = $objPrinter.ErrorCleared
$aResult[$i - 1][35] = $objPrinter.ErrorDescription
$aResult[$i - 1][36] = _StringUnSplit ($objPrinter.ErrorInformation)
$aResult[$i - 1][37] = $objPrinter.ExtendedDetectedErrorState
$aResult[$i - 1][38] = $objPrinter.ExtendedPrinterStatus
$aResult[$i - 1][39] = $objPrinter.Hidden
$aResult[$i - 1][40] = $objPrinter.HorizontalResolution
$aResult[$i - 1][41] = $objPrinter.InstallDate
$aResult[$i - 1][42] = $objPrinter.JobCountSinceLastReset
$aResult[$i - 1][43] = $objPrinter.KeepPrintedJobs
$aResult[$i - 1][44] = _StringUnSplit ($objPrinter.LanguagesSupported)
$aResult[$i - 1][45] = $objPrinter.LastErrorCode
$aResult[$i - 1][46] = $objPrinter.Local
$aResult[$i - 1][47] = $objPrinter.Location
$aResult[$i - 1][48] = $objPrinter.MarkingTechnology
$aResult[$i - 1][49] = $objPrinter.MaxCopies
$aResult[$i - 1][50] = $objPrinter.MaxNumberUp
$aResult[$i - 1][51] = $objPrinter.MaxSizeSupported
$aResult[$i - 1][52] = _StringUnSplit ($objPrinter.MimeTypesSupported)
$aResult[$i - 1][53] = $objPrinter.Name
$aResult[$i - 1][54] = _StringUnSplit ($objPrinter.NaturalLanguagesSupported)
$aResult[$i - 1][55] = $objPrinter.Network
$aResult[$i - 1][56] = _StringUnSplit ($objPrinter.PaperSizesSupported)
$aResult[$i - 1][57] = _StringUnSplit ($objPrinter.PaperTypesAvailable)
$aResult[$i - 1][58] = $objPrinter.Parameters
$aResult[$i - 1][59] = $objPrinter.PNPDeviceID
$aResult[$i - 1][60] = $objPrinter.PortName
$aResult[$i - 1][61] = _StringUnSplit ($objPrinter.PowerManagementCapabilities)
$aResult[$i - 1][62] = $objPrinter.PowerManagementSupported
$aResult[$i - 1][63] = _StringUnSplit ($objPrinter.PrinterPaperNames)
$aResult[$i - 1][64] = $objPrinter.PrinterState
$aResult[$i - 1][65] = $objPrinter.PrinterStatus
$aResult[$i - 1][66] = $objPrinter.PrintJobDataType
$aResult[$i - 1][67] = $objPrinter.PrintProcessor
$aResult[$i - 1][68] = $objPrinter.Priority
$aResult[$i - 1][69] = $objPrinter.Published
$aResult[$i - 1][70] = $objPrinter.Queued
$aResult[$i - 1][71] = $objPrinter.RawOnly
$aResult[$i - 1][72] = $objPrinter.SeparatorFile
$aResult[$i - 1][73] = $objPrinter.ServerName
$aResult[$i - 1][74] = $objPrinter.Shared
$aResult[$i - 1][75] = $objPrinter.ShareName
$aResult[$i - 1][76] = $objPrinter.SpoolEnabled
$aResult[$i - 1][77] = $objPrinter.StartTime
$aResult[$i - 1][78] = $objPrinter.Status
$aResult[$i - 1][79] = $objPrinter.StatusInfo
$aResult[$i - 1][80] = $objPrinter.SystemCreationClassName
$aResult[$i - 1][81] = $objPrinter.SystemName
$aResult[$i - 1][82] = $objPrinter.TimeOfLastReset
$aResult[$i - 1][83] = $objPrinter.UntilTime
$aResult[$i - 1][84] = $objPrinter.VerticalResolution
$aResult[$i - 1][85] = $objPrinter.WorkOffline
Next
Return $aResult
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _RemovePrinter
; Description ...: Removes a printer.
; Syntax.........: _RemovePrinter($sPrinterName)
; Parameters ....: $sPrinterName - Name of the printer to remove (use \\server\printerShare for shared printers)
; Return values .: Success - Returns 1
; Failure - Returns 0
; =================================================================================================
Func _RemovePrinter($sPrinterName)
$strComputer = "."
$sPrinterName = StringReplace($sPrinterName, "\", "\\")
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery ("Select * from Win32_Printer where DeviceID = '" & $sPrinterName & "'")
If NOT IsObj($colInstalledPrinters) Then Return 0
For $objPrinter in $colInstalledPrinters
$objPrinter.Delete_
Next
Return 1
Endfunc
; #FUNCTION# ======================================================================================
; Name...........: _RemovePrinterDriver
; Description ...: Removes a printer driver.
; Syntax.........: _RemovePrinterDriver($sDriverName)
; Parameters ....: $sDriverName - Name of the printer driver
; Return values .: Success - Returns 1
; Failure - Returns 0
; =================================================================================================
Func _RemovePrinterDriver($sDriverName)
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery ("Select * from Win32_PrinterDriver Where Name like '" & $sDriverName & ",%'")
If NOT IsObj($colInstalledPrinters) Then Return 0
For $objPrinter in $colInstalledPrinters
$objPrinter.Delete_
Next
Return 1
EndFunc
; #FUNCTION# ======================================================================================
; Name...........: _RemoveTCPIPPrinterPort
; Description ...: Removes a TCP printer port.
; Syntax.........: _RemoveTCPIPPrinterPort($sPortName)
; Parameters ....: $sPortName - Name of the port to remove
; Return values .: Success - Returns 1
; Failure - Returns 0
; =================================================================================================
Func _RemoveTCPIPPrinterPort($sPortName)
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery ("Select * from Win32_TCPIPPrinterPort where Name = '" & $sPortName & "'")
If NOT IsObj($colInstalledPrinters) Then Return 0
For $objPrinter in $colInstalledPrinters
$objPrinter.Delete_
Next
Return 1
Endfunc
; #FUNCTION# ======================================================================================
; Name...........: _SetDefaultPrinter
; Description ...: Sets the default system printer
; Syntax.........: _SetDefaultPrinter($sPrinterName)
; Parameters ....: $sPrinterName - Name of the printer
; Return values .: Success - Returns 1
; Failure - Returns 0
; =================================================================================================
Func _SetDefaultPrinter($sPrinterName)
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colPrinter = $objWMIService.ExecQuery ("Select * from Win32_Printer where DeviceID = '" & $sPrinterName & "'")
If NOT IsObj($colPrinter) Then Return 0
For $objPrinter in $colPrinter
$objPrinter.SetDefaultPrinter
Next
Return 1
EndFunc
Func _ErrFunc($oError)
Local $txt = "Error number : " & $oError.number & @CRLF & _
"WinDescription:" & @TAB & $oError.windescription & @CRLF & _
"Description : " & @TAB & $oError.description & @CRLF & _
"Source : " & @TAB & $oError.source
MsgBox(16, "Error", $txt)
EndFunc ;==>_ErrFunc
Func _StringUnSplit($aVal, $sSep = ";")
If NOT IsArray($aVal) Then Return 0
Local $sReturn = ""
For $i = 0 To UBound($aVal) - 1
If $i = 0 Then
$sReturn = $aVal[$i]
Else
$sReturn &= $sSep & $aVal[$i]
EndIf
Next
Return $sReturn
EndFunc