DNS NsLookup using Visual Basic
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
The Domain Name System (DNS) is the method by which Internet addresses in mnemonic form - such as www.activexperts.com - are converted into the equivalent numeric IP address such as 212.97.55.136. To the user and application process this translation is a service provided either by the local host or from a remote host via the Internet. The DNS server (or resolver) may communicate with other Internet DNS servers if it cannot translate the address itself. DNS names are constructed hierarchically. The highest level of the hierarchy is the last component or label of the DNS address. Labels can be up to 63 characters long and are not case sensitive. A maximum length of 255 characters is allowed. Labels must start with a letter and can only consist of letters, digits and hyphens.
Nslookup is a popular program for UNIX, Linux and Windows to query Internet domain name servers. It allows the user to query name servers for information about various hosts and domains or to print a list of hosts in a domain.
Step 1: Download and install the Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new Visual Basic project
Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':
Step 3: Refer to the Network Component Library and create the objects
A new Project is created, with a blank form.
First, you must add a reference to Network Component in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'Network Component 3.1 Type Library' reference as shown in the following picture:
Click 'OK' to close the 'References...' dialog.
Then, select the Project form and choose 'View Code' from the context menu:
Step 4: Create the object
From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now. In the 'Form Load' function, create the object in the following way:
Set objDnsServer = CreateObject("AxNetwork.DnsServer")
Appendix: Full source code
When the required DNS object is created, you can implement the code to query a hostname on a DNS server:
Option Explicit Public objDnsServer As AxNetwork.DnsServer Public objConstants As AxNetwork.NwConstants Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Const MAX_PATH = 260 '/////////////////////////////////////////////////////////////////////// Private Sub Form_Load() Set objDnsServer = CreateObject("AxNetwork.DnsServer") Set objConstants = CreateObject("AxNetwork.NwConstants") SetDefaultLogFile cmbType.AddItem ("A Record") cmbType.AddItem ("NS Record") cmbType.AddItem ("CNAME Record") cmbType.AddItem ("SOA Record") cmbType.AddItem ("PTR Record") cmbType.AddItem ("MX Record") cmbType.AddItem ("AAAA Record") cmbType.AddItem ("ANY Record") cmbType.ListIndex = 7 End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnLookup_Click() Dim lType As Long Dim objDnsRecord As DnsRecord Select Case (cmbType.ListIndex) Case 0 lType = objConstants.nwDNS_TYPE_A Case 1 lType = objConstants.nwDNS_TYPE_NS Case 2 lType = objConstants.nwDNS_TYPE_CNAME Case 3 lType = objConstants.nwDNS_TYPE_SOA Case 4 lType = objConstants.nwDNS_TYPE_PTR Case 5 lType = objConstants.nwDNS_TYPE_MX Case 6 lType = objConstants.nwDNS_TYPE_AAAA Case 7 lType = objConstants.nwDNS_TYPE_ANY End Select objDnsServer.Server = txtServer.Text objDnsServer.LogFile = txtLogfile.Text objDnsServer.Lookup txtHost.Text, lType lsResult.Clear If (GetResult() = 0) Then Set objDnsRecord = objDnsServer.GetFirstRecord On Error Resume Next While (objDnsServer.LastError = 0) Select Case objDnsRecord.Type Case objConstants.nwDNS_TYPE_A lsResult.AddItem ("Type: A") lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "IPv4 Address: " & objDnsRecord.Address Case objConstants.nwDNS_TYPE_AAAA lsResult.AddItem "Type: AAAA" lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "IPv6 Address: " & objDnsRecord.Address Case objConstants.nwDNS_TYPE_CNAME lsResult.AddItem "Type: CNAME" lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "Alias: " & objDnsRecord.Address Case objConstants.nwDNS_TYPE_MX lsResult.AddItem "Type: MX" lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "Preference: " & objDnsRecord.Preference lsResult.AddItem vbTab & "Mail Exchange: " & objDnsRecord.MailExchange Case objConstants.nwDNS_TYPE_NS lsResult.AddItem "Type: NS" lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "Name Server: " & objDnsRecord.NameServer Case objConstants.nwDNS_TYPE_PTR lsResult.AddItem "Type: PTR" lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "Host: " & objDnsRecord.Address Case objConstants.nwDNS_TYPE_SOA lsResult.AddItem "Type: SOA" lsResult.AddItem vbTab & "Name: " & objDnsRecord.Name lsResult.AddItem vbTab & "Name Server: " & objDnsRecord.NameServer lsResult.AddItem vbTab & "MailBox: " & objDnsRecord.MailBox lsResult.AddItem vbTab & "Serial: " & objDnsRecord.SerialNumber lsResult.AddItem vbTab & "Refresh: " & objDnsRecord.RefreshInterval lsResult.AddItem vbTab & "Retry Interval: " & objDnsRecord.RetryInterval lsResult.AddItem vbTab & "Expiration Limit: " & objDnsRecord.ExpirationLimit lsResult.AddItem vbTab & "Minimum TTL: " & objDnsRecord.MinimumTTL End Select Set objDnsRecord = objDnsServer.GetNextRecord On Error Resume Next Wend End If End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnView_Click() If FileExists(txtLogfile.Text) = True Then Shell "notepad " + txtLogfile.Text, vbNormalFocus End If End Sub '/////////////////////////////////////////////////////////////////////// Public Function FileExists(sFileName As String) As Boolean FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName)) End Function '/////////////////////////////////////////////////////////////////////// Private Function GetResult() As Long Dim lResult As Long lResult = objDnsServer.LastError txtResult.Text = lResult & ": " & objDnsServer.GetErrorDescription(lResult) GetResult = lResult End Function '/////////////////////////////////////////////////////////////////////// Private Function SetDefaultLogFile() Dim Buffer As String Buffer = Space(MAX_PATH) If GetTempPath(MAX_PATH, Buffer) <> 0 Then txtLogfile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "Dns.log" Else txtLogfile.Text = "C:\Dns.log" End If End Function '///////////////////////////////////////////////////////////////////////
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.