473,703 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_li b.h
#ifdef EVENT_SOURCE_LI B_EXPORTS
#define EVENT_SOURCE_LI B_API __declspec(dlle xport)
#else
#define EVENT_SOURCE_LI B_API __declspec(dlli mport)
#endif

#include <boost/signals.hpp>

// This class is exported from the event_source_li b.dll
class EVENT_SOURCE_LI B_API EventSource {
public:
boost::signal<v oid (int)> event;
void RaiseEvent();

};

///Unmanaged DLL, compiled without /clr
///file event_source_li b.cpp
#include "event_source_l ib.h"
void EventSource::Ra iseEvent()
{
event(3);
}
///managed C++ console app
///file event_sink.cpp
#include "event_source_l ib.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"I n managed handler");
}
static void ManagedHandler( gcroot<EventSin k*> instance, int a)
{
instance->Sink(a);
}

#pragma unmanaged
static void UnmanagedHandle r(gcroot<EventS ink*> 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 RegisterEventSi nk(EventSource&
source, gcroot<EventSin k*> instance)
{
return source.event.co nnect(boost::bi nd(&UnmanagedHa ndler,
instance, _1));
}

#pragma managed

int _tmain()
{
EventSource source;
EventSink* sink=new EventSink;
boost::signals: :connection con=RegisterEve ntSink(source,
gcroot<EventSin k*>(sink)); //(1)
source.RaiseEve nt();
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
RegisterEventSi nk and call boost::signal:: connect directly from
_tmain, I've got an SEHException at runtime. However, if I declare
RegisterEventSi nk *but do not call it*, the code run smooth. The line
(1) can therefore be replaced with:
boost::signals: :connection
con=source.even t.connect(boost ::bind(&Unmanag edHandler,
gcroot<EventSin k*>(sink), _1));
I suspect that the boost::bind specialization is (correctly) build
when compiling RegisterEventSi nk, and the same specialization is used
in _tmain. But if RegisterEventSi nk 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 1938

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

Similar topics

16
2608
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 years now. The first two postings in this group this morning are addressed almost trivially by using libraries from www.boost.org. IMO, the functionality in boost increases the usability of C++ at least as much as the standard library does. ...
3
2297
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() { boost::intrusive_ptr<Unmanaged> pUn = __nogc new Unmanaged();
13
3581
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 exception when you call it with f() saying the function is empty. If you un-comment the #pragma unamanged, it runs fine. It appears to be a code generation problem when compiling function and bind as managed. Thanks Russell
0
1030
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 to do this for __gc classes? I do want to be able to assign the result to a boost::function, but can write my own wrapper if necessary. Thanks
0
1010
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 std::cout; using std::endl; #include <boost/signal.hpp> #include <functional>
9
3170
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 #include "boost/signal.hpp" #include <iostream> int main() { struct HelloWorld {
1
5559
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
1746
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 application that uses the delegate with boosts signals, but i'm a big noob, and i don't how what to do. I think i need to create a .a library for using the signals, but how ?
3
2372
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 is supposed to update widget content. So if I want to wait for the thread to end in the event handler, I have 2 threads waiting on each other - with the one in the background waiting to access the widget content. My current workaround is to...
0
9089
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8983
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8941
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5910
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2406
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2037
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.