0

I am having problem with the following code in autoit.

it is suppose to list all printers available in my system and the curresponding papernames supported by each printer.

but am getting only the printer names ans series of '0s' which is suppose to be the papernames

#include <Debug.au3>
#include <String.au3>
Const $DC_BINS = 6
Const $DC_BINNAMES = 12
Const $DC_PAPERNAMES = 16
Const $DC_PAPERS = 2
Const $DC_PAPERSIZE = 3
Dim $BinNameList
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery ("Select Name, PortName from Win32_Printer")
For $objPrinter In $colInstalledPrinters
    $result = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERS, "str", Chr(0), "long", 0)
    $s_struct = ""
_DebugSetup ($s_struct)
    $s_struct=_StringRepeat("0", $result[0]*64)
    ;$s_struct = StringTrimRight($s_struct, 1)
    $struct = DllStructCreate($s_struct)
    $result2 = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERNAMES, "ptr", DllStructGetPtr($struct), "long", 0)
    _DebugOut ( $objPrinter.Name) 
    For $i = 0 To $result[0]-1
        _DebugOut (DllStructGetData($struct, $i)) 
    Next
    $struct = 0
Next
Smith
  • 5,765
  • 17
  • 102
  • 161

1 Answers1

1

Check this out: http://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

Example that uses just WMI:

#include <Array.au3>

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery ("Select * from Win32_Printer",Default,48)
For $objPrinter In $colInstalledPrinters
    $arr = $objPrinter.PrinterPaperNames
    _ArrayDisplay($arr, $objPrinter.Name)
Next

Or try this which prints the actual paper names (run in SciTE so you can see the output from ConsoleWrite):

Const $DC_PAPERS = 2
Const $DC_PAPERSIZE = 3
Const $DC_PAPERNAMES = 16

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery ("Select Name, PortName from Win32_Printer")

For $objPrinter In $colInstalledPrinters
    $result = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERS, "str", Chr(0), "long", 0)

    $s_struct = ""
    $s_struct2 = ""

    For $i = 1 To $result[0]
        $s_struct = $s_struct & "char[64];"
    Next

    For $i = 1 To $result[0]
        $s_struct2 &= "long x;long y;"
    Next

    $s_struct = StringTrimRight($s_struct, 1)
    $s_struct2 = StringTrimRight($s_struct2, 1)
    $j = 1

    $struct = DllStructCreate($s_struct)
    $pointStruct = DllStructCreate($s_struct2)

    $result2 = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERNAMES, "ptr", DllStructGetPtr($struct), "long", 0)
    $result3 = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $objPrinter.Name, "str", $objPrinter.PortName, "int", $DC_PAPERSIZE, "ptr", DllStructGetPtr($pointStruct), "long", 0)

    ConsoleWrite($objPrinter.Name & " on Port: " & $objPrinter.PortName & @CRLF)
    For $i = 1 To $result[0]
        ConsoleWrite(DllStructGetData($struct, $i) & " (" & DllStructGetData($pointStruct, $j) & "mm x " & DllStructGetData($pointStruct, $j + 1) & "mm)" & @CRLF)
        $j += 2
    Next

    $struct = 0
    $pointStruct = 0
Next
Jos van Egmond
  • 2,370
  • 15
  • 19
  • thank you very much for replying, but it dosen't return the actual paper size, but instead it returns somthing like 1,1,1,1,7,24,23,1,1,1,1,1,8 etc. not the actual paper sizes – Smith Apr 07 '11 at 08:11
  • On the msdn page I sent you it describes what the numbers are used for. For example, 8 indicates A3 format. I've actually another answer which can print the actual names and it does basically what you said in the first post. I'll edit my answer and post that instead. – Jos van Egmond Apr 07 '11 at 11:12