I’m writing a .NET wrapper for an unmanaged DLL. The original DLL is C++ with a C shim that basically just replicates the API in C form, so that language bindings aren’t such a pain. I’ve already written a Python binding for it, so I know what I’m trying to do should work.
The DLL has a number of callbacks exported as global members, viz.:
__declspec(dllexport) extern int (*some_callback)(int32_t*, uint32_t);
I need to attach a managed function to this pointer, but I can’t figure out how to get at non-function resources in an unmanaged library. Unless I’m just blind, DllImport only imports functions.
Is there a C# way to do this, or do I need to write a little shim DLL in C that provides registration functions? I hate that approach, because it just feels inelegant, but if I have to, I have to.
You’re right, P/Invoke cannot handle data exports from a DLL. It is however technically possible by obtaining the export directly. This is very ugly and you’ll probably regret it some day, but this worked:
Sample C/C++ DLL:
Test C# code: