tags:

views:

21

answers:

1

Native C++ library that I am using in C++/CLI project raises events giving me results,

  • If I try to handle the event by extending the unmanaged event, it says the ref class can only extend ref class.
  • I then tried to create a native event but have manged object inside it to collect the results, but I get the error cannot declare managed object in unmanaged class.

Is there anyway to get it done in one of the ways I am trying, or should I declare unmanaged result objects fill them in unmanaged event and then Marshall it ?

Edit:

class MyNativeListener: public NativeEventListener
{ 
private:
    ManagedResultsObject ^_results;
public:

void onEndProcessing(ProcessingEvent *event) 
{
    _results.Value = event->value;
      //Many more properties to capture

}

};

This is what I am trying, I have extended the native event listener to capture the event, but not sure how to capture the results to a managed object.

Edit2 Found this while searching on the same line as suggested by @mcdave auto_gcroot

+1  A: 

Your native class needs to store a handle to the managed object instead of a reference to it. You can do this using the gcroot template. If you dig into the gcroot template you will find it uses the GCHandle Structure, which with appropriate static casting can be stored as a void* pointer and so provides a means of storing managed references in native code.

Try expanding your code along the following lines:

#include <vcclr.h>

class MyNativeListener: public NativeEventListener
{ 
private:
    gcroot<ManagedResultsObject^> _results;
public:
    void onEndProcessing(ProcessingEvent *event) 
    {
        _results->Value = event->value;
        //Many more properties to capture
    }
};
mcdave