Initializing the WinSock2 library

The WSAStartUp Function


The WSAStartUp function initializes the WinSock2 library for use by your program, and must be the first WinSock2 library function called by your program. Calling the WSAStartUp function serves two major purposes:


The system include file contains the following declaration for the WSAStartUp function:

function WSAStartup(wVersionRequested : shortword; var lpWSAData : WSADATA) : integer;
  external dll='ws2_32.dll';


The First Argument

The first argument passed to the WSAStartUp function is an expression of shortword type. This argument indicates the highest version of the Windows Sockets API that your program can support. Bits 0 through 7 (i.e. the least significant byte) specify the major version number of the API. Bits 8 through 15 (i.e. the most significant byte) specify the minor version number of the API. So to specify that the highest Windows Socket API version, that your program can support, is 2.1, you could use 2+1*256, or $0102, or just 258 as the value of this argument.

The Second Argument

The second argument passed to the WSAStartUp function, is a reference to a variable of type WSADATA. This argument is used by the WSAStartUp function to pass information back to your program. NOTE: The WSADATA type is declared in the system include file, as a record type, with the following important fields:

  1. wVersion, contains the version of the Windows Sockets API that your program should use (the least significant byte contains the major version number, and the most significant byte contains the minor version number).
  2. wHighVersion, contains the highest version of the Windows Sockets API that can be supported by the WinSock2 library DLL (the layout of this field is the same as the layout of the first field).
  3. szDescription, contains a description of the Windows Sockets API implementation.
  4. szSystemStatus, contains status or configuration information that might be useful to the user or the support staff (this field may be empty).

Return Values

The WSAStartUp function returns a value of integer type that indicates whether the call was successful. A value of zero means that the call succeeded. A non-zero value is an error code that indicates the reason why the call failed.

Sample Program

The sample program below illustrates how to call the WSAStartUp and WSACleanUp functions.

(*$I *)
program start(output);
 data : WSADATA;
 iRet : integer;
 iRet := WSAStartUp($0202, data);
 if iRet <> 0 then
   writeln('WSAStartUp call failed. Return Code=', iRet);
 writeln('WSAStartUp call succeeded.');
 writeln('Version: $', hex(data.wVersion));
 writeln('High Version: $', hex(data.wHighVersion));
 writeln('Description: ', data.szDescription);
 writeln('System Status: ', data.szSystemStatus);
 iRet := WSACleanUp

Reference Information

The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at