The WSAStartup function initiates the use of the Windows Sockets DLL. Before any other Windows Sockets function can be called, a Winsock application or DLL must successfully call WSAStartup.
int WSAStartup(
[in] WORD wVersionRequested,
[out] LPWSADATA lpWSAData
);
The version of the Winsock protocol specification that the calling application, or the DLL it calls, requires. The high byte specifies the minor version number; the low byte specifies the major version number.
For example, to request Winsock version 2.2, this parameter should be set to 0x0202
.
A pointer to the WSADATA data structure that is to receive details about the Winsock implementation. This structure is filled by WSAStartup.
Every application or DLL that calls the Winsock interface must call WSAStartup one or more times before calling any other Winsock functions. It is recommended that an application call WSAStartup once during its initialization.
The wVersionRequested parameter should be set to the highest version of the Windows Sockets specification that can be supported by the DLL. The highest supported version by the Winsock implementation is a value between 2.0 and 2.2.
When WSAStartup returns successfully, the WSADATA data structure pointed to by lpWSAData is filled with information about the Winsock implementation.
The calling process can call WSAStartup multiple times, as long as it also calls WSACleanup an equal number of times. The Winsock DLL keeps a count of how many times WSAStartup has been called. The DLL only performs its actual initialization tasks the first time it is called. It increments its internal counter for each successful call to WSAStartup.
To allow an application to specify the Winsock version it needs, the WSAStartup function supports the use of the MAKEWORD
macro, defined in Winsock2.h
, as shown in the following example:
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2); // Request Winsock version 2.2
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
// Tell the user that Winsock DLL could not be found or is not accessible
// or that the desired version of Winsock is not supported.
fprintf(stderr, "WSAStartup failed with error %d\n", err);
// handle error
}
// ... other Winsock functions can be called now ...
// When done, call WSACleanup
// WSACleanup();
Requires Winsock 1.1.
Requires Winsock 1.1.
Winsock2.h (include Winsock.h)
Ws2_32.lib
Ws2_32.dll