Sign in to follow this  
Followers 0
Lococobra

Retrieve printer queue

5 posts in this topic

I can't figure out how to retrieve the printer queue for a network printer using autoit. I'm open to using command line stuff, anything that will get me the same info as is listed in "Printers and Faxes". Also, I don't necessarily know the name of the printer, since I'm trying to write something that can be used on multiple computers. So in addition to getting the print queue, is there a way to reliably find the default printer?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I can't figure out how to retrieve the printer queue for a network printer using autoit. I'm open to using command line stuff, anything that will get me the same info as is listed in "Printers and Faxes". Also, I don't necessarily know the name of the printer, since I'm trying to write something that can be used on multiple computers. So in addition to getting the print queue, is there a way to reliably find the default printer?

This link shows how you can see the queue using the command line.

Here are 3 ways to get the default printer. The wmi is always very slow for me.

MsgBox(0, "Default printer is (method 1)", GetDefaultPrinter1())
MsgBox(0, "Default printer is (method 2)", GetDefaultPrinter2())
MsgBox(0, "Default printer is (method 3)", GetDefaultPrinter3())


Func GetDefaultPrinter1()
;by martin
    Local $szDefPrinterName
    Local $Size
    $namesize = DllStructCreate("dword")
    DllCall("winspool.drv", "int", "GetDefaultPrinter", "str", '', "ptr", DllStructGetPtr($namesize))
    $pname = DllStructCreate("char[" & DllStructGetData($namesize, 1) & "]")
    DllCall("winspool.drv", "int", "GetDefaultPrinter", "ptr", DllStructGetPtr($pname), "ptr", DllStructGetPtr($namesize))
    Return DllStructGetData($pname, 1)
EndFunc ;==>GetDefaultPrinter1


Func GetDefaultPrinter2()
;by unknown, probably not martin

    Local $result, $strComputer, $np, $colEvents
    $result = ''

    $strComputer = "."
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

    $colEvents = $objWMIService.ExecQuery _
            ("Select * From Win32_Printer Where Default = TRUE");TRUE
    $np = 0

    For $objPrinter In $colEvents
        $result = $objPrinter.DeviceID
    Next

    Return $result
EndFunc ;==>GetDefaultPrinter2

Func GetDefaultPrinter3()
;by martin
    $defp = RegRead("HKCU\Printers", "DeviceOld")
    Return StringLeft($defp, StringInStr($defp, ',') - 1)
EndFunc ;==>GetDefaultPrinter3
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Sorry it took me so long to answer.

I like your methods of retrieving the default printer, but I've seen that "Command Line Printer Control" page before and I can't seem to get it to do what I want. The closest I can get to actually retrieving print job information using the suggested methods was something like

RUNDLL32 PRINTUI.DLL,PrintUIEntry /o /n\\machine\printer

Which really doesn't get me anywhere, it just shows me the window that I'd get by going to "Printers and Faxes".

How would I go about actually retrieving print job information, such as the document name, status, owner... etc?

Share this post


Link to post
Share on other sites

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output &= "Computer: " & $strComputer  & @CRLF
$Output &= "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PrintJob", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output &= "Caption: " & $objItem.Caption & @CRLF
      $Output &= "DataType: " & $objItem.DataType & @CRLF
      $Output &= "Description: " & $objItem.Description & @CRLF
      $Output &= "Document: " & $objItem.Document & @CRLF
      $Output &= "DriverName: " & $objItem.DriverName & @CRLF
      $Output &= "ElapsedTime: " & WMIDateStringToDate($objItem.ElapsedTime) & @CRLF
      $Output &= "HostPrintQueue: " & $objItem.HostPrintQueue & @CRLF
      $Output &= "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output &= "JobId: " & $objItem.JobId & @CRLF
      $Output &= "JobStatus: " & $objItem.JobStatus & @CRLF
      $Output &= "Name: " & $objItem.Name & @CRLF
      $Output &= "Notify: " & $objItem.Notify & @CRLF
      $Output &= "Owner: " & $objItem.Owner & @CRLF
      $Output &= "PagesPrinted: " & $objItem.PagesPrinted & @CRLF
      $Output &= "Parameters: " & $objItem.Parameters & @CRLF
      $Output &= "PrintProcessor: " & $objItem.PrintProcessor & @CRLF
      $Output &= "Priority: " & $objItem.Priority & @CRLF
      $Output &= "Size: " & $objItem.Size & @CRLF
      $Output &= "StartTime: " & WMIDateStringToDate($objItem.StartTime) & @CRLF
      $Output &= "Status: " & $objItem.Status & @CRLF
      $Output &= "StatusMask: " & $objItem.StatusMask & @CRLF
      $Output &= "TimeSubmitted: " & WMIDateStringToDate($objItem.TimeSubmitted) & @CRLF
      $Output &= "TotalPages: " & $objItem.TotalPages & @CRLF
      $Output &= "UntilTime: " & WMIDateStringToDate($objItem.UntilTime) & @CRLF
      ;if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      ConsoleWrite($Output & @CRLF)
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PrintJob" )
Endif


Func WMIDateStringToDate($dtmDate)
    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

Share this post


Link to post
Share on other sites

Wow... that's awesome. Much closer than I ever got... it seems like some of the data isn't right though. Totalpages almost always returns 0 or 1... but sometimes 2. It doesn't seem to have anything to do with the actual number of pages. I'll keep looking in to it though, at least now I know where I'm headed.

Share this post


Link to post
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
Sign in to follow this  
Followers 0