Winsock API

Note:  All of the the information on this site is Copyright © 1999, Computer Services Contracting, unless otherwise noted and can be used freely for any non-commercial use as long as credit is given to Computer Services Contracting and/or the noted author, and is presented here for educational purposes only.  Contact Computer Services Contracting at for permission to use this information for commercial use.

I posted requests on many news groups asking for information about Winsock programming.   I got quite a few bits and pieces, nothing very complete, allot of it was   conflicting. I am trying to put it all together and post it to this site along with a bunch of other stuff.

As I work through this problem, it will be solved in a few phases.  First, multiple mini-apps in WinSock blocked mode.  Second, Rolled into a single app.   Third, converted to non-blocked WinSock mode.  Finally converted to Class modules and Controls.

All code is for a 32bit Windows environment.

 

WinSock Blocked Mode
Naming Conventions
I will be using the "Visual Basic Programming Conventions from Microsoft Consulting Services" from Microsoft as a basis.  I have some additions that I will be using.  Type structures and classes will end in an underscore and an identifying suffix.  This suffix is used as a prefix in the declaration.   All API declarations are prefixed with "api".   All Functions are prefixed with "fnc".  All Subroutines are prefixed with "sub".
Error Handling
I think that Microsoft missed the boat when it didn't include access to Module and Procedure names along with the Call Chain. 

I use a Collection to capture Call Chain Information.  This process is contained in the module cscCallChain.bas.  By putting an entry at the beginning and end of each process, a Call Chain that includes Module and Procedure names can be built.  If an error occurs The entry is not removed and an error handling routine can use the Collection to print out a Call Chain along with the error information.  In a production environment,   this information would be logged and a more meaningful English message would be presented to the User.  I don't use this in properties. 

All Dll functions that are used, are wrapped by VB code that converts error information returned by the function, to err.raise type errors.  Dll subroutines can be called directly without the VB wrapper, but some will be wrapped anyway.

All that is needed at this point, is to add an error handler in the procedure(s) that would pass control to VB.  This limits the number of error handlers required by excluding any subordinate procedures from having an error handler in it.

Note: I am providing cscCallChain.bas before it is complete.  I will be adding documentation to it.

Winsock Information
The WinSock Dll procedures utilize a few Variable modifying functions in many places.  I have used the procedures mentioned in Microsoft article Q189323 HOWTO: Convert Between Signed and Unsigned Numbers and Tip 22: Converting DWords, Words, and Bytes and built cscVariables.bas.

fncTrimNulls is one that I made myself.

I used Articles Q154512 SAMPLE: Getting HostAddress Using Windows Sockets and Q160215 HOWTO: Obtain the Host IP Address Using Windows Sockets to build cscWinSock.bas.

This is the module that provides access to the WinSock Dll.

WinSockInfo project directions:
Create a directory that will hold all of the projects in this series. 
Create a directory called "Common" under that directory, download  Common.zip (**Updated 2/12/99**) and expand it into that directory. 
Create a directory called "WinSockInfo", and download WinSockInfo.zip and expand it into that directory.

Common.zip includes cscCallChain.bas, cscVariables.bas, cscUser.bas, cscMAC.bas and cscWinSock.bas. 

WinSockInfo.zip contains WinSockInfo.frm, WinSockInfo.frx, WinSockInfo.vbw and WinSockInfo.vbp

After expanding the files open the WinSockInfo.vbp file and run it from VB.
WinSockInfo displays the information contained in the WSADATA Type structure that is returned in the WSAStartup procedure.

Note: I am providing cscVariables.bas and cscWinSock.bas (**Updated 2/12/99**) before they are complete.  I will be adding documentation to it.
Note: Common.zip and WinSockInfo.zip are not complete, but will run. 

User Name
I used article Q161394 VBA: Sample Code to Retrieve the Current User Name to build cscUser.bas.  As the title of the article notes, This module retrieves the current User Name.

Note: I am providing cscUser.bas before it is complete.  I will be adding documentation to it.

MAC Address
I used article Q175472 HOWTO: Get Network Adapter Address from Visual Basic to build cscMAC.bas.  This module retrieves the MAC address of the network adapter.

Note: I am providing cscMAC.bas before it is complete.  I will be adding documentation to it.

Local Information
In the WinSockInfo project, the data was displayed in text boxes.  In the LocalInfo project, data is displayed in a TreeView.

LocalInfo project directions:
If you didn't do so in the WinSockInfo project, create a directory that will hold all of the projects in this series. 
If you didn't do so in the WinSockInfo project, create a directory called "Common" under that directory, download Common.zip (**Updated 2/12/99**) and expand it into that directory. 
Create a directory called "LocalInfo", and download LocalInfo.zip (**Updated 2/12/99**) and expand it into that directory.

Common.zip includes cscCallChain.bas, cscVariables.bas, cscUser.bas, cscMAC.bas and cscWinSock.bas. 

LocalInfo.zip contains LocalInfo.frm, LocalInfo.frx, LocalInfo.vbw and LocalInfo.vbp

After expanding the files open the LocalInfo.vbp file and run it from VB.

LocalInfo displays the information contained in the WSADATA Type structure that is returned in the WSAStartup procedure, the Host Name from the GetHostName call, the IP addresses from the GetHostByName call,  the user name returned from GetUser and the MAC address returned from the ASTAT type structure that is returned from  the NETBIOS call.  All of the information is displayed in a TreeView control.

DNS Lookup and Reverse DNS Lookup.
DNS lookups are performed by the GetHostByName and GetHostByAddr functions of the Winsock Dll.and returned in the HOSTENT structure.  The GetHostByName was used in the LocalInfo project.  I could not find any VB examples on the Microsoft site for GetHostByAddr function.  I used the Winsock.h file provided in the Win32 SDK, the Windows Sockets GetHostByAddr function definition, the HOSTENT structure definition and  WINSOCK.EXE- Windows Sockets API Spec (ASCII .TXT)  to define the GetHostByAddr declare statement and followed the previous Microsoft examples to build the code.  In the process of working with the GetHostByAddr function I built code that retrieved the Alias names in the HOSTENT structure and updated the LocalInfo project to include this information.

The DNSLookup project displays the data in text boxes as did the WinSockInfo project.

DNSLookup project directions:
If you didn't do so in the previous project, create a directory that will hold all of the projects in this series. 
If you didn't do so in the previous project, create a directory called "Common" under that directory, download Common.zip (**Updated 2/12/99**) and expand it into that directory. 
Create a directory called "DNSLookup", and download DNSLookup.zip  (**New 2/12/99**) and expand it into that directory.

Common.zip includes cscCallChain.bas, cscVariables.bas, cscUser.bas, cscMAC.bas and cscWinSock.bas. 

DNSLookup.zip contains DNSLookup.frm, DNSLookup.frx, DNSLookup.vbw and DNSLookup.vbp

After expanding the files open the DNSLookup.vbp file and run it from VB.

In DNSLookup you can enter either a Domain Name or an IP Address to retrieve the DNS information.  Errors creating the HOSTEN structure are reported in the Domain Name or IP Address field corresponding to the type of  lookup made.  To see Alias names, try a lookup for "www.mysite.com".  To see Secondary IP addresses, try a lookup for "www.search.com".

Ping
It looks like in order to do a ping using Winsock, it must be able to do Raw connections. Microsoft did not support Raw connections in Winsock until version 2.0. They created a DLL called ICMP.Dll that would do the tasks. This would include ping and tracert. The best way would be to look for the ability to do Raw connections with Winsock and if that fails use ICMP.Dll. I think the first shot will be with straight ICMP.Dll.
Tracert
Tracert also uses Raw connections like ping.
WhoIs
 
CGI