P/Invoke, short for platform invocation services, allow you to access functions, structs, and callbacks in unmanaged DLLs.
Here, I’m able to import user32.dll, and then use a function inside user32.dll. Pretty cool, huh? Simply by naming a static method with the same name as the extern keyword, MessageBox, in our case, you can access functions within the DLL.
Compiled in Visual Studio 2017.
using System; using System.Runtime.InteropServices; class MessageBoxDLL { // call to which dll you would like to import // our unmanaged dll [DllImport("user32.dll")] // our function static extern int MessageBox(IntPtr hWnd, string text, string caption, int type); // a static method with the same name as the function, i.e. MessageBox public static void Main() { // calls function with relative parameters // the marshaler does the translation for us MessageBox(IntPtr.Zero,"This is a test!", "Yo", 0); } }
Notes