Get computer name by ip address (specify which DNS)

G

Guest

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top