我们这里的每台打印机都直接对应一个IP地址而不是连接到电脑上,那么我在编程时如何获得打印机的IP地址,请各位赐教,急急急急急急急急急急急急急急急急急急急急急!

解决方案 »

  1.   

    Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
    Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
    Private Type HOSTENT
       hName As Long
       hAliases As Long
       hAddrType As Integer
       hLength As Integer
       hAddrList As Long
    End Type
    Private Function GetIP(ByVal phostname As String) As String
       Dim hostent_addr As Long
       Dim host As HOSTENT
       Dim hostip_addr As Long
       Dim temp_ip_address() As Byte
       Dim i As Integer
       Dim ip_address As String
       
       hostent_addr = gethostbyname("lily")
       
       If hostent_addr = 0 Then
          MsgBox "不可识别"
          Exit Function
       End If
       
       RtlMoveMemory host, hostent_addr, LenB(host)
       RtlMoveMemory hostip_addr, host.hAddrList, 4   ReDim temp_ip_address(1 To host.hLength)
       RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
       
       For i = 1 To host.hLength
          ip_address = ip_address & temp_ip_address(i) & "."
       Next
       ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
       GetIP = ip_address
    End FunctionPrivate Sub Command1_Click()
        If Left(Combo1.Text, 2) <> "\\" Then
            MsgBox "打印是本机的!"
        Else
            MsgBox "IP为" & GetIP(Mid(Combo1.Text, 3, InStr(3, Combo1.Text, "\") - 3))
        End If
    End SubPrivate Sub Form_Load()
        Dim PR As Printer
        For Each PR In Printers
           Combo1.AddItem PR.DeviceName
        Next PR
        Combo1.Text = Printer.DeviceName
    End Sub'本机IP这里就不显示了,很容易得到了
      

  2.   

    问题已经解决了,多谢wwqna(york) 的热心,虽然没能给出我问题直接答案,不过还是给了我一定启发,奖励20分表示感谢