Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 401
hookenz Avatar asked Dec 08 '09 20:12

hookenz


People also ask

When should I call WSAStartup?

When it has finished using the services of the Winsock DLL, the application must call WSACleanup to allow the Winsock DLL to free internal Winsock resources used by the application. An application can call WSAStartup more than once if it needs to obtain the WSADATA structure information more than once.

What is WSAStartup?

The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and to retrieve details of the specific Windows Sockets implementation.

When should I call WSACleanup?

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.


2 Answers

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
like image 171
hookenz Avatar answered Oct 06 '22 10:10

hookenz


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

like image 24
Remy Lebeau Avatar answered Oct 06 '22 11:10

Remy Lebeau