Dllcall autoit部分获得结果

时间:2011-04-05 08:59:17

标签: autoit printers

我在autoit中遇到以下代码问题。

假设列出我系统中可用的所有打印机以及每台打印机支持的相应纸张名称。

但我只得到打印机名称和'0s'系列,这被认为是纸名

#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

1 个答案:

答案 0 :(得分:1)

检查出来:http://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

仅使用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

或尝试使用它来打印实际的纸张名称(在SciTE中运行以便您可以看到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