Hi,
I'm trying to use the gethostbyname function from wsock32.dll and failing dismally Has anyone got a successful implementation of this in VB.NET? My ulitimate goal is to resolve NetBIOS names to IP Addresses. I can use the framework DNS features if a DNS server is present on the network but this cannot use WINS if no DNS server is available.
Any help or pointers are apprecatiated. Here is the code I've managed to do so far. I've commented out a big chunk as I am trying to just open a socket. I'm pretty sure I've got the structures wrong.
Code
Imports System.Runtime. InteropServices
Public Class Form4
.....
<StructLayout(L ayoutKind.Seque ntial)> Public Structure WSADATA
Dim wVersion As Int16
Dim wHighVersion As Int16
<MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=257) > Dim szDescription As String
<MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=129) > Dim szSystemStatus As String
Dim iMaxSockets As Integer
Dim iMaxUdpDg As Integer
Dim lpVendorInfo As Integer
End Structure
Public Declare Function WSAStartup Lib "wsock32.dl l" (ByVal wVersionRequest ed As Short, ByVal lpWSAData As WSADATA) As Integer
Public Declare Function WSACleanup Lib "wsock32.dl l" () As Integer
<StructLayout(L ayoutKind.Seque ntial)> Public Structure HOSTENT
Dim h_name As Integer
Dim h_aliases As Integer
Dim h_addrtype As Short
Dim h_length As Short
Dim h_addr_list As Integer
End Structure
Public Const AF_INET = 2
Public Declare Function gethostbyname Lib "wsock32.dl l" (ByVal name As String) As Integer
Public Declare Function inet_ntoa Lib "wsock32.dl l" (ByVal inaddr As Long) As Integer
Public Declare Sub CopyMemory Lib "kernel32.d ll" Alias "RtlMoveMem ory" (ByRef Destination As IntPtr, ByRef Source As String, ByVal length As Integer)
Public Declare Function lstrlen Lib "kernel32.d ll" Alias "lstrlenA" (ByRef lpString As String) As Integer
Public Declare Function lstrcpy Lib "kernel32.d ll" Alias "lstrcpyA" (ByRef lpString1 As String, ByRef lpString2 As String) As Integer
' Define a relevant API macro.
Public Function MAKEWORD(ByVal bLow As Byte, ByVal bHigh As Byte) As Integer
MAKEWORD = Val("&H" & Microsoft.Visua lBasic.Right("0 0" & Hex(bHigh), 2) & Microsoft.Visua lBasic.Right("0 0" & Hex(bLow), 2))
End Function
' *** Place the following code inside the form window. ***
Private Sub Test()
Dim sockinfo As WSADATA ' information about Winsock
Dim hostinfo As HOSTENT ' information about an Internet host
Dim pHostinfo As Integer ' pointer to a HOSTENT structure
Dim pIPAddress As Integer ' pointer to an IP address dword
Dim ipAddress As Integer ' an IP address, packed into a dword
Dim pIPString As Integer ' pointer to an IP address formatted as a string
Dim ipString As String ' holds a human-readable IP address string
Dim retval As Integer ' generic return value
' Open up a Winsock session, using version 2.2.
retval = WSAStartup(MAKE WORD(2, 2), sockinfo)
If retval <> 0 Then
Debug.WriteLine ("ERROR: Attempt to open Winsock failed: error " & retval)
Exit Sub
End If
' Get information about the domain specified in txtDomain.
pHostinfo = gethostbyname(" BRAZIL")
If pHostinfo = 0 Then
Debug.WriteLine ("Unable to resolve domain name.")
Else
' Copy the data into a HOSTENT structure.
' CopyMemory(mars hal.StructureTo Ptr(hostinfo, pHostinfo, Len(hostinfo))
' If hostinfo.h_addr type <> AF_INET Then
' Debug.Print("A non-IP address was returned.")
' Else
' ' Copy the pointer to the first (and probably only) IP address in the structure.
'CopyMemory pIPAddress, ByVal hostinfo.h_addr _list, 4
' ' Copy the actual IP address.
'CopyMemory ipAddress, ByVal pIPAddress, 4
' ' Convert the IP address into a human-readable string.
' pIPString = inet_ntoa(ipAdd ress)
' ' Copy the result into a string variable.
' ipString = Space(lstrlen(p IPString))
' retval = lstrcpy(ipStrin g, pIPString)
' ' Print the result: a human-readable IP address.
' Debug.Print(ipS tring)
'End If
End If
' Close the Winsock session.
retval = WSACleanup()
End Sub
Private Sub Form4_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Test()
End Sub
End Class
Thanks
Glen
Author of Flarepath Windows Update Analyser. Download your copy at www.flarepath.com/fwua