Search code examples
windowswinsock2wsastartupwindows-socket-api

What happens if multiple WSAStartup calls requesting different WinSock versions occur in a process?


WSAStartup() can be called multiple times in a single process, as long as the requested version is supported by the WinSock dll and that calls to WSAStartup() and WSACleanup() are balanced. In addition to that, Multiple sockets using different WinSock versions is allowed. (see this post: Is it possible to tell if WSAStartup has been called in a process?)

In that case, how do these different WinSock versions coexist?

For example, what if I request the use of a specific WinSock version for my application, and my application also loads a third party dll that happens to request the use a different version? What version gets used, and when?


Solution

  • It says in the MS doc -

    An application can call WSAStartup more than once if it needs to obtain the WSADATA structure information more than once. On each such call, the application can specify any version number supported by the Winsock DLL.

    The last sentence implies, that those subsequent calls don't actually request a different winsock version, just getting the existing one that's saved in wsadata.

    If you wanted to change the winsock version in the middle of the program, I suppose you could call WSACleanup (as many times as needed), and "start fresh" with a new WSAStartup