$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-users] [ASIO] Where to use CoInitizeEx
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2009-06-03 04:49:27
Hi,
in a library I have a factory function that creates objects:
---- Main Library File ----
// Main io_service
static boost::asio::io_service          io_service;
static boost::scoped_ptr<boost::thread> io_service_thread;
static boost::asio::io_service::work*   p_work;
static BOOL thread_started;
EXTERN_C EUROATLIB_API TETRAMotoHndl CALL GetTETRAMotoObj()
{
  if (!thread_started) {
    try {
      // create the work object on the heap
      p_work = new boost::asio::io_service::work(io_service);
      // run the IO service as a separate thread
      io_service_thread.reset(
         new boost::thread(
           boost::bind(&boost::asio::io_service::run, &io_service)
         )
      );
      thread_started = !thread_started;
    } catch (boost::thread_resource_error e) {
      // Failed to create the new thread
      return 0;
    }
  }
  // create the new object
  TETRAMoto* pTETRAMoto = new TETRAMoto(io_service);
  // Assign a reference to io_service work object:
  // This reference is used to counting the objects created.
  // When the last TETRAMoto object is deleted the main work object
  // is destroyed and the io_service thread ends.
  pTETRAMoto->_io_work_ptr.reset(p_work);
  // return the object
  return pTETRAMoto;
};
---------------------------------
These TETRAMoto objects creates another object, ATDispatcher, that use a
 SerialPort (which is a wrapper around asio::serial_port) to
continuously read and write to RS232.
When the SerialPort receive data, pass it to the ATDispatcher that
raises a signal to the TETRAMoto object that parses the data and raise
the event (always a signal) to the extern:
asio::serial_port -> SerialPort -> ATDispatcher -> TetraMoto -|-> EXTERN
Now I have to encapsulate this lib into an ActiveX DLL to be used by VB6
and I have to call CoInitizeEx in the thread that "raise" events..
My question is: where I have to call CoInitizeEx?
Thanks in advance,
Daniele.