VB6 Lookup Hostname From IP, Specifying DNS Server

I know how to look up a hostname from IPv4 to VB using the Windows API call GetHostByAddr ( this works fine). However, this feature does not allow you to specify the DNS server to use. Sometimes DNS servers are fine by default, but in other cases I need to specify an external DNS server for searching, and I donโ€™t think that making an nslookup shell and analyzing the output is the best method here.

Note: in fact, it will be used as VBA code in an Excel workbook to help someone else do their job, and itโ€™s not worth writing a large application when it needs only simple functionality.

I thought I might have found the answer in the getnameinfo API call , but it seems that a thorough reading indicates that it does not offer the servername parameter.

After some intensive searching, I found a link to the pExtra parameter to the DNSQuery function . But I donโ€™t even know how to start using this in VB6.

Can someone help me somehow do a DNS lookup from VB6 by specifying the server name to use?

A complete working solution, of course, would be nice, but I'm ready to work: just point me in the right direction.

UPDATE: For some odd reason, he did not click that DNSQuery was a call to the Windows API. It just doesn't seem like one. Of course, I could have made more progress on this issue if I had gathered this small detail.

+4
source share
3 answers

Try the following:

Option Explicit Private Declare Function DnsQuery Lib "dnsapi" Alias "DnsQuery_A" (ByVal strname As String, ByVal wType As Integer, ByVal fOptions As Long, ByVal pServers As Long, ppQueryResultsSet As Long, ByVal pReserved As Long) As Long Private Declare Function DnsRecordListFree Lib "dnsapi" (ByVal pDnsRecord As Long, ByVal FreeType As Long) As Long Private Declare Function lstrlen Lib "kernel32" (ByVal straddress As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Long, ByVal Length As Long) Private Declare Function inet_ntoa Lib "ws2_32.dll" (ByVal pIP As Long) As Long Private Declare Function inet_addr Lib "ws2_32.dll" (ByVal sAddr As String) As Long Private Const DnsFreeRecordList As Long = 1 Private Const DNS_TYPE_A As Long = &H1 Private Const DNS_QUERY_BYPASS_CACHE As Long = &H8 Private Type VBDnsRecord pNext As Long pName As Long wType As Integer wDataLength As Integer flags As Long dwTel As Long dwReserved As Long prt As Long others(35) As Byte End Type Private Sub Command1_Click() MsgBox Resolve("google.com", "208.67.222.222") End Sub Private Function Resolve(sAddr As String, Optional sDnsServers As String) As String Dim pRecord As Long Dim pNext As Long Dim uRecord As VBDnsRecord Dim lPtr As Long Dim vSplit As Variant Dim laServers() As Long Dim pServers As Long Dim sName As String If LenB(sDnsServers) <> 0 Then vSplit = Split(sDnsServers) ReDim laServers(0 To UBound(vSplit) + 1) laServers(0) = UBound(laServers) For lPtr = 0 To UBound(vSplit) laServers(lPtr + 1) = inet_addr(vSplit(lPtr)) Next pServers = VarPtr(laServers(0)) End If If DnsQuery(sAddr, DNS_TYPE_A, DNS_QUERY_BYPASS_CACHE, pServers, pRecord, 0) = 0 Then pNext = pRecord Do While pNext <> 0 Call CopyMemory(uRecord, pNext, Len(uRecord)) If uRecord.wType = DNS_TYPE_A Then lPtr = inet_ntoa(uRecord.prt) sName = String(lstrlen(lPtr), 0) Call CopyMemory(ByVal sName, lPtr, Len(sName)) If LenB(Resolve) <> 0 Then Resolve = Resolve & " " End If Resolve = Resolve & sName End If pNext = uRecord.pNext Loop Call DnsRecordListFree(pRecord, DnsFreeRecordList) End If End Function 
+4
source

Can you use the WMI DNS provider to install the DNS system, and then use GetHostByAddr

0
source

This is not an answer, but a very important note for wqw post :

Security warning in lstrlen function (lines 5 and 55):

Using this function incorrectly can compromise the security of your application . lstrlen assumes lpString is a null string or NULL. If this is not the case, it may result in a buffer overflow or denial of service against your application.

Consider using one of the following alternatives: StringCbLength or StringCchLength .

0
source

Source: https://habr.com/ru/post/1300525/


All Articles