Windows API Guide: WIN32_FIND_DATA Structure


Declare Function WSAGetLastError Lib "wsock32.dll" () As Long

Platforms

  • Windows 95: Supported.
  • Windows 98: Supported.
  • Windows NT: Requires Windows NT 3.1 or later.
  • Windows 2000: Supported.
  • Windows CE: Requires Windows CE 1.0 or later.

Description & Usage

WSAGetLastError identifies the last error code reported by the last Windows Sockets (Winsock) function call. Note that successful Winsock functions typically do not clear this error code, so WSAGetLastError will identify the last error that occured. Since most Winsock functions only return whether or not they failed, this function should be used to get a better description of the exact error that occured.

Return Value

The function returns one of the Winsock error codes identifying the last Winsock error that occured.

Visual Basic-Specific Issues

None.

Parameters

None.

Example

Intentionally cause an error by failing to initialize Winsock before calling its functions. This is the same code that is in the WSAStartup page's example, except that the call to WSAStartup has been removed. This code intentionally does not work. Any Winsock errors that occur should be reported via WSAGetLastError.

To run this example, place a text box named txtDomain and a command button named cmdGetIP on a form window.

' This code is licensed according to the terms and conditions listed here. ' Declarations and such needed for the example: ' (Copy them to the (declarations) section of a module.) Public Declare Function WSAGetLastError Lib "wsock32.dll" () As Long Public Declare Function WSACleanup Lib "wsock32.dll" () As Long Public Type HOSTENT h_name As Long h_aliases As Long h_addrtype As Integer h_length As Integer h_addr_list As Long End Type Public Const AF_INET = 2 Public Declare Function gethostbyname Lib "wsock32.dll" (ByVal name As String) As Long Public Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inaddr As Long) As Long Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _ As Any, ByVal length As Long) Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal _ lpString2 As Any) As Long ' Define some useful API macros. Public Function MAKEWORD(ByVal bLow As Byte, ByVal bHigh As Byte) As Integer MAKEWORD = Val("&H" & Right("00" & Hex(bHigh), 2) & Right("00" & Hex(bLow), 2)) End Function Public Function LOBYTE(ByVal wValue As Integer) As Byte LOBYTE = Val("&H" & Right("00" & Hex(wValue), 2)) End Function Public Function HIBYTE(ByVal wValue As Integer) As Byte HIBYTE = Val("&H" & Left(Right("0000" & Hex(wValue), 4), 2)) End Function ' In the event of an error, print the Winsock error number in the Debug ' window.  Ideally the error code would be used in a more meaningful ' manner in a real program (e.g., printing out a textual error message), but this ' is just an example. Public Sub ReportWinsockError () Dim winsockError As Long  ' the Winsock error code winsockError = WSAGetLastError() Debug.Print "Winsock error"; winsockError; "reported!" End Sub ' *** Place the following code inside the form window. *** Private Sub cmdGetIP_Click() Dim sockinfo As WSADATA  ' information about Winsock Dim hostinfo As HOSTENT  ' information about an Internet host Dim pHostinfo As Long    ' pointer to a HOSTENT structure Dim pIPAddress As Long   ' pointer to an IP address dword Dim ipAddress As Long    ' an IP address, packed into a dword Dim pIPString As Long    ' pointer to an IP address formatted as a string Dim ipString As String   ' holds a human-readable IP address string Dim retval As Long       ' generic return value ' Get information about the domain specified in txtDomain. pHostinfo = gethostbyname(txtDomain.Text) If pHostinfo = 0 Then Debug.Print "Unable to resolve domain name." ReportWinsockError Else ' Copy the data into a HOSTENT structure. CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo) If hostinfo.h_addrtype <> AF_INET Then Debug.Print "A non-IP address was returned." ReportWinsockError 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

See Also

-->

Category

Winsock

Back to the Function list.
Back to the Reference section.


Last Modified: October 29, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/w/wsagetlasterror.html



Windows API Guide
Windows API Guide - Reference - Volume 1: Version 3.0 For the MS-DOS and PC-DOS Operating Systems
ISBN: B001V0KQIY
EAN: N/A
Year: 1998
Pages: 610

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net