Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function terminates use of the ws2.dll.

Syntax

int  WSACleanup (void);

Parameters

None.

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 WSAGetLastErrorfunction.

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

Error code Description

WSANOTINITIALISED

A successful WSAStartupcall must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAEINPROGRESS

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

Remarks

An application or dynamic-link library (DLL) is required to perform a successful WSAStartupcall before it can use Windows Sockets services. When it has completed the use of Windows Sockets, the application or DLL must call WSACleanupto 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/ WSARecvFromwith 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 WSACleanupwas called are reset and automatically de-allocated as if closesocketwas called. Sockets that have been closed with closesocketbut 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 shutdownto close the connection, and then wait until the close completes before calling closesocketand 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 WSACleanupcall are closed and de-allocated as they would be if closesocketwas called. Lingering sockets, sockets that were closed with closesocketbut 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 closesocketand 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 WSACleanupfor every successful call to WSAStartupmade by a task. Only the final WSACleanupcall for that task does the actual cleanup; the preceding calls simply decrement an internal reference count in the ws2.dll.

Requirements

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