Функция OpenPrinter

Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" ( _
        ByVal pPrinterName As String,  _
        phPrinter As Long,  _
        pDefault As Any) As Long

OpenPrinter получает дескриптор заданного принтера.

pPrinterName
Имя открываемого принтера или сервера печати для получения дескриптора. Если используется пустая строка, то функция получает дескриптор локального сервера печати по умолчанию
phPrinter
Переменная, в которую заносится дескриптор открываемого принтера
pDefault
Структура PRINTER_DEFAULTS, задающая некоторые свойства для принтера. При использовании свойств по умолчанию применяйте 0 (Используйте в этом случае выражение ByVal CLng(0))

Пример

' смотри также пример функции PrinterProperties

Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Long, ByVal lpString2 As String) As Long
Private Declare Function lstrcpyToBuffer Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long

' Возвращает True, если установлен PostScript-принтер
Private Function GetDriverInfo2(ByVal printer_name As String) As DRIVER_INFO_2
Dim h_printer As Long
Dim buf_len As Long
Dim buf() As Long
Dim driver_info As DRIVER_INFO_2

    ' Получим дескриптор принтера
    If OpenPrinter(printer_name, h_printer, ByVal 0&) = 0 Then
        MsgBox "Ошибка при открытии принтера " & printer_name & "." & vbCrLf & _
            Err.Description
        GetDriverInfo2 = driver_info
        Exit Function
    Else
        ' See how much space we need for the
        ' driver info structure with its strings
        ReDim buf(1 To 1)
        GetPrinterDriver h_printer, "", 2, _
            buf(1), 0, buf_len

        ' Получим данные
        ReDim buf(1 To buf_len / 4 + 1)
        If GetPrinterDriver(h_printer, "", 2, _
            buf(1), buf_len, buf_len) = 0 _
        Then
            MsgBox "Error getting driver information for " & printer_name & "." & vbCrLf & _
                Err.Description
            GetDriverInfo2 = driver_info
            ClosePrinter h_printer
            Exit Function
        End If

        ' Получим информацию
        driver_info.cVersion = buf(1)
        driver_info.pName = StringFromPointer(buf(2), 1024)
        driver_info.pEnvironment = StringFromPointer(buf(3), 1024)
        driver_info.pDriverPath = StringFromPointer(buf(4), 1024)
        driver_info.pDataFile = StringFromPointer(buf(5), 1024)
        driver_info.pConfigFile = StringFromPointer(buf(6), 1024)

        GetDriverInfo2 = driver_info

        ClosePrinter h_printer
    End If
End Function

Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String
    ' Returns a VB string from an API returned string pointer
    ' Parameters:
    '   lpString - The long pointer to the string
    '   lMaxlength - the size of empty buffer to allow
    
    Dim sRet As String
    Dim lret As Long
    
    If lpString = 0 Then
        StringFromPointer = ""
        Exit Function
    End If
    
    lret = lstrlen(lpString)
    If lret < lMaxLength Then
        lMaxLength = lret
    End If
    
    If IsBadStringPtrByLong(lpString, lMaxLength) Then
        ' An error has occured - do not attempt to use this pointer
       ' Call ReportError(Err.LastDllError, "StringFromPointer", "Attempt to read bad string pointer: " & lpString)
        StringFromPointer = ""
        Exit Function
    End If
    
    ' Pre-initialise the return string...
    sRet = Space$(lMaxLength)
    Call lstrcpyToBuffer(sRet, lpString)
    'CopyMemory ByVal sRet, ByVal lpString, ByVal Len(sRet)
    If Err.LastDllError = 0 Then
        If InStr(sRet, Chr$(0)) > 0 Then
            sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
        End If
    End If

    StringFromPointer = sRet
End Function

Private Sub Command1_Click()
Dim driver_info As DRIVER_INFO_2
Dim i As Long
Dim txt As String

' Получим информацию о принтерах
    For i = 0 To Printers.Count - 1
        ' Get the printer's information.
        driver_info = GetDriverInfo2(Printers(i).DeviceName)

        ' Выводим информацию на экран
        Print driver_info.cVersion
        Print driver_info.pName
        Print driver_info.pEnvironment
        Print driver_info.pDriverPath
        Print driver_info.pDataFile
        Print driver_info.pConfigFile
    Next i
End Sub

Смотри также

ClosePrinter, SetPrinter, StartDocPrinter

Категория

Принтеры