Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Can an unmanaged function pointer callback be exposed to C#?

$
0
0

Hi,

I have some very high frequency calls being made from a C# app into a C++ CLI wrapper around an unmanaged library. My current CLI wrapper exposes the callback as a managed delegate and then stores the callback using a gcroot object. The use of gcroot is resulting in a large number of very short lived GC handles being created and this is wreaking havoc with garbage collection.

I would like to define the managed method as follows and then call it from my C# application. I could use GetFunctionPointerForDelegate to create a single rooted instance of the callback that would be passed to the method repeatedly effectively eliminating the creation of the temporary GC handles.

#pragma unmanaged

typedef void (__stdcall * MyCallback)(const TriggerEvent* events, uint32_t count, void* context);

#pragma managed

public ref class Scene

{

       void Notify (System::UInt32 bodyId, MyCallback handler);

};

Is this possible?

If not, does anyone have a way to provide the callback without killing the GC with GC handles?


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>