Dynamic PInvoke
14 Jul 2004 6:25 AM
CLR provides platform invoke mechanism to enable managed applications to call unmanaged APIs exported in a DLL. Here is a simple example: using System.Runtime.InteropServices; [DllImport("user32.dll", CharSet=CharSet.Auto)] CLR will search the dll in your assembly's directory first, then search the dll in directories listed in PATH environment variable. If the dll is not in any of those directories, you have to use so called Dynamic PInvoke technique. There are two known good dynamic PInvoke techniques in .Net framework 1.0 and 1.1: 1. Call LoadLibrary before you call the exported API. Example: http://www./faq/?q=LoadLibrary 2. Use Reflection.Emit. Example: http://www./dotnet/eng/samples/dotnet_dynpinvoke.asp Of course, the newest and greatest Whidbey (.Net framework 2.0) will save the world. It introduces a new mechanism for dynamic PInvoke --- Marshal.GetDelegateForFunctionPointer. Here is an example: using System.Runtime.InteropServices; public class TestClass internal delegate int MyMessageBox(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]String text, [MarshalAs(UnmanagedType.LPWStr)]String Caption, int type); [DllImport("kernel32.dll")] [DllImport("kernel32.dll")] This example is tested in whidbey beta1. |
|