Is it possible to tell if WSAStartup has been called in a process?

14,848

Solution 1

Yes it is possible.

And here is how it's done.

bool WinsockInitialized()
{
    SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET){
        return false;
    }

    closesocket(s);
    return true;
}

int main()
{
    //...
    if ( !WinsockInitialized() )
       // Init winsock here...

    // Carry on as normal.
    // ...         
}

But it's not really necessary to do this. It's quite safe to call WSAStartup at any time. It's also safe to end each successful call to WSAStartup() with a matching call to WSACleanup().

e.g.

// socket calls here would be an error, not initialized
WSAStartup(...)
// socket calls here OK

WSAStartup(...)
// more socket calls OK

WSACleanup()
// socket calls OK

WSACleanup()

// more socket calls error, not initialized

Solution 2

  • No, it is not possible to tell if WSAStartup() has already been called.

  • Yes, WSAStartup() can be called multiple times in a single process, as long as the requested version is supported by the WinSock DLL. Calls to WSAStartup() and WSACleanup() must be balanced.

  • WinSock initialization is a negotiated process; you are responsible for validating the info that WSAStartup() returns to make sure it meets your app's requirements.

  • Existing sockets are not affected by subsequent WSAStartup() calls.

  • Multiple sockets using different WinSock versions is allowed.

See the WSAStartup() documentation for more information.

Share:
14,848

Related videos on Youtube

hookenz
Author by

hookenz

Updated on April 17, 2022

Comments

  • hookenz
    hookenz about 2 years

    I've started writing an ActiveX control that makes use of sockets.

    Applications that use this control may or may not also use sockets. Is it possible for my control to tell whether WSAStartup has been called?

    If not, call it. A little test reveals that calling WSAStartup multiple times is tollerated. But what happens if a different winsock version is requested? will this break other parts of the application?

  • hookenz
    hookenz over 14 years
    In addition to this, I've found that so long as the number of WSACleanup calls match the number of successful WSAStartup calls then calling WSACleanup will not affect other socket calls. Provided of course you don't call WSACleanup too many times.
  • czz
    czz almost 9 years
    It's NOT safe to call WSAStartup at any time. From MSDN: "The WSAStartup function typically leads to protocol-specific helper DLLs being loaded. As a result, the WSAStartup function should not be called from the DllMain function in a application DLL. This can potentially cause deadlocks"
  • Remy Lebeau
    Remy Lebeau about 7 years
    WinsockInitialized() is returning true instead of false if socket() fails for any error code other than WSANOTINITIALISED (MSDN shows 13 possible error codes that socket() could fail with). The WSAGetLastError() check is redundant anyway, since socket() either succeeds or fails, checking the error code won't change the result. bool WinsockInitialized() { SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s == INVALID_SOCKET) return false; closesocket(s); return true; }