Jump to content

convert one fucntion to autoit


Recommended Posts

please help me convert this code to autoit, please help in which ever way you can. in part or in full

Option Explicit

'Private Declare Function DeviceCapabilities Lib "winspool.drv" Alias _
    "DeviceCapabilitiesA" (ByVal lpDeviceName As String, ByVal lpPort As String, _
    ByVal iIndex As Long, lpOutput As Any, lpDevMode As Any) As Long

Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2

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



Function EnumeratePrinters() As String()

    Dim ListOfPrinters()      As String
    Dim Success               As Boolean, cbRequired As Long, cbBuffer As Long
    Dim Buffer()              As Long, nEntries As Long
    Dim i                     As Long, PFlags As Long, PDesc As String, PName As String
    Dim pComment              As String, Temp As Long
    
    cbBuffer = 3072
    ReDim Buffer((cbBuffer \ 4) - 1) As Long
    Success = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or PRINTER_ENUM_LOCAL, _
        vbNullString, 1, Buffer(0), cbBuffer, cbRequired, nEntries)
         
    If Success Then
        If cbRequired > cbBuffer Then
            cbBuffer = cbRequired
            Debug.Print "Buffer too small.  Trying again with " & cbBuffer & " bytes."
            ReDim Buffer(cbBuffer \ 4) As Long
            Success = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or PRINTER_ENUM_LOCAL, _
                vbNullString, 1, Buffer(0), cbBuffer, cbRequired, nEntries)
            If Not Success Then
                Debug.Print "Error enumerating printers."
                Exit Function
            End If
        End If
        Debug.Print "There are " & nEntries & " local and connected printers."
        
        ReDim ListOfPrinters(nEntries - 1)
        
        For i = 0 To nEntries - 1
            PFlags = Buffer(4 * i)
            PDesc = Space$(StrLen(Buffer(i * 4 + 1)))
            Temp = PtrToStr(PDesc, Buffer(i * 4 + 1))
            PName = Space$(StrLen(Buffer(i * 4 + 2)))
            Temp = PtrToStr(PName, Buffer(i * 4 + 2))
            pComment = Space$(StrLen(Buffer(i * 4 + 2)))
            Temp = PtrToStr(pComment, Buffer(i * 4 + 2))
            Debug.Print PFlags, PDesc, PName, pComment
            ListOfPrinters(i) = PName
        Next i
    Else
        Debug.Print "Error enumerating printers."
    End If
    EnumeratePrinters = ListOfPrinters     
End Function


Private Sub List1_Click()
    Dim Prt      As Printer
    List2.Clear
    List1.Enabled = False
    For Each Prt In Printers
        If InStr(Prt.DeviceName, List1.List(List1.ListIndex)) Then
            Set Printer = Prt
            Exit For
        End If
    Next
    ListPaperSizes
    List1.Enabled = True
    Label2.Caption = "Supported Paper sizes ( " & List2.ListCount & " )"
End Sub


Private Sub ListPaperSizes()
    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
    List2.Clear
    lPaperCount = DeviceCapabilities(Printer.DeviceName, _
        Printer.Port, DC_PAPERNAMES, ByVal vbNullString, 0)
    ReDim numPaper(1 To lPaperCount)
    sPaperNamesList = String(64 * lPaperCount, 0)
    lPaperCount = DeviceCapabilities(Printer.DeviceName, Printer.Port, DC_PAPERNAMES, ByVal sPaperNamesList, 0)
    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)
        List2.AddItem paperNumbers(lCounter) & vbTab _
            & Format(paperSizes(lCounter).x / 254, "0.00") & " x " _
            & Format(paperSizes(lCounter).y / 254, "0.00") _
            & " inch" & vbTab & sNextString
    Next lCounter
End Sub
Link to comment
Share on other sites

What is it you are trying to do? Instead of thinking about converting what you have provided, it might just be easier to figure out what you need to do in AutoIt then research the AutoIt code for it.

We did this the other day in another thread, to get a list of printers:

$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$collPrinters = $objWMIService.ExecQuery("Select * FROM Win32_Printer")
For $printer In $collPrinters
    $name = $printer.name
    ConsoleWrite("Printer: " & $name & @CRLF)
Next
Link to comment
Share on other sites

thanks very much for this.

I have used koda to design the form, and partially written a few code. for each printer, i need to get the list of all paper size supported. so for example i'll use your code to list all printers in a listbox, then on clicking any printer, another function will list all the paper size supported in another listbox

you used WMI,which is slow compared to API DLL calls in my code, and what if the system does not have it installed by default e.g winxp

Edited by oghenez
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...