468,513 Members | 1,881 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Help with gethostbyname winsock api call


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.

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.")
' 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
End Sub
End Class



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

Nov 20 '05 #1
1 3394
* "=?Utf-8?B?R2xlbiBDb253YXk=?=" <gl*********@flarepath.com> scripsit:
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'?

Herfried K. Wagner [MVP]
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by yawnmoth | last post: by
1 post views Thread by N.Naeem | last post: by
1 post views Thread by User | last post: by
reply views Thread by yanyu81 | last post: by
1 post views Thread by Quentin Carbonneaux | last post: by
6 posts views Thread by cpptutor2000 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.