Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, _ ByVal name As String, _ ByVal Level As Long, _ pPrinterEnum As Long, _ ByVal cdBuf As Long, _ pcbNeeded As Long, _ pcReturned As Long) As Long
EnumPrinters перечисляет все доступные принтеры, включая локальные и сетевые.
Dim longbuffer() As Long ' массив, заполняемый информацией от функции Dim printinfo() As PRINTER_INFO_1 ' значения для longbuffer() Dim numbytes As Long ' размер в байтах для longbuffer() Dim numneeded As Long ' число необходимых байт, если longbuffer() слишком мал Dim numprinters As Long ' число найденных принтеров Dim c As Integer, retval As Long Me.AutoRedraw = True ' Получим информацию о локальных принтерах numbytes = 3076 ' выделим достаточно большой буфер ReDim longbuffer(0 To numbytes / 4) As Long ' меняем массив -- 1 Long = 4 bytes retval = EnumPrinters(PRINTER_ENUM_LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters) If retval = 0 Then ' пробуем увеличить longbuffer() для получения необходимой информации numbytes = numneeded ReDim longbuffer(0 To numbytes / 4) As Long retval = EnumPrinters(PRINTER_ENUM_LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters) If retval = 0 Then ' опять неудача!' Конвертируем longbuffer() в printinfo() If numprinters <>> 0 Then ReDim printinfo(0 To numprinters - 1) As PRINTER_INFO_1 For c = 0 To numprinters - 1 printinfo(c).flags = longbuffer(4 * c) printinfo(c).pDescription = Space(lstrlen(longbuffer(4 * c + 1))) retval = lstrcpy(printinfo(c).pDescription, longbuffer(4 * c + 1)) printinfo(c).pName = Space(lstrlen(longbuffer(4 * c + 2))) retval = lstrcpy(printinfo(c).pName, longbuffer(4 * c + 2)) printinfo(c).pComment = Space(lstrlen(longbuffer(4 * c + 3))) retval = lstrcpy(printinfo(c).pComment, longbuffer(4 * c + 3)) Next c For c = 0 To numprinters - 1 Me.Print "Имя принтера"; c + 1; "- "; printinfo(c).pName Next c