Hope this helps wrote this last night for a project am working on. It uses WMI for retrieving the data and added some other things to it. I've tested on xp seemed to work didn't have alot time test it on other OS. To use need Beta version for Autoit. Enjoy
#include <Array.au3>
;Global Variables
Const $wbemFlagReturnImmediately = 0x10
Const $wbemFlagForwardOnly = 0x20
Const $strComputer = "."
$count = 0
$sHold = ""
$array = ""
;HOW TO USE:
;$ARRAY = Enumprinter()
Func EnumPrinter()
local $colItems = ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
For $objItem In $colItems
If $objitem.DeviceID <> "" Then
$sHold = $sHold & $objItem.DeviceID & ","
$count = $count + 1
Endif
Next
If $sHold = "" Then
Return $sHold
else
$array = StringSplit($sHold,",")
_ArrayDelete($array,$count + 1)
_arrayDelete($array,0)
Return $array
EndIf
Endif
EndFunc
; HOW TO USE:
; $DefaultPrinter = DefaultPrinter()
; Will return the default printer name if it doesnt have a
; Default printer it will return 0
Func DefaultPrinter()
Local $colItems = ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
For $objItem In $colItems
;msgbox(0,"",$Objitem.Default)
If $objitem.Default <> 0 Then
$result = $objItem.DeviceID
;MsgBox(0,"",$objitem.deviceID)
Return $result
Endif
Next
Endif
Return 0
EndFunc
;Add A global printer for all profiles
; $default 1=Default use either "" blank statement or 0
Func AddGlobalPrinter($Server,$Printer,$Default)
Run(@ComSpec & " /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /n\\" & $Server & "\" & $printer,"",@SW_HIDE)
RestartPrintSpool()
If $Default = 1 Then
;msgbox(0,"",$printer)
SetDefault("\\" & $Server & "\" & $Printer)
;RestartPrintSpool()
Elseif $Default = 0 Then
Elseif $Default = "" Then
EndIF
EndFunc
;Remove Printer that was added by AddGlobalPrinter func otherwise if printer
;was installed manual it will fail
Func RemoveGlobalPrinter($Server,$Printer)
Run(@ComSpec & " /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /n\\" & $Server & "\" & $printer,"",@SW_HIDE)
RestartPrintSpool()
EndFunc
;Set the Default Printer
Func SetDefault($Printer)
Run(@ComSpec & " /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n " & $Printer , "",@SW_HIDE)
EndFunc
;Helper function for restarting of print spooler
Func RestartPrintSpool()
local $colItems = ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = 'Spooler'", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
For $objItem In $colItems
; msgbox(0,"",$objitem.Name)
If $objitem.State = "Running" Then
msgbox(0,"",$ObjItem.state)
$some = $objItem.StopService()
sleep(5000)
$some1 = $objItem.StartService()
ElseIF $objitem.State = "Stopped" Then
; msgbox(0,"",$ObjItem.state)
$ObjItem.StartService()
EndIf
Next
EndIf
EndFunc
Sorry if its hard to understand like i said i just wrote and didn't spend alot of time cleaning it up.
Also put alot of msgbox for testing so i might of missed acouple and will pop up when you it.