Sign in to follow this  
Followers 0

WMI QUERY help

20 posts in this topic

#1 ·  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



#2 ·  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


Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#3 ·  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

#4 ·  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


Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#5 ·  Posted

but am not getting any result from your code,

Share this post


Link to post
Share on other sites

#6 ·  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

Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#7 ·  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

#8 ·  Posted

i still don't get anything, please help

Share this post


Link to post
Share on other sites

#9 ·  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

Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#10 ·  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

#11 ·  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


Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#12 ·  Posted

thank you very much, am grateful

Share this post


Link to post
Share on other sites

#13 ·  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

#14 ·  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


Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#15 ·  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

#16 ·  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.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#17 ·  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

Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#18 ·  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

#19 ·  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


Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#20 ·  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