.net - Dynamic call to native library -


i trying invoke function native c++ dll, given name, arguments , return type. having problem make delegate marshalling.

i doing this:

object^ invike(type^ return_type, array<type^>^ types, array<object^>^ parameters) {  //demo data  return_type=long::typeid;  types = gcnew array < type^ > {string::typeid, int::typeid};  parameters = gcnew array < type^ > {"test",1};   //parameters initializated in class constructor  //intptr dll_file = native_dll::loadlibrary(dll_name);  //intptr init_function = native_dll::getprocaddress(dll_file, "_mcamgetnumberofcameras@0");   dynamicmethod^ function_prototype = gcnew dynamicmethod("test", return_type, types);   //argumentexception exception in line  delegate^ function_delegate = marshal::getdelegateforfunctionpointer(init_function, function_prototype->gettype());   object^ result = function_delegate->dynamicinvoke(parameters);   return result; } 

at line creating function_delegate have exception

an exception of type 'system.argumentexception' occurred in mscorlib.dll not handled in user code additional information: type must derive delegate. 

it possible want?

is other ways this?

i looking func<> template also, not understand how use it.

i did solve problem, making dynamic wrapper using assemblybuilder. here code piece.

assemblyname^ delegate_class_name = gcnew assemblyname("dynamic_class");  assemblybuilder^ delegate_class = appdomain::currentdomain->definedynamicassembly(delegate_class_name, assemblybuilderaccess::runandsave);  modulebuilder^ delegate_class_module = delegate_class->definedynamicmodule(delegate_class_name->name, delegate_class_name->name + ".dll");  typebuilder^ delegate_class_type = delegate_class_module->definetype("dynamic_class_type", typeattributes::public);  //a can add functions native dll need methodbuilder^ delegate_function = delegate_class_type->definepinvokemethod(         "_mcamgetnumberofcameras@0",         dll_name,         methodattributes::public | methodattributes::static | methodattributes::pinvokeimpl,         callingconventions::standard,         long::typeid,         type::emptytypes,         callingconvention::winapi,         charset::ansi);  delegate_function->setimplementationflags(delegate_function->getmethodimplementationflags() | methodimplattributes::preservesig);  type^ delegate_type = delegate_class_type->createtype();  methodinfo^ delegate_instace = delegate_type->getmethod("_mcamgetnumberofcameras@0");  //now can run it. int result= (int) delegate_instace->invoke(nullptr, nullptr); 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -