Foxtable(狐表)用户栏目专家坐堂 → [求助]获取已安装的打印机名称


  共有3067人关注过本帖树形打印复制链接

主题:[求助]获取已安装的打印机名称

帅哥哟,离线,有人找我吗?
农村人
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:174 积分:2349 威望:0 精华:0 注册:2015/8/12 15:36:00
[求助]获取已安装的打印机名称  发帖心情 Post By:2015/10/20 21:40:00 [只看该作者]

For Each p As String in InstalledPrinters
Output
.Show(p
)
Next

以上代码,可以列出已经安装的打印机名称

但以下代码却不能.问题出在哪呢
Dim cmd As WinForm.ComboBox = e.Form.Controls("ComboBox1")
For Each p As String In InstalledPrinters
    cmd.Combolist = p
Next

 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/10/20 21:40:00 [只看该作者]

Dim cmd As WinForm.ComboBox = e.Form.Controls("ComboBox1")
For Each p As String In InstalledPrinters
    cmd.Items.Add(p)
Next

 回到顶部
帅哥哟,离线,有人找我吗?
15986720145
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:146 积分:1243 威望:0 精华:0 注册:2015/8/7 16:22:00
  发帖心情 Post By:2015/10/22 20:52:00 [只看该作者]

追问::已经获得局域网内在线的电脑名字列表,并已经赋值给 ComboList ,
其中某台电脑装有打印机,并且已经共享,请问 如何获得局域网内的打印机名字。
[此贴子已经被作者于2015/10/22 21:06:34编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/10/22 21:06:00 [只看该作者]

以下是引用15986720145在2015/10/22 20:52:00的发言:
追问::
如何获得局域网内的打印机。

 

你获取没有用,获取了你没有添加到你的电脑上不能直接打印。

 

 


 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/10/22 21:07:00 [只看该作者]

全局代码

 

<FlagsAttribute> _
Public Enum PrinterEnumFlags
PRINTER_ENUM_DEFAULT = &H1
PRINTER_ENUM_LOCAL = &H2
PRINTER_ENUM_CONNECTIONS = &H4
PRINTER_ENUM_FAVORITE = &H4
PRINTER_ENUM_NAME = &H8
PRINTER_ENUM_REMOTE = &H10
PRINTER_ENUM_SHARED = &H20
PRINTER_ENUM_NETWORK = &H40
PRINTER_ENUM_EXPAND = &H4000
PRINTER_ENUM_CONTAINER = &H8000
PRINTER_ENUM_ICONMASK = &Hff0000
PRINTER_ENUM_ICON1 = &H10000
PRINTER_ENUM_ICON2 = &H20000
PRINTER_ENUM_ICON3 = &H40000
PRINTER_ENUM_ICON4 = &H80000
PRINTER_ENUM_ICON5 = &H100000
PRINTER_ENUM_ICON6 = &H200000
PRINTER_ENUM_ICON7 = &H400000
PRINTER_ENUM_ICON8 = &H800000
PRINTER_ENUM_HIDE = &H1000000
End Enum

<StructLayout(LayoutKind.Sequential, CharSet := CharSet.Auto)> _
Public Structure PRINTER_INFO_1
Private flags As Integer
<MarshalAs(UnmanagedType.LPTStr)> _
Public pDescription As String
<MarshalAs(UnmanagedType.LPTStr)> _
Public pName As String
<MarshalAs(UnmanagedType.LPTStr)> _
Public pComment As String
End Structure

<DllImport("winspool.drv", CharSet := CharSet.Auto, SetLastError := True)> _
Public Function EnumPrinters(Flags As PrinterEnumFlags, Name As String, Level As UInteger, pPrinterEnum As IntPtr, cbBuf As UInteger, ByRef pcbNeeded As UInteger, _
ByRef pcReturned As UInteger) As Boolean
End Function

Private Const ERROR_INSUFFICIENT_BUFFER As Integer = 122

Public Function MyEnumPrinters(Flags As PrinterEnumFlags) As PRINTER_INFO_1()
Dim cbNeeded As UInteger = 0
Dim cReturned As UInteger = 0
Dim pPrInfo4 As IntPtr = IntPtr.Zero
Dim size As UInteger = 0

If EnumPrinters(Flags, Nothing, 1, IntPtr.Zero, size, cbNeeded, _
    cReturned) Then
    Return New PRINTER_INFO_1() {}
End If
If cbNeeded <> 0 Then
    pPrInfo4 = Marshal.AllocHGlobal(CInt(cbNeeded) + 128)
    size = cbNeeded + 128
    EnumPrinters(Flags, Nothing, 1, pPrInfo4, size, cbNeeded, _
    cReturned)
    If cReturned <> 0 Then
        Dim printerInfo1 As PRINTER_INFO_1() = New PRINTER_INFO_1(cReturned - 1) {}
        Dim offset As Integer = pPrInfo4.ToInt32()
        Dim Type As Type = Gettype(PRINTER_INFO_1)
        Dim increment As Integer = Marshal.SizeOf(Type)
        For i As Integer = 0 To cReturned - 1
            printerInfo1(i) = CType(Marshal.PtrToStructure(New IntPtr(offset), Type), PRINTER_INFO_1)
            offset += increment
        Next
        Marshal.FreeHGlobal(pPrInfo4)
        Return printerInfo1
    End If
End If

Return New PRINTER_INFO_1() {}
End Function


 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/10/22 21:07:00 [只看该作者]

调用代码

 

Dim printers As PRINTER_INFO_1() = MyEnumPrinters(PrinterEnumFlags.PRINTER_ENUM_REMOTE)
For Each printer As PRINTER_INFO_1 In printers
    If -1 = printer.pName.IndexOf("!!") Then
        msgbox(printer.pName)
    Else
        Dim cbNeeded As UInteger = 0
        Dim cReturned As UInteger = 0
        Dim pPrInfo4 As IntPtr = IntPtr.Zero
        Dim size As UInteger = 0
        Dim pNewName As String = printer.pName
        EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_NAME, pNewName, 1, IntPtr.Zero, size, cbNeeded, _
        cReturned)
        If cbNeeded <> 0 Then
            pPrInfo4 = Marshal.AllocHGlobal(CInt(cbNeeded) + 128)
            size = cbNeeded + 128
            EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_NAME, pNewName, 1, pPrInfo4, size, cbNeeded, _
            cReturned)
            Dim printerInfo1 As PRINTER_INFO_1() = New PRINTER_INFO_1(cReturned - 1) {}
            Dim offset As Integer = pPrInfo4.ToInt32()
            Dim Type As Type = Gettype(PRINTER_INFO_1)
            Dim increment As Integer = Marshal.SizeOf(Type)
            For i As Integer = 0 To cReturned - 1
                printerInfo1(i) = DirectCast(Marshal.PtrToStructure(New IntPtr(offset), Type), PRINTER_INFO_1)
                offset += increment
                msgbox(printerInfo1(i).pName)
            Next
            Marshal.FreeHGlobal(pPrInfo4)
        End If
    End If
Next


 回到顶部
帅哥哟,离线,有人找我吗?
15986720145
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:146 积分:1243 威望:0 精华:0 注册:2015/8/7 16:22:00
  发帖心情 Post By:2015/10/22 21:33:00 [只看该作者]

谢谢大神,虽然看不太懂!!!

 回到顶部