Sign in to follow this  
Followers 0

WMI QUERY help

20 posts in this topic

Posted

am trying to select the paper types supported through WMI for a particular printer, so i do this

$Name="Adobe PDF" ; this name it set programatically, i did this to make u understand
$colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $Name & "'")
MsgBox(4160, "Information", _ArrayToString($colItems,@tab ))

i get a null/empty message in the message box. what am i doing wrong?

Share this post


Link to post
Share on other sites



Posted

Try this:

Global $Name = "Adobe PDF" ; this name it set programatically, i did this to make u understand
Global $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
Global $colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $Name & "'")
Global $paper
For $objItem In $colItems
    $paper &= $objItem.PaperTypesAvailable & " "
Next
MsgBox(0, "Information", "Paper types available: " & $paper)

Br,

UEZ

Share this post


Link to post
Share on other sites

Posted

UEZ

i tried your code, it dosent work, see what i have so far

Func List1Click()
    Dim $Prt
    ; get selected text index
    $selIndex=_GUICtrlListBox_GetCurSel($List1)
    ;get selected text string from index
    $selText=_GUICtrlListBox_GetText($List1, $selIndex)
    ; get the paper sizes for this printer
    MsgBox(4160, "Information", $selText)
    $strComputer = "."
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $selText & "'")
    ;$strPaperSizesSupported = $colItems.PaperSizesSupported
    ;For $objItem in $colItems 
    ;_ArrayToString($avArray, @TAB, 1, 7)
    MsgBox(4160, "Information", _ArrayToString($colItems,@tab ))

    ;Next
EndFunc

Share this post


Link to post
Share on other sites

Posted

Look to my example how to enumerate values from $colItems object!

The way you are doing is wrong - $colItems is not an array!

Br,

UEZ

Share this post


Link to post
Share on other sites

Posted

but am not getting any result from your code,

Share this post


Link to post
Share on other sites

Posted (edited)

Probably value is empty.

When I start it on my PC PaperTypesAvailable is always emtpy although I have some printers installed!

If you replace PaperTypesAvailable with PrinterPaperNames will probably get some resultes!

Use Scriptomatic to get an overview of all methods and properties of Win32_Printer from cimv2!

Br,

UEZ

Edited by UEZ

Share this post


Link to post
Share on other sites

Posted

i have a vbscript am trying to convert to autoit, but i used abpi in the script not WMI, can you help convert api calls

Share this post


Link to post
Share on other sites

Posted

i still don't get anything, please help

Share this post


Link to post
Share on other sites

Posted (edited)

Hmmm, then try this:

Global $Name = "Adobe PDF" ; this name it set programatically, i did this to make u understand
Global $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
Global $colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $Name & "'", "WQL", 0x30)
Global $paper
For $objItem In $colItems
    For $x = 0 To UBound($objItem.PrinterPaperNames) - 1
        $paper &= $objItem.PrinterPaperNames($x) & ", "
    Next
Next
$paper = StringMid($paper, 1, StringLen($paper) - 2)
MsgBox(0, "Information", "Printer Paper Names: " & $paper)

Shorter:

#include <Array.au3>
Global $Name = "Adobe PDF" ; this name it set programatically, i did this to make u understand
Global $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
Global $colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $Name & "'", "WQL", 0x30)
Global $paper, $aPaper
For $objItem In $colItems
    $aPaper = $objItem.PrinterPaperNames
    $paper = _ArrayToString($aPaper, ", ")
Next
MsgBox(0, "Information", "Printer Paper Names: " & $paper)

Br,

UEZ

Edited by UEZ

Share this post


Link to post
Share on other sites

Posted

thank you so much for your help, but one last help. Autoit crash when i run this code

;listbox1 event handler
Func List1Click()
    Dim $Prt
    ; get selected text index
    $selIndex=_GUICtrlListBox_GetCurSel($List1)
    ;get selected text string from index
    Global $Name=_GUICtrlListBox_GetText($List1, $selIndex)
    ; get the paper sizes for this printer
    _GUICtrlListBox_ResetContent($List2)
    $strComputer = "."
    Global $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
    Global $colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $Name & "'", "WQL", 0x30)
    Global $paper, $aPaper
    _GUICtrlListBox_BeginUpdate($List2)
    For $objItem In $colItems
        $aPaper = $objItem.PrinterPaperNames
        _GUICtrlListBox_AddString($List2, $aPaper )
    Next
_GUICtrlListBox_EndUpdate($List2)
EndFunc

Share this post


Link to post
Share on other sites

Posted

Maybe this will help you:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
$hGUI = GUICreate("Test", 600, 400)
$List1 = GUICtrlCreateList("", 0, 0, 300, 350)
GUICtrlSetData(-1, "Microsoft XPS Document Writer")
_GUICtrlListBox_SetSel(-1, 0)
$List2 = GUICtrlCreateList("", 300, 0, 300, 400)
$hButton = GUICtrlCreateButton("List Papers", 50, 360)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case  $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            Exit
        Case $hButton
            List1Click()
    EndSwitch
WEnd

;listbox1 event handler
Func List1Click()
    ; get selected text index
    Local $selIndex=_GUICtrlListBox_GetCurSel($List1)
    ;get selected text string from index
   Local $Name=_GUICtrlListBox_GetText($List1, $selIndex)
    ; get the paper sizes for this printer
    _GUICtrlListBox_ResetContent($List2)
   Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
   Local $colItems =  $objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & $Name & "'", "WQL", 0x30)
   Local $paper, $aPaper
    _GUICtrlListBox_BeginUpdate($List2)
    For $objItem In $colItems
        For $x = 0 To UBound($objItem.PrinterPaperNames) - 1
            _GUICtrlListBox_AddString($List2, $objItem.PrinterPaperNames($x))
        Next
    Next
_GUICtrlListBox_EndUpdate($List2)
EndFunc

Br,

UEZ

Share this post


Link to post
Share on other sites

Posted

thank you very much, am grateful

Share this post


Link to post
Share on other sites

Posted

Hello,

I face one more problem. Am trying to get the papersizes in millimeter, but you solution returns series of 1s and some other digits. see what i have done

Share this post


Link to post
Share on other sites

Posted

Have a look here to the table what the digits mean: http://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

PaperSizesSupported

Data type: uint16 array

Access type: Read-only

Array of the paper types that the printer supports. This property is inherited from CIM_Printer.

...

Br,

UEZ

Share this post


Link to post
Share on other sites

Posted

thanks for responding,

I am not getting the result i need from the WMI win32_printer class. am using the DevicecapabilitiesA api call now.

i checked out the reference you posted, but the papersizes is supposed to be retrieved in point(x,y) x=width in millimeters y=height in millimeters.

Share this post


Link to post
Share on other sites

Posted

From the page that UEZ referenced, the paper sizes are interpreted from the returned value. It doesn't return the actual paper sizes it returns a reference to the paper sizes. Search on that page for PaperSizesSupported and you will see about 3/4 down the page what the return values should be.

Share this post


Link to post
Share on other sites

Posted (edited)

You can use Win32_PrinterConfiguration to get the paper size:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
$hGUI = GUICreate("Test", 600, 400)
$List1 = GUICtrlCreateList("", 0, 0, 300, 350)
GUICtrlSetData(-1, "Microsoft XPS Document Writer")
_GUICtrlListBox_SetSel(-1, 0)
$List2 = GUICtrlCreateList("", 300, 0, 300, 400)
$hButton = GUICtrlCreateButton("List Papers", 50, 360)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case  $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            Exit
        Case $hButton
            List1Click()
    EndSwitch
WEnd

;listbox1 event handler
Func List1Click()
    ; get selected text index
    Local $selIndex=_GUICtrlListBox_GetCurSel($List1)
    ;get selected text string from index
   Local $Name=_GUICtrlListBox_GetText($List1, $selIndex)
    ; get the paper sizes for this printer
    _GUICtrlListBox_ResetContent($List2)
   Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
   Local $colItems =  $objWMIService.ExecQuery ("Select * from Win32_PrinterConfiguration Where Name = '" & $Name & "'", "WQL", 0x30)
    _GUICtrlListBox_BeginUpdate($List2)
    For $objItem In $colItems
        _GUICtrlListBox_AddString($List2, $objItem.PaperSize)
    Next
_GUICtrlListBox_EndUpdate($List2)
EndFunc

Use Scriptomatic to see the plenty of functions in WMI respectively in CIMv2 :)

Br,

UEZ

Edited by UEZ

Share this post


Link to post
Share on other sites

Posted (edited)

Hello,

I ran your code as well as this one below, it gives the width, but not the height and the length, and it only returns the width for one paper only for each printer, instead of the actual amount of papers, probably the current paper set for the printer.

$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_PrinterConfiguration", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output &= "DeviceName: " & $objItem.DeviceName & @CRLF
      $Output &= "Name: " & $objItem.Name & @CRLF
      $Output &= "PaperLength: " & $objItem.PaperLength & @CRLF
      $Output &= "PaperSize: " & $objItem.PaperSize & @CRLF
      $Output &= "PaperWidth: " & $objItem.PaperWidth & @CRLF

      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PrinterConfiguration" )
Endif
Edited by oghenez

Share this post


Link to post
Share on other sites

Posted

If CIMv2 cannot give you the information you need then you have to create a 2D array with the needed information from PaperSizesSupported in Win32_Printer.

Array[ubound(UBound($objItem.PrinterPaperNames)][5]

Return value from PaperSizesSupported|Paper|width|heigth|length

You have to fill out the values for width, heigth and length manually but you can access afterwards the value read from PaperSizesSupported and jump to the array directly to read out the values.

Br,

UEZ

Share this post


Link to post
Share on other sites

Posted

i swict to dllcall and used this code. this dllcall is suppose to fill $sPaperNamesList with the paper list for the selected printer, i plan on using this methis to get the with and height of the paper later in code, but i get nothing

Const $DC_BINS = 6
Const $DC_BINNAMES = 12
Const $DC_PAPERNAMES = 16
Const $DC_PAPERS = 2
Const $DC_PAPERSIZE = 3

$lPaperCount = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERNAMES, "str", Chr(0), "long", 0)
$numPaper[$lPaperCount[0]]
; Pad the variable to accept 64 bytes for each paper name.
$sPaperNamesList = _StringRepeat(0, $lPaperCount[0]*64)
;Get paper names
$lPaperCount = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERNAMES, "str", $sPaperNamesList, "long", 0)

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