I am writing a C# binding for an unmanaged C dll.
The dll provides 5 hooks to return several data:
typedef void (*t_libpd_printhook)(const char *recv);
and exports a field like:
EXTERN t_libpd_printhook libpd_printhook;
now i was using Interop Assistant to generate the binding code, which gave me just a delegate definition:
public delegate void t_libpd_printhook([In] [MarshalAs(UnmanagedType.LPStr)] string recv);
So is there some magic Interop function call i can use, to set the t_libpd_printhook field in the DLL?
You can use
LoadLibraryandGetProcAddressto obtain a pointer to the exportedlibpd_printhookvariable. You can then useMarshal.WriteIntPtrandMarshal.GetFunctionPointerForDelegateto assign to the delegate.You will want to add the error checking that I excised in the interests of a concise example.
Now, if you are in control of the unmanaged library I would still recommend adding a function to encapsulate writing to this function pointer. That feels like a better interface to me.