How can i invoke a during compiletime unknown native function from C#

。_饼干妹妹 提交于 2019-12-12 14:34:21

问题


During runtime i am supplied with a String name, a sorted array of objects and some additional information about a native function (dllpath, calltype, etc).

lets assume all information was in a CallInformation Object.

How can i call this native function?

note: - i am not supplied with the dll before runtime. - i am not supplied with the call information before runtime. - i dont know the ammount or types of call parameter before runtime. - i dont know the returntype before runtime.

The usual information on the web about calling native functions deals with invoking

  • known functionstypes in known dlls
  • known functionstypes in unknown dlls

What i need is

  • unknown functionstypes in unknown dlls

回答1:


The solution would involve Reflection.Emit. You have to dynamically generate a class containing your PInvoke signature decorated with the appropriate DllImport attribute based on your CallInformation. Sorry that there is no code, but it can't be boiled down to something worthy to post, considering the verbose Reflection.Emit API.




回答2:


Make a delegate signature dynamically and call it using reflection.
Just look up how to create delegates dynamically, how to load a native function, how to call a function using reflection and connect those together.



来源:https://stackoverflow.com/questions/11850893/how-can-i-invoke-a-during-compiletime-unknown-native-function-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!