Is there a way to generalize the conversion of a managed ,NET delegate to a C++ function pointer?
I created this function template for the moment:
template<typename TManagedDelegate, typename TNativeFunc>
TNativeFunc CreateFunctionPointer(TManagedDelegate^ del)
{
using namespace System::Runtime::InteropServices;
GCHandle gch = GCHandle::Alloc(del);
IntPtr ip = Marshal::GetFunctionPointerForDelegate(del);
return static_cast<TNativeFunc>(ip.ToPointer());
}
but this requires the call site to explicitly indicate the types:
using NativeFuncionPointer = void(*)();
System::Action^ action;
NativeFuncionPointer fp = CreateFunctionPointer<System::Action, NativeFuncionPointer>(action);
My goal is to have a way to call:
System::Action^ action;
auto fp = CreateFunctionPointer(action); //type of 'fp' deduced to 'void(*)()'
Aucun commentaire:
Enregistrer un commentaire