Get computer name by ip address (specify which DNS)

Discussion in 'ASP .Net' started by =?Utf-8?B?Umlja3kgQ2hhbg==?=, Mar 2, 2005.

  1. I am using DNSQuery API to get computer name from IP address.
    However, by below sample code, I don't know how to specify the DNS Address
    for the field (ByVal aipServers As Integer).

    any idea? thank you very much ar!


    -------
    <%@ Import Namespace="System.Runtime.InteropServices" %>
    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.ComponentModel" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System" %><script language="VB" runat="server">

    <DllImport("dnsapi.dll", EntryPoint:="DnsQuery_W", CharSet:=CharSet.Unicode,
    SetLastError:=True)> _
    Private Shared Function DnsQuery(<MarshalAs(UnmanagedType.VBByRefStr)> ByRef
    pszName As String, ByVal wType As Short, ByVal options As Integer, ByVal
    aipServers As Integer, ByRef ppQueryResults As IntPtr, ByVal pReserved As
    Integer) As Integer
    End Function

    <DllImport("dnsapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Sub DnsRecordListFree(ByVal pRecordList As IntPtr, ByVal
    FreeType As Integer)
    End Sub

    Private Structure DNS
    Public pNext As IntPtr
    Public pName As IntPtr
    Public wType As Short
    Public wDataLength As Short
    Public flags As Integer
    Public dwTtl As Integer
    Public dwReserved As Integer
    Public pAddress As IntPtr
    Public pOther0 As IntPtr
    Public pOther1 As IntPtr
    Public pOther2 As IntPtr
    Public pOther3 As IntPtr
    Public pOther4 As IntPtr
    Public pOther5 As IntPtr
    End Structure

    Function DNSLookUp(ByVal Domain As String) As String
    Dim num1 As Integer
    Dim ptr1 As IntPtr = IntPtr.Zero
    Dim ptr2 As IntPtr = IntPtr.Zero
    Dim html As New StringBuilder()
    html.Append("<table border=1><tr><td colspan=3 align=center>" &
    Domain & "</td></tr><tr><td>Host</td><td>Type</td><td>Value</td></tr>")
    Try
    num1 = DnsQuery(Domain, 255, 0, 0, ptr1, 0)
    If num1 = 0 Then
    Dim rec As DNS, myList As New ArrayList()
    ptr2 = ptr1
    While Not ptr2.Equals(IntPtr.Zero)
    rec = CType(Marshal.PtrToStructure(ptr2, GetType(DNS)),
    DNS)
    'response.write(rec.wType & ": " &
    Marshal.PtrToStringAuto(rec.pName) & " (" & rec.pOther0.ToInt32() & "," &
    rec.pOther1.ToInt32() & "," & rec.pOther2.ToInt32() & "," &
    rec.pOther3.ToInt32() & "," & rec.pOther4.ToInt32() & "," &
    rec.pOther5.ToInt32() & "<br>")
    Select Case rec.wType
    Case 6
    myList.Add("0 " &
    Marshal.PtrToStringAuto(rec.pName) & " SOA " &
    Marshal.PtrToStringAuto(rec.pAddress))
    myList.Add("01 &nbsp; " &
    Marshal.PtrToStringAuto(rec.pOther0) & " (Administrator)")
    myList.Add("02 &nbsp; " &
    rec.pOther1.ToInt32() & " (Serial)")
    myList.Add("03 &nbsp; " &
    rec.pOther2.ToInt32() & " (Refresh)")
    myList.Add("04 &nbsp; " &
    rec.pOther3.ToInt32() & " (Retry)")
    myList.Add("05 &nbsp; " &
    rec.pOther4.ToInt32() & " (Expire)")
    myList.Add("06 &nbsp; " &
    rec.pOther5.ToInt32() & " (TTL)")
    Case 15
    myList.Add("2 " &
    Marshal.PtrToStringAuto(rec.pName) & " MX (" & rec.pOther0.ToInt32() & ") " &
    Marshal.PtrToStringAuto(rec.pAddress))
    Case 1
    myList.Add("3 " &
    Marshal.PtrToStringAuto(rec.pName) & " A " & MakeIP(rec.pAddress.ToInt64()))
    Case 2
    myList.Add("1 " &
    Marshal.PtrToStringAuto(rec.pName) & " NS " &
    Marshal.PtrToStringAuto(rec.pAddress))
    End Select
    ptr2 = rec.pNext
    End While
    myList.Sort()
    For num1 = 0 To myList.Count - 1
    html.Append("<tr><td>" & Replace(Mid(myList.Item(num1),
    4), vbTab, "</td><td>") & "</td></tr>")
    Next
    End If
    Finally
    DnsRecordListFree(ptr2, 0)
    End Try
    html.Append("</table>")
    Return html.ToString()
    End Function

    Function MakeIP(ByVal dwIP As Long) As String
    ' old fashion model
    Dim ip(3) As Integer
    ip(0) = Int(dwIP / 256 ^ 3) : dwIP = dwIP - (ip(0) * 256 ^ 3)
    ip(1) = Int(dwIP / 256 ^ 2) : dwIP = dwIP - (ip(1) * 256 ^ 2)
    ip(2) = Int(dwIP / 256) : dwIP = dwIP - (ip(2) * 256)
    ip(3) = dwIP : If ip(0) < 0 Then ip(0) = 256 + ip(0)
    Return ip(3) & "." & ip(2) & "." & ip(1) & "." & ip(0)
    End Function

    Sub DNSAll(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Handles MyBase.Load
    Response.Write(DNSLookUp("hp.com"))
    End Sub

    </script>
     
    =?Utf-8?B?Umlja3kgQ2hhbg==?=, Mar 2, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kazu
    Replies:
    6
    Views:
    6,011
    Tauno Voipio
    Jun 4, 2005
  2. Jason

    Using DNS name verses Machine Name causes 403 error

    Jason, Sep 13, 2006, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    304
    Steven Cheng[MSFT]
    Sep 18, 2006
  3. Sam Roberts
    Replies:
    0
    Views:
    227
    Sam Roberts
    Mar 20, 2005
  4. Jan Lelis
    Replies:
    22
    Views:
    299
    Robert Klemme
    Jul 13, 2010
  5. gavino
    Replies:
    2
    Views:
    180
    Ted Zlatanov
    Jul 27, 2006
Loading...

Share This Page