Jump to content

Get Default Printer / All Printers


ReFran
 Share

Recommended Posts

Hello,

I searched for a script to get all printers (incl. Networkprinters) and the default printer.

I found only a Script from CyberSlug

http://www.autoitscript.com/forum/index.ph...998entry79998

which stated only the local printers.

So attached a simple script, which state all printers and mark the default printer. It uses the WSH Network object, that is installed on all >Win95 computers.

Improvements or more AutoIT3 native Methods are welcome.

Best regards, Reinhard

Global $defPrtNm
Global $prtList

MsgBox(0,"",_GetDefaultPrinter())
MsgBox(0,"",_GetAllPrinters())


;;; Get default Printer;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func _GetDefaultPrinter() ; CyberSlug - 18 Nov 2004
   Local $key, $default
   If @OSType = "WIN32_WINDOWS" Then
      $key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers"
      $defPrtNm = RegRead("HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers","Default")
   Else;WIN_NT type
      $key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers"
      $default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device")
      $defPrtNm = StringLeft($default, StringInStr($default,",")-1)
      return $defPrtNm
EndIf
EndFunc  

;;; Get all printers;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func _GetAllPrinters()
    $oWSN   = ObjCreate("WScript.Network")
    $oPrt   = $oWSN.EnumPrinterConnections
      For $i = 0 To $oPrt.Count-1 Step 2
         if $defPrtNm = $oPrt($i+1) then 
             $pm = ".=> " 
             $InpDef= ($i+2)/2 
         else 
             $pm = ".   " 
         endif
        $PrtList = $PrtList & @crlf & ($i+2)/2 & $pm & $oPrt($i+1) 
      Next
    return $PrtList
EndFunc
Link to comment
Share on other sites

search for :

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

DOCUMENT: AdPrintX Readme

AUTHOR: James Hart (watermarkdesign@bigpond.com)

CREATED: 3 May 2002

MODIFIED: 27 June 2003

VERSION: 1.0.0.002

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

it can do everything you wan with local and network printer!

it simple to integrate in autoit script!

GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.

Link to comment
Share on other sites

search for :

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

DOCUMENT: AdPrintX Readme

AUTHOR: James Hart (watermarkdesign@bigpond.com)

CREATED: 3 May 2002

MODIFIED: 27 June 2003

VERSION: 1.0.0.002

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

it can do everything you wan with local and network printer!

it simple to integrate in autoit script!

I don't want extra dlls or whatever. Above you have all on board. The other way would only be Regread, like CyberSlug did it, where I would prefer to read from HKCU instead of HKLM. Also _SetDefaulPrinter is possible. Will look for it by time.

Best regards, Reinhard

PS: You forgot to set the five Stars B)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Hello,

I searched for a script to get all printers (incl. Networkprinters) and the default printer.

I found only a Script from CyberSlug

http://www.autoitscript.com/forum/index.ph...5533;entry79998

which stated only the local printers.

So attached a simple script, which state all printers and mark the default printer. It uses the WSH Network object, that is installed on all >Win95 computers.

Improvements or more AutoIT3 native Methods are welcome.

Best regards, Reinhard

Nice util. It works great.

Cheers.. :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...