Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Get computer name by ip address (specify which DNS)

Reply
Thread Tools

Get computer name by ip address (specify which DNS)

 
 
=?Utf-8?B?Umlja3kgQ2hhbg==?=
Guest
Posts: n/a
 
      03-02-2005
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Computer forgets network name but remembers IP address SteveC Wireless Networking 7 02-19-2006 04:30 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Changing a Domain Controllers Computer Name & IP Address Baljit Singh MCSE 4 03-16-2005 05:57 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments