473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Interoperabiliy of boost::signals and managed event receivers

Hello all.

I've got a bunch of existing, non managed, C++ DLLs that export types
with, among other things, public events implemented using the
boost::signals library.

Now, I need to have these DLL interoperate with managed code. Among
others things, the managed code need to be able to register for
notification with the boost signals. I've tried different approaches
(trying to register a pinned delegate with the boost::signal, etc...),
but the only thing I could make to work is the ugly code below :

///Unmanaged DLL, compiled without /clr
///file event_source_lib.h
#ifdef EVENT_SOURCE_LIB_EXPORTS
#define EVENT_SOURCE_LIB_API __declspec(dllexport)
#else
#define EVENT_SOURCE_LIB_API __declspec(dllimport)
#endif

#include <boost/signals.hpp>

// This class is exported from the event_source_lib.dll
class EVENT_SOURCE_LIB_API EventSource {
public:
boost::signal<void (int)> event;
void RaiseEvent();

};

///Unmanaged DLL, compiled without /clr
///file event_source_lib.cpp
#include "event_source_lib.h"
void EventSource::RaiseEvent()
{
event(3);
}
///managed C++ console app
///file event_sink.cpp
#include "event_source_lib.h"
#include <boost/bind.hpp>
#include <vcclr.h>
#using <mscorlib.dll>

using namespace System;

__gc class EventSink
{
public:
void Sink(int a);
};

void EventSink::Sink(int a)
{
System::Console::WriteLine(S"In managed handler");
}
static void ManagedHandler(gcroot<EventSink*> instance, int a)
{
instance->Sink(a);
}

#pragma unmanaged
static void UnmanagedHandler(gcroot<EventSink*> instance, int a)
{
ManagedHandler(instance, a);
}
//If I do this directly in the _tmain funcion (managed code), I've got
an SEH exception at runtime.
static boost::signals::connection RegisterEventSink(EventSource&
source, gcroot<EventSink*> instance)
{
return source.event.connect(boost::bind(&UnmanagedHandler ,
instance, _1));
}

#pragma managed

int _tmain()
{
EventSource source;
EventSink* sink=new EventSink;
boost::signals::connection con=RegisterEventSink(source,
gcroot<EventSink*>(sink)); //(1)
source.RaiseEvent();
con.disconnect();
return 0;
}
This solution requires *3* static functions (2 unmanaged and 1
managed) to be able to register a member funcion of a __gc class with
the boost signal. My real librarie has tens of different signals, with
different signatures, and the managed wrapper code would soon be an
horrible mess of macros hack if I use this technique.

A curiosity about this code : If I do not declare the unmanaged
RegisterEventSink and call boost::signal::connect directly from
_tmain, I've got an SEHException at runtime. However, if I declare
RegisterEventSink *but do not call it*, the code run smooth. The line
(1) can therefore be replaced with:
boost::signals::connection
con=source.event.connect(boost::bind(&UnmanagedHan dler,
gcroot<EventSink*>(sink), _1));
I suspect that the boost::bind specialization is (correctly) build
when compiling RegisterEventSink, and the same specialization is used
in _tmain. But if RegisterEventSink is commented out, the compiler is
unable to compile the bind specialization when in a managed context.

Does anyone have another cleaner solution for this problem?
Thanks.

Arnaud
MVP - VC
Nov 17 '05 #1
0 1905

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5...
3
by: Drew | last post by:
Has anyone used boost's intrusive_ptr with Managed C++ extensions? I have a small project where the executable (managed) is linked to a native C++ dll. The code looks like: int _tmain() {...
13
by: Russell Hind | last post by:
I'm trying to use boost::bind and boost::function inside managed code, but there appears to be some code generation problems. The following code compiles fine, but the function object throws an...
0
by: Russell Hind | last post by:
Is it possible to create a boost::bind object for a managed class method? With non-managed classes, I can do boost::function<void ()> f = boost::bind(&Class_c::Function, this); Is there a way...
0
by: Arnaud Debaene | last post by:
Could someone explain why the following code works when compiled without /clr but fails at runtime with a boost::bad_function_call exception when compiled with /clr? #include <iostream> using...
9
by: atomik.fungus | last post by:
g++ wont let me compile boost signals, not even the example that comes in the documentation. I have the libboost_signals.a binary but im not sure if im using it correctly. Here is the code ...
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
2
by: timor.super | last post by:
Hi group, I've downloaded the windows compiler djgpp on www.delorie.com and the boost installer on http://www.boost-consulting.com/download.html I would like to be able to create an...
3
by: Lars Uffmann | last post by:
I have this wxWidgets OnButtonClick event handler, that apparently holds a lock on all widgets in my form, but this event handler is supposed to end a thread in the background - while that thread...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.