A version of this page is also available for

Windows Embedded CE 6.0 R3


This function terminates use of the ws2.dll.


int  WSACleanup (void);



Return Value

If no error occurs, this function returns zero. If an error occurs, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling the WSAGetLastError function.

In a multithreaded environment, WSACleanup terminates Windows Sockets operations for all threads. The following table shows a list of possible error codes.

Error code Description


A successful WSAStartup call must occur before using this function.


The network subsystem has failed.


A blocking Winsock call is in progress, or the service provider is still processing a callback function.


An application or dynamic-link library (DLL) is required to perform a successful WSAStartup call before it can use Windows Sockets services. When it has completed the use of Windows Sockets, the application or DLL must call WSACleanup to deregister itself from a Windows Sockets implementation and allow the implementation to free any resources allocated on behalf of the application or DLL. Any pending blocking or asynchronous calls issued by any thread in this process are canceled without posting any notification messages or without signaling any event objects. Any pending overlapped send and receive operations (WSASend/WSASendTo/WSARecv/WSARecvFrom with an overlapped socket) issued by any thread in this process are also canceled without setting the event object or invoking the completion routine, if specified. In this case, the pending overlapped operations fail with the error status WSA_OPERATION_ABORTED.

Sockets that were open when WSACleanup was called are reset and automatically de-allocated as if closesocket was called. Sockets that have been closed with closesocket but that still have pending data to be sent can be affected. The pending data can be lost if the ws2.dll is unloaded from memory as the application exits. To ensure that all pending data is sent, an application should use shutdown to close the connection, and then wait until the close completes before calling closesocket and WSACleanup. All resources and internal state, such as queued unposted-posted messages, must be de-allocated so as to be available to the next user.

Sockets opened at the time of a WSACleanup call are closed and de-allocated as they would be if closesocket was called. Lingering sockets, sockets that were closed with closesocket but that still have pending data to be sent, can be affected. Their pending data will be lost if the ws2.dll is unloaded from memory as the application exits. To ensure that data is not lost, an application should shut down the socket before calling closesocket and then WSACleanup. All of the ws2.dll resources and internal state will be reset and/or de-allocated on the final call to WSACleanup.

There must be a call to WSACleanup for every successful call to WSAStartup made by a task. Only the final WSACleanup call for that task does the actual cleanup; the preceding calls simply decrement an internal reference count in the ws2.dll.


Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also