Jump to content
Sign in to follow this  

Getting Pagesizes using DLL call convert from VB

Recommended Posts

I am trying to get a list of available papersize names and the papersize numbers for all of the printers installed on the computer.

This is what I have so far:

#include <Array.au3>
Const $DC_PAPERS = 2
Dim $BinNameList, $struct
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colInstalledPrinters = $objWMIService.ExecQuery("Select Name, PortName from Win32_Printer")
For $objPrinter In $colInstalledPrinters
    $Printer_Name = $objPrinter.Name
    $Printer_Port = $objPrinter.PortName
    $result = DllCall("winspool.drv", "long", "DeviceCapabilitiesA", "str", $Printer_Name, "str", $Printer_Port, "int", $DC_PAPERNAMES, "str", DllStructGetPtr($struct), "long", 0)

This AutoIt script will step through each print driver installed and show the number of page sizes available for that printer, printer name, and the first page size. What I am not able to do is loop through the rest of the page sizes for each printer and also get the pagesize numbers for each printer.

This VB code will do what I want but I am having trouble formatting the DLL calls to make it work in AutoIt:

Option Explicit
Private Declare Function DeviceCapabilities Lib _
 "winspool.drv" Alias "DeviceCapabilitiesA" _
 (ByVal lpsDeviceName As String, ByVal lpPort _
 As String, ByVal iIndex As Long, lpOutput As Any, _
 ByVal dev As Long) As Long
Private Type POINTAPI
 x As Long
 y As Long
End Type
Private Const DC_PAPERNAMES = 16
Private Const DC_PAPERS = 2
Private Const DC_PAPERSIZE = 3

Private Sub Command1_Click()
Dim lPaperCount As Long, lCounter As Long
Dim hprinter As Long, sDeviceName As String
Dim sDevicePort As String, sPaperNamesList As String
Dim sNextString As String, numPaper() As Long
Dim paperNumbers() As Integer, paperSizes() As POINTAPI
lPaperCount = DeviceCapabilities(Printer.DeviceName, _
   Printer.Port, DC_PAPERNAMES, ByVal vbNullString, 0)
ReDim numPaper(1 To lPaperCount)
sPaperNamesList = String(64 * lPaperCount, 0)
' Get paper names
lPaperCount = DeviceCapabilities(Printer.DeviceName, _
   Printer.Port, DC_PAPERNAMES, ByVal sPaperNamesList, 0)
' Get matching paper numbers
ReDim paperNumbers(1 To lPaperCount)
lPaperCount = DeviceCapabilities(Printer.DeviceName, _
   Printer.Port, DC_PAPERS, paperNumbers(1), 0)
ReDim paperSizes(1 To lPaperCount)
lPaperCount = DeviceCapabilities(Printer.DeviceName, _
   Printer.Port, DC_PAPERSIZE, paperSizes(1), 0)
For lCounter = 1 To lPaperCount
 sNextString = Mid(sPaperNamesList, _
     64 * (lCounter - 1) + 1, 64)
 sNextString = Left(sNextString, _
     InStr(1, sNextString, Chr(0)) - 1)
 List1.AddItem paperNumbers(lCounter) & vbTab & vbTab _
     & Format(paperSizes(lCounter).x / 254, "0.00") & " x " _
     & Format(paperSizes(lCounter).y / 254, "0.00") _
     & " inch" & vbTab & vbTab & sNextString
Next lCounter
End Sub

Any help would be appreciated.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...