Help with gethostbyname winsock api call

G

Guest

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(LayoutKind.Sequential)> Public Structure WSADATA
Dim wVersion As Int16
Dim wHighVersion As Int16
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=257)> Dim szDescription As String
<MarshalAs(UnmanagedType.ByValTStr, 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.dll" (ByVal wVersionRequested As Short, ByVal lpWSAData As WSADATA) As Integer
Public Declare Function WSACleanup Lib "wsock32.dll" () As Integer

<StructLayout(LayoutKind.Sequential)> 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.dll" (ByVal name As String) As Integer
Public Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inaddr As Long) As Integer
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As IntPtr, ByRef Source As String, ByVal length As Integer)
Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByRef lpString As String) As Integer
Public Declare Function lstrcpy Lib "kernel32.dll" 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.VisualBasic.Right("00" & Hex(bHigh), 2) & Microsoft.VisualBasic.Right("00" & 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(MAKEWORD(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(marshal.StructureToPtr(hostinfo, pHostinfo, Len(hostinfo))
' If hostinfo.h_addrtype <> 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(ipAddress)
' ' Copy the result into a string variable.
' ipString = Space(lstrlen(pIPString))
' retval = lstrcpy(ipString, pIPString)
' ' Print the result: a human-readable IP address.
' Debug.Print(ipString)
'End If
End If

' Close the Winsock session.
retval = WSACleanup()
End Sub

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Test()
End Sub
End Class

Thanks

Glen

Author of Flarepath Windows Update Analyser. Download your copy at www.flarepath.com/fwua
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?R2xlbiBDb253YXk=?= said:
Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequested As Short, ByVal lpWSAData As WSADATA) As Integer

Why do you pass the 2nd parameter 'ByVal'?
 

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

Top