네이티브 DLL 호출

int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTStr lpCaption, UINT uType);
using System;
using System.Runtime.IneropServices;

class MsgBoxTest
{
  [DllImport("user32.dll")]
  static extern int MessageBox(IntPtr hWnd, string text, string caption, int type);

  public static void Main()
  {
    MessageBox(IntPtr.Zero, "Please do not press this again.", "Attention", 0);
  }
}

형식 인도

공통 형식의 인도

[DllImport("...")]
static extern int Foo([MarshalAs(UnmanagedType.LPStr)] string s);
using System;
using System.Text;
using System.Runtime.IneropServices;

class Test
{
  [DllImport("kernel32.dll")]
  static extern int GetWindowsDirectory(StringBuilder sb, int maxChars);

  public static void Main()
  {
    StringBuilder s = new StringBuider(256);
    GetWindowsDirectory(s, 256);
    Console.WriteLine(s);
  }
}

클래스와 구조체의 인도