Jump to content

Getting Local Printer Information Using Autoit


ds10025
 Share

Recommended Posts

Hi

I'm in the process of writing a script that that allows users to install local printer drivers for our network. I can now install local printers drivers using Run() with runndll32.

I want to use Auoit to ccheck if the printer driver is already installed or not.

EG if printer name " HP laserjet 4100 PS" and LPT2: is mapped to the printer.

Then

Display "printer already installed"

Else

Install the printer driver and assign LPT2: to the printer.

Any ideas?

Link to comment
Share on other sites

@ds10025

#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

Maybe this can get you started.

regards

ptrex

Link to comment
Share on other sites

ptrex, great class!

How come I can't set the default to be networked printer?

I used

$ARRAY = Enumprinter()
_ArrayDisplay($ARRAY,"Printers")
ControlSend("Untitled - Notepad","","Edit1",$ARRAY[7])

to obtain the name of the printer I want.

However, when I set the default using that printer name I get an error telling me basically that the name is wrong.

SetDefault("\\WEB_SERVER\hp LaserJet 2420 PCL6")

A decision is a powerful thing
Link to comment
Share on other sites

By looking at http://www.autoitscript.com/forum/index.php?showtopic=29127 I found out that adjusting your script to

Run(@ComSpec & " /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /q /y /n " & '"' & $Printer & '"', "", @SW_HIDE)

from

Run(@ComSpec & " /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n " & $Printer , "",@SW_HIDE)

works, but I don't know why. What does the added quotations and /q mean?

A decision is a powerful thing
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...