Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

WMI QUERY help


  • Please log in to reply
19 replies to this topic

#1 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 02 April 2011 - 02:43 PM

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?







#2 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 02 April 2011 - 05:06 PM

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

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


#3 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 02 April 2011 - 05:34 PM

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


#4 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 02 April 2011 - 06:33 PM

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

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


#5 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 02 April 2011 - 06:42 PM

but am not getting any result from your code,

#6 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 02 April 2011 - 07:27 PM

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, 02 April 2011 - 07:28 PM.

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


#7 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 02 April 2011 - 07:35 PM

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

#8 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 02 April 2011 - 07:47 PM

i still don't get anything, please help

#9 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 02 April 2011 - 08:45 PM

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, 02 April 2011 - 09:07 PM.

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


#10 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 03 April 2011 - 10:41 AM

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


#11 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 03 April 2011 - 11:45 AM

Maybe this will help you:

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

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


#12 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 03 April 2011 - 12:32 PM

thank you very much, am grateful

#13 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 04 April 2011 - 04:21 PM

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

http://www.autoitscript.com/forum/topic/127254-help-printer-papernames-and-papersizes/

#14 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 04 April 2011 - 06:02 PM

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

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


#15 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 04 April 2011 - 06:27 PM

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.

#16 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 9,549 posts

Posted 04 April 2011 - 06:35 PM

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 an XP machine, and I'm not going to.

 

How to ask questions the smart way!

 

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.
GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips. Latin Square password generator

 

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.


#17 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 04 April 2011 - 06:36 PM

You can use Win32_PrinterConfiguration to get the paper size:

AutoIt         
#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, 04 April 2011 - 06:57 PM.

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


#18 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 04 April 2011 - 06:54 PM

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.

AutoIt         
$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, 04 April 2011 - 06:55 PM.


#19 UEZ

UEZ

    Never say never

  • MVPs
  • 4,496 posts

Posted 04 April 2011 - 07:13 PM

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

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


#20 TheCurrent

TheCurrent

    Wayfarer

  • Active Members
  • Pip
  • 70 posts

Posted 04 April 2011 - 07:50 PM

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)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users