Is it possible using reflection and C# .NET to call dynamically different function (with arguments) written in C or C++ before .NET came (unmanaged code) ?
A small C# example if possible would be appreciated!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, dynamic P/Invoke is possible in .NET in different ways.
LoadLibrary and Marshal.GetDelegateForFunctionPointer
Here’s an example using
Marshal.GetDelegateForFunctionPointertaken from the section Delegates and unmanaged function pointers from the article Writing C# 2.0 Unsafe Code by Patrick Smacchia, a very similar sample is also available in this old blog post by Junfeng ZhangReflection.Emit
This method work in all versions of .NET. It is described with an example in the documentation of
System.Reflection.Emit.ModuleBuilder.DefinePInvokeMethod