Code:
Option ExplicitPrivate Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" ( _
ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long
Private Const MAX_PRINTERS = 16
Private strPrinterNames(MAX_PRINTERS) As String
Private strPrinterDrivers(MAX_PRINTERS) As String
Private strPrinterPorts(MAX_PRINTERS) As String
Private intPrinterCount As Integer
Public Sub prcGetPrinterList()
Dim strBuffer As String
Dim intIndex As Integer
strBuffer = Space$(8192)
GetProfileString "PrinterPorts", vbNullString, "", _
strBuffer, Len(strBuffer)
prcGetPrinterNames strBuffer
prcGetPrinterPorts
For intIndex = 0 To intPrinterCount
Debug.Print strPrinterNames(intIndex), _
strPrinterPorts(intIndex), _
strPrinterDrivers(intIndex)
Next
End Sub
Private Sub prcGetPrinterNames(ByVal strBuffer As String)
Dim intIndex As Integer
Dim strName As String
intPrinterCount = 0
Do
intIndex = InStr(strBuffer, Chr(0))
If intIndex > 0 Then
strName = Left$(strBuffer, intIndex - 1)
If Len(Trim$(strName)) > 0 Then
strPrinterNames(intPrinterCount) = Trim$(strName)
intPrinterCount = intPrinterCount + 1
End If
strBuffer = Mid$(strBuffer, intIndex + 1)
Else
If Len(Trim$(strBuffer)) > 0 Then
strPrinterNames(intPrinterCount) = Trim$(strBuffer)
intPrinterCount = intPrinterCount + 1
End If
strBuffer = ""
End If
Loop While (intIndex > 0) And (intPrinterCount < MAX_PRINTERS)
End Sub
Private Sub prcGetPrinterPorts()
Dim strBuffer As String
Dim intIndex As Integer
For intIndex = 0 To intPrinterCount - 1
strBuffer = Space$(1024)
GetProfileString "PrinterPorts", strPrinterNames(intIndex), "", _
strBuffer, Len(strBuffer)
prcGetDriverAndPort strBuffer, strPrinterDrivers(intIndex), _
strPrinterPorts(intIndex)
Next
End Sub
Private Sub prcGetDriverAndPort(ByVal Buffer As String, _
DriverName As String, PrinterPort As String)
Dim intDriver As Integer
Dim intPort As Integer
DriverName = ""
PrinterPort = ""
intDriver = InStr(Buffer, ",")
If intDriver > 0 Then
DriverName = Left$(Buffer, intDriver - 1)
intPort = InStr(intDriver + 1, Buffer, ",")
If intPort > 0 Then
PrinterPort = Mid$(Buffer, intDriver + 1, _
intPort - intDriver - 1)
End If
End If
End Sub