472,958 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 3756
* "=?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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: yawnmoth | last post by:
using the gethostbyname function seems to noticeably slow down pages. some of the comments in php.net's gethostbyname entry suggest using a version that caches the result, but those versions also...
by: N.Naeem | last post by:
Hello all i need a solution to the following problem I have an application which listens to a port for incoming connections from a client via a Winsock. let's call this Winsock1 Upon a...
by: User | last post by:
Hi, I am writing a program in VB.NET and it is very slow when it reach this line : Dim ipHostInfo As IPHostEntry = Dns.Resolve(strMyForeignIP) It stay there at least 5 seconds to reach the...
by: Nadav | last post by:
Hi I am writing some kind of a storage system that have to deal with large amounts of data passing over the net, Now, I Wonder... traditional programming would use win32 Winsock DLL as the means...
by: yanyu81 | last post by:
I had developed the client program under Visual C++ 6.0. I use the Windows socket programming. I compile and build my program using the Win32 Console application.There is no errors and warnings....
by: Quentin Carbonneaux | last post by:
Hello, I'm writing a little program which has to get a FQDN on his stdin and return the IP on stdout, we can do it easily with gethostbyname call but I would like to set up a timeout to this...
by: aj | last post by:
I have the following snippet of code. On some platforms, the delete calls works, on Linux, it core dumps (memory dump) at the delete call. Am I responsible for deleting the memory that...
by: cpptutor2000 | last post by:
Could some C guru please help ? I am writing a Web server on RH 9.0 box. In the section of the code where the Web server has to be initiialized, I have: char host; struct hostent *hp = NULL;...
by: AaronL | last post by:
Hello, I am currently working on a project that has me in sort of a bind. What I want to do is retrieve web pages from the internet, and strip them down to just text. I'll get using Regular...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.