How to move data from unmanaged to managed code?

I use C ++ / CLI Wrapper to access a clean C ++ library (-> unmanaged) from the C # framework (-> managed). I want to create a mechanism that allows the C ++ library to display information about its status in a framework. In my understanding, this means that at some point I will have to call at least a managed function from unmanaged code. Is this possible and how can I achieve this?

Many thanks for your help!

Regards, Jakob

+4
source share
2 answers

Use the delegate so that unmanaged code invokes the managed method. Marshall :: GetFunctionPointerForDelegate () creates a stub that takes care of the transition, an instance method is supported. You can apply the return pointer to a function pointer that can be used by unmanaged code.

You will find the full sample code in this answer .

+7
source

I would recommend using an (managed) event for this. You could force your C ++ wrapper to call a method for your generated C ++ / CLI class that raises the event.

An event can be easily subscribed by C # and used as any other event in C #.

+2
source

Source: https://habr.com/ru/post/1342544/


All Articles