Using WSAStartup to Initialize Winsock

An application must call the WSAStartup function to initialize Winsock, regardless of which version of Winsock is being used. WSAStartup initializes Winsock2.dll and a WSADATA structure that contains the details of the Winsock implementation. When an application or DLL has finished using Winsock2.dll, it must call WSACleanup to enable Ws2.dll to free any resources for the application. For every call to WSAStartup, there must be a call to WSACleanup.

The following code example shows how to use WSAStartup.

if (WSAStartup (MAKEWORD(2,2), &WSAData) != 0) 
{
    MessageBox (NULL, TEXT("WSAStartup failed!"), TEXT("Error"), MB_OK);
    return FALSE;
}

If successful, WSAStartup returns 0. After WSAStartup returns, an application cannot call WSAGetLastError to determine the error value

The WSADATA structure pointed to by lpWSAData stores Winsock initialization data returned by a call to WSAStartup. WSADATA contains Ws2.dll implementation data. An application or DLL can call WSAStartup repeatedly if it needs to obtain the WSADATA structure data more than once.

The following table shows values that WSAStartup assigns to the members of WSADATA.

WSADATA member Assigned value
Wversion 2.2
WhighVersion 2.2
SzDescription NULL string
SzSystemStatus NULL string
ImaxSockets 100
ImaxUdpDg 0
LpVendorInfo NULL

See Also

WSAStartup | WSADATA | WSACleanup | WSAGetLastError | Winsock Application Development

 Last updated on Saturday, April 10, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.