Unable to use managed events / objects in unmanaged error code c3265, c2811

The C ++ built-in library used in the C ++ / CLI project raises events giving me results,

  • If I try to handle the event by extending the unmanaged event, it says that the ref class can only extend the ref class.
  • Then I tried to create my own event, but an object was marked in it to collect the results, but I get that the error cannot declare a managed object in an unmanaged class.

Is there a way to do this one of the ways I'm trying, or to declare unmanaged result objects, populate them in an unmanaged event, and then Marshall?

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'm trying, I expanded my own event listener to capture an event, but not sure how to capture the results into a managed entity.

Edit2 This was detected when searching on the same line as suggested by @mcdave auto_gcroot

+4
source share
1 answer

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

Try expanding your code in 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 } }; 
+18
source

All Articles