[Mono-dev] sigc++ signal C# wraper

buhochileno at gmail.com buhochileno at gmail.com
Tue Oct 30 16:33:14 EDT 2007


Thank for the help Jonathan... I follow your links and I learn a lote 
:-)...I fix the aliendetector class and the wrapper for the sigc++ 
signal, and the C++ shared library execute my managed method!!!, is so 
great learn new stuff, I'm realy happy thank you so much.
I only have now a little problem, but I'm sure that is my stupidity, I 
add to the C# example code that you send me a delegate and a event , to 
allow to other C# class to asociate  method to this event in a 
traditional .Net/mono way, something like this (only for test):
...
[DllImport ("/usr/local/lib/devel/libaliendetector.so")]
    private static extern void AlienDetector_AddSignalDetected (IntPtr 
detector, SignalDetectedHandler h);
...
public delegate void SignalDetectedHandlerM (); //I define this to not 
use the delegate used in the 
//DllImport...AlienDetector_AddSignalDetected...
    public static event SignalDetectedHandlerM SignalDetected;
...
and then in the "MyHandler" method (the method executed by the unmanaged 
code) I add this:
    public static void MyHandler ()
    {
        Console.WriteLine ("Managed Code Invoked!"); //this work prefectly
        Demo.SignalDetected(); //traditional .net/mono event trigger, 
dont' work
    }
...allways I get a:
Unhandled Exception: System.NullReferenceException: Object reference not 
set to an instance of an object

The porpuse of this is that later other class can do a traditional:
Demo.SignalDetected += new Demo.SignalDetectedHandlerM(MyOtherHandler);
...and the unmanaged code also triger the MyOtherHandler and use 
"MyHandler" only as a bringe or something like that.

do you see my stupid error?, there are is other better aproach??

thanks again..

Greetings
Mauricio

Jonathan Pryor wrote:
> On Mon, 2007-10-29 at 20:58 -0300, buhochileno at gmail.com wrote:
>   
>> About the sigc++ C# wraper, please be patient with me, I love 
>> programming and I really want to learn this kind of wraper (in other 
>> situation I write little wraper for common inpout/output function), and 
>> you must know that this is a very difcult subject to find 
>> documentation...
>>     
>
> I used:
>
> http://libsigc.sourceforge.net/libsigc2/docs/reference/html/group__ptr__fun.html
> http://libsigc.sourceforge.net/libsigc2/docs/manual/html/index.html
> http://www.mono-project.com/Dllimport
>
>   
>> and also recently I learn how manipulate signal/slot in 
>> C++ code (I know how to work and define Delegates and Events in C#).
>>
>> First I try to write your test code only to see if I can understad and 
>> compile the code, so I write the aliendetector.cc class like:
>> struct AlienDetector { sigc::signal<void> signal_detected;};
>> extern "C" void* CreateAlienDetector (){ return new AlienDetector ();}
>> void (*SignalDetectedHandler) ();
>>     
>
> As I said, this was untested.  The above *should* be:
>
> 	typedef void (*SignalDetectedHandler) ();
>
> Otherwise it declares the variable SignalDetectedHandler instead of
> declaring the _type_ SignalDetectedHandler.
>
>   
>> extern "C" void AlienDetector_AddSignalDetected (AlienDetector* 
>> detector, SignalDetectedHandler handler)
>> {detector->signal_detected.connect (sigc::ptr_fun(handler));
>> }
>>
>> And I add a little dummy "main" only to see if a traditional compiling 
>> command work:
>> g++ aliendetector.cc -o aliendetector `pkg-config --cflags --libs 
>> sigc++-2.0`
>> //I get...
>> aliendetector.cc:15: error: ‘SignalDetectedHandler’ is not a type
>> aliendetector.cc: In function ‘void 
>> AlienDetector_AddSignalDetected(AlienDetector*, int)’:
>> aliendetector.cc:17: error: no matching function for call to ‘ptr_fun(int&)’
>> The second error may be is related to the firs ‘SignalDetectedHandler’ 
>> is not a type, I try diferent stuff, but I don't know exactly what can I 
>> change...
>>     
>
> The second error is probably related to the first.
>
>   
>> Anyway and only to move forward in the building procees of a shared 
>> library, I comment the "void (*SignalDetectedHandler) ();" stuff and the 
>> compilation work, but I have some doubts about it:
>>
>> I use the following command to get the object code:
>> g++ -fpic -c aliendetector.cc -Wall -g `pkg-config --cflags --libs 
>> sigc++-2.0` //for the object code without main
>>     
>
> When using -c you don't need `pkg-config --libs`, just `pkg-config
> --cflags`.
>
>   
>> So later I do a :
>> g++ -o libaliendetector.so aliendetector.o -shared -Wl,-Bsymbolic -g 
>> -lsigc-2.0
>>     
>
> I think you'll need a `pkg-config --libs sigc++-2.0` here, instead of
> -lsigc-2.0.
>
>   
>> Also I want to show you a little of my work:
>> I write this little test header WebCamOrbit.h:
>> #include <sigc++/sigc++.h>
>> class WebCamOrbit
>> {public:
>> WebCamOrbit(); virtual ~WebCamOrbit();
>> virtual void SetPanTilt(int, int);
>> //signal accessor:
>> typedef sigc::signal<void, int, int> type_OnMove;
>> type_OnMove OnMove();
>>     
>
> OnMove should return `type_OnMove&' instead of `type_OnMove', so that
> later calls in "Consumer" classes like wcOrbit.OnMove().connect(...)
> don't make an extra copy of m_OnMove and instead modify the variable
> directly.
>
>   
>> this work like a charm in C++, and I need to write a wrapper for the 
>> signal exposed by the WebCamOrbit class, so I understend that I have to 
>> build a shared library with this class right?
>>     
>
> Not necessarily, but you should. :-)
>
>   
>> , and then in some how 
>> write the C# wrapper to allow to a C# programmer to access to this event 
>> in a traditional .NET/mono way (with delegates and events). Do you have 
>> any documentation?, sugestion?.
>>     
>
> See above URLs.
>
>  - Jon
>
>
>
>   




More information about the Mono-devel-list mailing list