how to replace WSAStartup( MAKEWORD(2, 2), &data ); with boost:asio?

1.4k Views Asked by At

I have native dll project which I use from C# via CLI wrapper. In Initialize method of dll i'm calling

WSAStartup(MAKEWORD(2, 2), &data);

This works fine. Now I"ve decided to move entire dll to boost::asio (because I will port it to Linux and I want to enable /Za compiler option).

The question is - where and how should I replace WSAStartup call?

  • move it to CLI wrapper or to the top-level C# project?
  • replace it with some boost::asio call (which method to call?)
1

There are 1 best solutions below

1
On

Boost.Asio initialises Winsock before main() - it uses a static object for this purpose. Here is an excerpt from winsock_init.hpp:

// Static variable to ensure that winsock is initialised before main, and
// therefore before any other threads can get started.
static const winsock_init<>& winsock_init_instance = winsock_init<>(false);

So, in your case Winsock will get initialised on dll load.

(That's said, I'm afraid you can't compile Asio under Windows with /Za, but you also don't have to do this in order to make your code portable to Linux.)