1

有谁知道包含 VB6 中打印机对象读取的所有打印机信息的文件在哪里?

我们通常编写如下代码:

For x in **Printers** 
...  ... ...
Next

通常,我会将列表放入组合框中,以便用户选择打印机。

当使用上面的编码在服务器上运行应用程序以通过远程桌面会话进行打印时,我可以看到其他打印机也出现在列表中。我只想在会话中看到我自己的打印机而不是其他打印机。

在我看来,如果我能找到文件的位置,我想我可以做点什么。

任何额外的建议表示赞赏!:)

谢谢!

4

2 回答 2

2

我正在使用以下代码(它现在有点旧,所以它可能会做得更好)

声明:

Public Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal Flags As Long, ByVal Name As String, ByVal Level As Long, ByRef pPrinterEnum As Byte, ByVal cdBuf As Long, ByRef pcbNeeded As Long, ByRef pcReturned As Long) As Long
Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Public Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

'--------------------------------------------------------------
'EnumPrinters Flags
Public Const PRINTER_ENUM_CONNECTIONS As Long = &H4
Public Const PRINTER_ENUM_CONTAINER   As Long = &H8000
Public Const PRINTER_ENUM_DEFAULT     As Long = &H1
Public Const PRINTER_ENUM_EXPAND      As Long = &H4000
Public Const PRINTER_ENUM_FAVORITE    As Long = &H4
Public Const PRINTER_ENUM_HIDE        As Long = &H1000000
Public Const PRINTER_ENUM_ICON1       As Long = &H10000
Public Const PRINTER_ENUM_ICON2       As Long = &H20000
Public Const PRINTER_ENUM_ICON3       As Long = &H40000
Public Const PRINTER_ENUM_ICON4       As Long = &H80000
Public Const PRINTER_ENUM_ICON5       As Long = &H100000
Public Const PRINTER_ENUM_ICON7       As Long = &H400000
Public Const PRINTER_ENUM_ICON6       As Long = &H200000
Public Const PRINTER_ENUM_ICON8       As Long = &H800000
Public Const PRINTER_ENUM_ICONMASK    As Long = &HFF0000
Public Const PRINTER_ENUM_LOCAL       As Long = &H2
Public Const PRINTER_ENUM_NAME        As Long = &H8
Public Const PRINTER_ENUM_NETWORK     As Long = &H40
Public Const PRINTER_ENUM_REMOTE      As Long = &H10
Public Const PRINTER_ENUM_SHARED      As Long = &H20

Public Type PRINTER_INFO_1 'REAL API STRUCTURE FOR USE WITH EnumPrinters
  Flags         As Long
  pDescription  As Long
  pName         As Long
  pComment      As Long
End Type

Public Type PRINTER_INFO_1_VB
  Flags         As Long
  pDescription  As String
  pName         As String
  pComment      As String
End Type

Private Enum NEPrinterEnumLevel1
  pel1Local = PRINTER_ENUM_LOCAL
  pel1Name = PRINTER_ENUM_NAME
  pel1Default = PRINTER_ENUM_DEFAULT
  pel1Connections = PRINTER_ENUM_CONNECTIONS
  pel1Network = PRINTER_ENUM_NETWORK
  pel1Remote = PRINTER_ENUM_REMOTE
End Enum

Sub 填充组合框:

'##############################################################################
'##
'##     Sub pFillcboPrinters
'##
'##############################################################################
Private Sub pFillcboPrinters()
  Dim atPIs() As PRINTER_INFO_1_VB
  Dim lCount  As Long
  Dim lIndex  As Long
  '--------------------------------------------------------------
  'Clear List
  Call cboPrinters.Clear
  '--------------------------------------------------------------
  'Get printers using the WinAPI
  lCount = fnEnumPrinters_PI1(atPIs(), pel1Local + pel1Connections)
  For lIndex = 1 To lCount
    Call cboPrinters.AddItem(atPIs(lIndex).pName)
  Next lIndex
  '--------------------------------------------------------------
  'No printers available?
  If cboPrinters.ListCount = 0 Then Call cboPrinters.AddItem("[No printers available]")
End Sub

实际枚举打印机的例程:

'##############################################################################
'##
'##     Function fnEnumPrinters_PI1
'##
'##############################################################################
'!! WARNING: sName = vbNullString IS IMPORTANT, BECAUSE sName = "" GIVES ANOTHER RESULT
Private Function fnEnumPrinters_PI1(ByRef atPIs() As PRINTER_INFO_1_VB, _
                                    ByVal eEnum As NEPrinterEnumLevel1, _
                                    Optional ByVal bShared As Boolean = False, _
                                    Optional ByVal sName As String = vbNullString) As Long
  Dim abBuffer()  As Byte
  Dim lBufferSize As Long
  Dim lFlags      As Long
  Dim lIndex      As Long
  Dim lLenPI1     As Long
  Dim lNeeded     As Long
  Dim lResult     As Long
  Dim lReturn     As Long
  Dim tPI1        As PRINTER_INFO_1
  '--------------------------------------------------------------
  'Default no printers available
  lResult = 0
  ReDim atPIs(0)
  '--------------------------------------------------------------
  'Set the flags for EnumPrinters
  lFlags = eEnum
  If bShared Then lFlags = lFlags Or PRINTER_ENUM_SHARED
  '--------------------------------------------------------------
  'Start with a big buffer
  lBufferSize = 32767
  lNeeded = 0
  ReDim abBuffer(lBufferSize)
  lReturn = EnumPrinters(lFlags, sName, 1, abBuffer(0), lBufferSize, lNeeded, lResult)
  '--------------------------------------------------------------
  'Need a bigger buffer? try again with bigger buffer
  If (lReturn = 0) And (lNeeded > 0) Then
    ReDim abBuffer(lBufferSize)
    lReturn = EnumPrinters(lFlags, sName, 1, abBuffer(0), lBufferSize, lNeeded, lResult)
  End If 'If (lReturn=0) And (lNeeded>0)
  '--------------------------------------------------------------
  'Everything OK? and printers to retrieve?
  If (lReturn <> 0) And (lResult > 0) Then
    '--------------------------------------------------------------
    'Create enough entries for the Printerinfo's
    ReDim atPIs(lResult)
    lLenPI1 = Len(tPI1)
    '--------------------------------------------------------------
    'Buffer constist of first all PRINTER_INFO_1 and after that extra info,
    'but the PRINTER_INFO_1 contains the important information which get's
    'the other information, but we only use the PRINTER_INFO_1 info of the buffer
    For lIndex = 1 To lResult
      '--------------------------------------------------------------
      'Copy the info to the real API PRINTER_INFO_1
      Call MoveMemory(tPI1, abBuffer((lIndex - 1) * lLenPI1), lLenPI1)
      '--------------------------------------------------------------
      'Copy the information to the VB version
      With atPIs(lIndex)
        '--------------------------------------------------------------
        'Copy the flags
        .Flags = tPI1.Flags
        '--------------------------------------------------------------
        'Copy the Description
        .pDescription = Space$(lstrlen(tPI1.pDescription))
        Call lstrcpy(.pDescription, tPI1.pDescription)
        '--------------------------------------------------------------
        'Copy the name
        .pName = Space$(lstrlen(tPI1.pName))
        Call lstrcpy(.pName, tPI1.pName)
        '--------------------------------------------------------------
        'Copy the Comment
        .pComment = Space$(lstrlen(tPI1.pComment))
        Call lstrcpy(.pDescription, tPI1.pComment)
      End With 'With atPIs(lIndex)
    Next lIndex
  End If 'If (lReturn<>0) And (lResult>0)
  '--------------------------------------------------------------
  'Return the ammount of printers
  fnEnumPrinters_PI1 = lResult
End Function
于 2015-05-28T09:50:32.140 回答
1

我正在使用在组合框中枚举资源管理器打印机文件夹并显示它们的图标。也可以在 RDS 下工作。

于 2012-06-04T10:30:07.657 回答