473,411 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help with gethostbyname winsock api call

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

Nov 20 '05 #1
1 3789
* "=?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]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
1
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...
1
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...
7
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...
0
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....
1
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...
18
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...
6
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;...
3
AaronL
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.