Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Get printer status?


  • Please log in to reply
3 replies to this topic
n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Hi all,

I'm trying to get the status of a printer based on it's name. I found a couple of postings, and a WMI VBScript. I can't get either to work. The script by Lexikos doesn't return anything, and the COM version doesn't do what I want.

Anybody?
/*
strComputer = "."
objWMIService = GetObject(StrCat("winmgmts:\\" , strComputer , "\root\cimv2"))
colPrinters = objWMIService.ExecQuery("Select * from Win32_PrintJob")
list = ""
ForEach objPrinter In colPrinters
list = StrCat(list, objPrinter.JobId, " | ", objPrinter.JobStatus, @TAB)
Next
AskItemlist("Printer Job Status",StrTrim(list), @TAB, @UNSORTED, @SINGLE)
colPrinters = 0
objWMIService = 0 
*/

#include %A_ScriptDir%\COM.ahk

COM_Init()
COM_Error(false)
OnExit, DoExit

psvc :=   COM_GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
pset :=   COM_Invoke(psvc, "ExecQuery",  "SELECT * FROM Win32_PrintJob")
penm :=   COM_Invoke(pset, "_NewEnum")
Loop, %   COM_Invoke(pset, "Count")
  If   COM_Enumerate(penm,pobj)=0
  {
     If  Not   Property
        MsgBox % COM_Invoke(pobj, "GetObjectText_")
     Else   sResult.=COM_Invoke(pobj, Property) . "`n"
     COM_Release(pobj)
  }
COM_Release(penm)
COM_Release(pset)
COM_Release(psvc)
MsgBox % sResult


Return

DoExit:
COM_Term()
ExitApp
Return

/*
; http://www.autohotkey.com/forum/topic16632.html
; Sean

WMI_Query(Namespace, Class, Property = "")
{
   psvc :=   COM_GetObject("winmgmts:{impersonationLevel=impersonate}!" . Namespace)
   pset :=   COM_Invoke(psvc, "ExecQuery",  "SELECT * FROM " . Class)
   penm :=   COM_Invoke(pset, "_NewEnum")
   Loop, %   COM_Invoke(pset, "Count")
      If   COM_Enumerate(penm,pobj)=0
      {
         If  Not   Property
            MsgBox % COM_Invoke(pobj, "GetObjectText_")
         Else   sResult.=COM_Invoke(pobj, Property) . "`n"
         COM_Release(pobj)
      }
   COM_Release(penm)
   COM_Release(pset)
   COM_Release(psvc)
   Return   sResult
}
*/

/*
; http://www.autohotkey.com/forum/post-159778.html#159778
; Lexikos

; NOT WORKING!

GetPrinterStatus(printer)
{
  DllCall("LoadLibrary","str","winspool.drv")
  if ! DllCall("winspool.drv\OpenPrinterA","str", printer,"uint*",hPrinter,"uint",0)
  {
      MsgBox OpenPrinter failed.`nErrorLevel: %ErrorLevel%`nLastError: %A_LastError%
      return
  }
  DllCall("winspool.drv\GetPrinterA","uint",hPrinter,"uint",2,"uint",0,"uint",0,"uint*",req)
  if (A_LastError = 122)
  { ; ERROR_INSUFFICIENT_BUFFER
      VarSetCapacity(pi,req)
      if DllCall("winspool.drv\GetPrinterA","uint",hPrinter,"uint",2,"uint",&pi,"uint",req,"uint*",req)
          Status := NumGet(pi,72)
  }
  if Status =
      MsgBox GetPrinter failed.`nErrorLevel: %ErrorLevel%`nLastError: %A_LastError%
  
  DllCall("winspool.drv\ClosePrinter","uint",hPrinter)
  
  Return Status
  
  /*
  PRINTER_STATUS_PAUSED            = 0x00000001
  PRINTER_STATUS_ERROR             = 0x00000002
  PRINTER_STATUS_PENDING_DELETION  = 0x00000004
  PRINTER_STATUS_PAPER_JAM         = 0x00000008
  PRINTER_STATUS_PAPER_OUT         = 0x00000010
  PRINTER_STATUS_MANUAL_FEED       = 0x00000020
  PRINTER_STATUS_PAPER_PROBLEM     = 0x00000040
  PRINTER_STATUS_OFFLINE           = 0x00000080
  PRINTER_STATUS_IO_ACTIVE         = 0x00000100
  PRINTER_STATUS_BUSY              = 0x00000200
  PRINTER_STATUS_PRINTING          = 0x00000400
  PRINTER_STATUS_OUTPUT_BIN_FULL   = 0x00000800
  PRINTER_STATUS_NOT_AVAILABLE     = 0x00001000
  PRINTER_STATUS_WAITING           = 0x00002000
  PRINTER_STATUS_PROCESSING        = 0x00004000
  PRINTER_STATUS_INITIALIZING      = 0x00008000
  PRINTER_STATUS_WARMING_UP        = 0x00010000
  PRINTER_STATUS_TONER_LOW         = 0x00020000
  PRINTER_STATUS_NO_TONER          = 0x00040000
  PRINTER_STATUS_PAGE_PUNT         = 0x00080000
  PRINTER_STATUS_USER_INTERVENTION = 0x00100000
  PRINTER_STATUS_OUT_OF_MEMORY     = 0x00200000
  PRINTER_STATUS_DOOR_OPEN         = 0x00400000
  PRINTER_STATUS_SERVER_UNKNOWN    = 0x00800000
  PRINTER_STATUS_POWER_SAVE        = 0x01000000
  PRINTER_STATUS_SERVER_OFFLINE    = 0x02000000
  PRINTER_STATUS_DRIVER_UPDATE_NEEDED    = 0x04000000
  ;*/
  
}

*/


Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
is it a network printer, if so the calling method is such (for the dllcall)

GetPrinterStatus("\\printserver\printer")

Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
It was known for some time that GetPrinter function had a bug. I suggest to try after replacing
VarSetCapacity(pi,req)
to
VarSetCapacity(pi,req*=3,0)
(Not tested)

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Ah, much obliged, that seems to do the trick... 8)