Dynamic PInvoke
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;
public class HelloWorld {
public static void Main() {
MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
}
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(int hWnd, String text, String caption, uint type);
}
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: https://www.dotnetinterop.com/faq/?q=LoadLibrary
2. Use Reflection.Emit. Example: https://www.msjogren.net/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;
using System;
public class TestClass
{
public static void Main(String[] args)
{
IntPtr user32 = LoadLibrary("user32.dll");
IntPtr procaddr = GetProcAddress(user32, "MessageBoxW");
MyMessageBox mbx = (MyMessageBox)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(MyMessageBox));
mbx(IntPtr.Zero, "Hello, World", "A Test Run", 0);
}
internal delegate int MyMessageBox(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]String text, [MarshalAs(UnmanagedType.LPWStr)]String Caption, int type);
[DllImport("kernel32.dll")]
internal static extern IntPtr LoadLibrary(String dllname);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetProcAddress(IntPtr hModule, String procname);
}
This example is tested in whidbey beta1.
Comments
Anonymous
July 14, 2004
Is there any way to specify the calling convention to use for the delegate? Or other options available with DllImport (PreserveSig, etc.)?Anonymous
July 14, 2004
Yes. Check out UnmanagedFunctionPointerAttribute (http://lab.msdn.microsoft.com/library/en-us/cpref/html/T_System_Runtime_InteropServices_UnmanagedFunctionPointerAttribute.asp)Anonymous
July 16, 2004
Nice, I am going to have to play around with this at home now! :-)Anonymous
November 11, 2004
PInvoke ?????? DLL ??? Load/Unload ????????????Anonymous
April 12, 2007
An unmanaged dll can be wrapped in a managed assembly by adding it as a file of a multi-module assembly.Anonymous
October 08, 2008
Calling function pointers from C#? Marshal.GetDelegateForFunctionPointer...Anonymous
January 18, 2009
PingBack from http://www.keyongtech.com/1194687-asp-net-and-dllimport