borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Browse for Folders - Problem with Win95)Next Topic (NT Services) New Topic New Poll Post Reply
AndreaVB Forum : API : Printer not connected to USB port
Poster Message
asihuay
Level: Guest


icon Printer not connected to USB port

Hi.
I want detect if an installed printer is conected , in order to switch by software to another printer if at VBA code run time is not connected.
Thanks in advance.
asihuay

04-12-2003 at 08:26 PM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Printer not connected to USB port

Maybe your solution lies in allowing s user to choose a printer (using Printers collection).

____________________________
If you find the answer helpful, please mark this topic as solved.

06-12-2003 at 01:57 AM
View Profile Send Email to User Show All Posts | Quote Reply
asihuay
Level: Guest

icon Re: Printer not connected to USB port

Hi Goran.
The printers are not in same place of Pc operator and in several case is alone without anybody near in order to verify if is ready by communication device like intercom or phone.
This is the reazon I need to know if is connected before print.
Any idea to do this?
Thanks in advance.
asihuay

06-12-2003 at 02:26 AM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Printer not connected to USB port

This can be done by retrieving info from PRINTER_INFO_2 structure (which is supported on both NT and non NT machines). This is done with GetPrinter API, which returns a pointer to this structure. I am not sure if I have done it right , because I dont have printer on this computer   , but this can be a good ground for your coding. Here is place where you can get more info on this.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_9otu.asp

' In amodule

Option Explicit

Const PRINTER_STATUS_BUSY = &H200
Const PRINTER_STATUS_DOOR_OPEN = &H400000
Const PRINTER_STATUS_ERROR = &H2
Const PRINTER_STATUS_INITIALIZING = &H8000
Const PRINTER_STATUS_IO_ACTIVE = &H100
Const PRINTER_STATUS_MANUAL_FEED = &H20
Const PRINTER_STATUS_NO_TONER = &H40000
Const PRINTER_STATUS_NOT_AVAILABLE = &H1000
Const PRINTER_STATUS_OFFLINE = &H80
Const PRINTER_STATUS_OUT_OF_MEMORY = &H200000
Const PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
Const PRINTER_STATUS_PAGE_PUNT = &H80000
Const PRINTER_STATUS_PAPER_JAM = &H8
Const PRINTER_STATUS_PAPER_OUT = &H10
Const PRINTER_STATUS_PAPER_PROBLEM = &H40
Const PRINTER_STATUS_PAUSED = &H1
Const PRINTER_STATUS_PENDING_DELETION = &H4
Const PRINTER_STATUS_POWER_SAVE = &H1000000
Const PRINTER_STATUS_PRINTING = &H400
Const PRINTER_STATUS_PROCESSING = &H4000
Const PRINTER_STATUS_READY As Long = &H0
Const PRINTER_STATUS_SERVER_UNKNOWN = &H800000
Const PRINTER_STATUS_TONER_LOW = &H20000
Const PRINTER_STATUS_USER_INTERVENTION = &H100000
Const PRINTER_STATUS_WAITING = &H2000
Const PRINTER_STATUS_WARMING_UP = &H10000

Private Type PRINTER_INFO_2
    pServerName As String
    pPrinterName As String
    pShareName As String
    pPortName As String
    pDriverName As String
    pComment As String
    pLocation As String
    pDevMode As Long
    pSepFile As String
    pPrintProcessor As String
    pDatatype As String
    pParameters As String
    pSecurityDescriptor As Long
    Attributes As Long
    Priority As Long
    DefaultPriority As Long
    StartTime As Long
    UntilTime As Long
    Status As Long
    cJobs As Long
    AveragePPM As Long

End Type

Public Type tPrinterInfo
    ServerName As String
    PrinterName As String
    ShareName As String
    PortName As String
    DriverName As String
    Comment As String
    Location As String
    Status As String
    Jobs As Long
    ' for more properties see PRINTER_INFO_2 structure
End Type

Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, ByVal name As String, ByVal Level As Long, pPrinterEnum As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
Private Declare Function IsBadStringPtr Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As String, ByVal ucchMax As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long

Public Function GetPrinterInfo(strPrinterName As String) As tPrinterInfo
Dim prn As Printer
Dim hPrinter As Long
Dim SizeInBytes As Long
Dim buffer() As Long
Dim BytesUsed As Long
Dim PI2 As PRINTER_INFO_2
Dim prnStatus As Long

    'Get printer handle
    OpenPrinter strPrinterName, hPrinter, ByVal 0&
    ' Get buffer size
    GetPrinter hPrinter, 2, ByVal 0&, 0, SizeInBytes
    'Resize buffer
    ReDim buffer(SizeInBytes - 1)
    'Retrieve printer information
    GetPrinter hPrinter, 2, buffer(0), SizeInBytes, BytesUsed
    ' copy data to structure
    CopyMemory PI2, buffer(0), Len(PI2)
    'Show the data
    GetPrinterInfo.ServerName = RetrieveData(buffer(0))
    GetPrinterInfo.PrinterName = RetrieveData(buffer(1))
    GetPrinterInfo.ShareName = RetrieveData(buffer(2))
    GetPrinterInfo.PortName = RetrieveData(buffer(3))
    GetPrinterInfo.DriverName = RetrieveData(buffer(4))
    GetPrinterInfo.Comment = RetrieveData(buffer(5))
    GetPrinterInfo.Location = RetrieveData(buffer(6))
    GetPrinterInfo.Status = GetPrinterStatus(buffer(18))
    GetPrinterInfo.Jobs = Trim(buffer(19))
                
    ' Release printer handle
    ClosePrinter hPrinter
End Function

Private Function RetrieveData(lpString As Long) As String
Dim strData As String

    If lpString > 0 Then
        If Not IsBadStringPtr(lpString, 255) Then
            strData = String(255, Chr(0))
            CopyMemory ByVal strData, ByVal lpString, ByVal Len(strData)
            If Err.LastDllError = 0 Then
                If InStr(strData, Chr$(0)) > 0 Then
                    strData = Left$(strData, InStr(strData, Chr$(0)) - 1)
                End If
            End If
        End If
        
        RetrieveData = strData
    End If
End Function

Private Function GetPrinterStatus(prnStatus As Long) As String
    Select Case prnStatus
        Case PRINTER_STATUS_READY
           GetPrinterStatus = "Ready"
        Case PRINTER_STATUS_PAUSED
           GetPrinterStatus = "Paused"
        Case PRINTER_STATUS_PENDING_DELETION
           GetPrinterStatus = "Deleting"
        Case PRINTER_STATUS_PAPER_JAM
           GetPrinterStatus = "Paper Jam"
        Case PRINTER_STATUS_PAPER_OUT
           GetPrinterStatus = "Paper Out"
        Case PRINTER_STATUS_OFFLINE
           GetPrinterStatus = "Offline"
        Case PRINTER_STATUS_BUSY
           GetPrinterStatus = "Busy"
        Case PRINTER_STATUS_PRINTING
           GetPrinterStatus = "Printing"
        Case PRINTER_STATUS_WAITING
           GetPrinterStatus = "Waiting"
        Case PRINTER_STATUS_NO_TONER
           GetPrinterStatus = "No Toner"
        
        'and many more
        
        Case Else
            GetPrinterStatus = Hex$(prnStatus)
    End Select
End Function



' In a form

Private Sub Command1_Click()
Dim prn As Printer
Dim PrinterInfo() As tPrinterInfo
Dim lCnt As Long
    
    For Each prn In Printers
        ReDim Preserve PrinterInfo(lCnt)
        PrinterInfo(lCnt) = GetPrinterInfo(prn.DeviceName)
        lCnt = lCnt + 1
    Next
    
    Set prn = Nothing
End Sub


____________________________
If you find the answer helpful, please mark this topic as solved.

06-12-2003 at 09:10 PM
View Profile Send Email to User Show All Posts | Quote Reply
asihuay
Level: Guest

icon Re: Printer not connected to USB port

Hi. Goran
Thanks for the code , but I can not know the way to run in VBA can you help me with Function or Sub demostration.
Thanks in advance.
asihuay

07-12-2003 at 04:13 PM
| Quote Reply
AndreaVB Forum : API : Printer not connected to USB port
Previous Topic (Browse for Folders - Problem with Win95)Next Topic (NT Services) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder