Yes, dynamic P / Invoke is possible in .NET using Marshal.GetDelegateForFunctionPointer . See the following example, taken from the Delegates and Unmanaged Function Pointers section of the article Writing C # 2.0 Unsafe Code by Patrick Smackey:
using System; using System.Runtime.InteropServices; class Program { internal delegate bool DelegBeep(uint iFreq, uint iDuration); [DllImport("kernel32.dll")] internal static extern IntPtr LoadLibrary(String dllname); [DllImport("kernel32.dll")] internal static extern IntPtr GetProcAddress(IntPtr hModule,String procName); static void Main() { IntPtr kernel32 = LoadLibrary( "Kernel32.dll" ); IntPtr procBeep = GetProcAddress( kernel32, "Beep" ); DelegBeep delegBeep = Marshal.GetDelegateForFunctionPointer(procBeep , typeof( DelegBeep ) ) as DelegBeep; delegBeep(100,100); } }
There is also another method described by Junfeng Zhang, which also works in .NET 1.1:
Dynamic PInvoke
source share