473,772 Members | 3,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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

Nov 20 '05 #1
1 3814
* "=?Utf-8?B?R2xlbiBDb25 3YXk=?=" <gl*********@fl arepath.com> scripsit:
Public Declare Function WSAStartup Lib "wsock32.dl l" (ByVal wVersionRequest ed 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
6636
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 only speedup subsequent calls to gethostbyname - the first call is still as slow as ever. so is there anything i can do to speed this up? perhapes i can just implement a function equivalent to gethostbyname using fsockopen, or something?
1
3490
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 connection of a client i would like the application to call an ActiveX.exe which also has a Winsock Control (Winsock2), and i would like to transfer the Winsock1 information to Winsock2, hence allowing the client
1
402
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 foreign host, how can it be fast (instant)? With winsock (in VB6) it is instant connection.
7
2855
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 of data transportation... now, indigo is the new communication layer of the CLR, - Does indigo uses Winsock internally? - Is it possible to use indigo for such a task? - I assume using indigo has it's performance penalty ( doesn't it ? - Does...
0
948
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. But after I executing the program, I found that if(argc < 3) { printf("usage: %s <server> <data1> <data2> ... <dataN>\n",argv); exit(1); } the output is <server> <data1> <data2> ...
1
7223
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 blocking system call. So I read texts about this and I planed to use a hack with alarm and setjmp/longjmp. My program works until there is a timeout, when it occurs, all the gethostbyname calls which follow fail, backtrace in gdb give me this : #0 ...
18
3661
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 gethostbyname allocated? struct hostent *lHostInfo; lHostInfo = gethostbyname(ipHost.c_str()); memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list, lHostInfo- delete lHostInfo;
6
6857
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; .............................. ............................... if(gethostname(host, sizeof(host)) < 0) {
3
7619
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 Expressions to strip out the HTML code itself, the problem is actually getting the web pages from the internet. I tried using the Microsoft Internet Transfer Control but my client was experiencing problems with some web pages not downloading, this...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.