Tutorial by Examples

using System.Runtime.InteropServices; class PInvokeExample { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern uint MessageBox(IntPtr hWnd, String text, String caption, int options); public static void test() { MessageBox(IntPtr.Zero,...
Use pinvoke.net. Before declaring an extern Windows API function in your code, consider looking for it on pinvoke.net. They most likely already have a suitable declaration with all supporting types and good examples.
Arrays of simple type [DllImport("Example.dll")] static extern void SetArray( [MarshalAs(UnmanagedType.LPArray, SizeConst = 128)] byte[] data); Arrays of string [DllImport("Example.dll")] static extern void SetStrArray(string[] textLines);
Simple struct C++ signature: typedef struct _PERSON { int age; char name[32]; } PERSON, *LP_PERSON; void GetSpouse(PERSON person, LP_PERSON spouse); C# definition [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct PERSON { public int age; [Ma...
Value-type fields only C++ declaration typedef union { char c; int i; } CharOrInt; C# declaration [StructLayout(LayoutKind.Explicit)] public struct CharOrInt { [FieldOffset(0)] public byte c; [FieldOffset(0)] public int i; } Mixing value-type and reference...

Page 1 of 1