Share via


Using WSAStartup to Initialize Winsock

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

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

Reference

WSAStartup
WSADATA
WSACleanup
WSAGetLastError

Other Resources

Winsock Application Development