Archive for the ‘MSIL’ Category

  1. Hello, DirectX(MSIL) World!

    Posted on 7月 31st, 2012 by cx20

    Win32 DirectX(MSIL)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    .NET Framework 用の DirectX ライブラリとしては、DirectX SDK より入手可能な Managed DirectX がある。
    以下は MSIL における Managed DirectX アプリケーション の例となっている。

    ソースコード

     
    //  Microsoft (R) .NET Framework IL Disassembler.  Version 2.0.50727.42
    //  Copyright (c) Microsoft Corporation.  All rights reserved.
     
    // Metadata version: v2.0.50727
    .assembly extern System.Windows.Forms
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly extern Microsoft.DirectX.Direct3D
    {
      .publickeytoken = (31 BF 38 56 AD 36 4E 35 )                         // 1.8V.6N5
      .ver 1:0:2902:0
    }
    .assembly extern Microsoft.DirectX.Direct3DX
    {
      .publickeytoken = (31 BF 38 56 AD 36 4E 35 )                         // 1.8V.6N5
      .ver 1:0:2911:0
    }
    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly extern System.Drawing
    {
      .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
      .ver 2:0:0:0
    }
    .assembly extern System.Windows.Forms as System.Windows.Forms_5
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .hash = (46 CF F1 2A B1 D2 B5 94 71 B8 95 F2 3D 65 06 C2   // F..*....q...=e..
               33 D3 CF 05 )                                     // 3...
      .ver 1:0:5000:0
    }
    .assembly extern System.Drawing as System.Drawing_6
    {
      .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
      .hash = (01 F2 8E 4D 2E 2C 74 F0 83 0A 38 19 0C D6 04 59   // ...M.,t...8....Y
               7A 24 81 23 )                                     // z$.#
      .ver 1:0:5000:0
    }
    .assembly Hello
    {
      .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
      .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module Hello.exe
    // MVID: {5CADCB46-A918-449C-8A1F-169311ED3D80}
    .imagebase 0x00400000
    .file alignment 0x00000200
    .stackreserve 0x00100000
    .subsystem 0x0002       // WINDOWS_GUI
    .corflags 0x00000003    //  ILONLY 32BITREQUIRED
    // Image base: 0x02FA0000
     
     
    // =============== CLASS MEMBERS DECLARATION ===================
     
    .class public auto ansi beforefieldinit HelloForm
           extends [System.Windows.Forms]System.Windows.Forms.Form
    {
      .field private class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device device_
      .field private class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters presentParam_
      .field private class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font font_
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // コード サイズ       44 (0x2c)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
        IL_0006:  nop
        IL_0007:  nop
        IL_0008:  ldarg.0
        IL_0009:  ldc.i4     0x280
        IL_000e:  ldc.i4     0x1e0
        IL_0013:  newobj     instance void [System.Drawing]System.Drawing.Size::.ctor(int32,
                                                                                      int32)
        IL_0018:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::set_Size(valuetype [System.Drawing]System.Drawing.Size)
        IL_001d:  nop
        IL_001e:  ldarg.0
        IL_001f:  ldstr      "Hello, World!"
        IL_0024:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
        IL_0029:  nop
        IL_002a:  nop
        IL_002b:  ret
      } // end of method HelloForm::.ctor
     
      .method public hidebysig instance bool 
              InitD3D() cil managed
      {
        // コード サイズ       84 (0x54)
        .maxstack  8
        .locals init (bool V_0,
                 class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters[] V_1)
        IL_0000:  nop
        IL_0001:  ldarg.0
        IL_0002:  newobj     instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters::.ctor()
        IL_0007:  stfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_000c:  ldarg.0
        IL_000d:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_0012:  ldc.i4.1
        IL_0013:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters::set_Windowed(bool)
        IL_0018:  nop
        IL_0019:  ldarg.0
        IL_001a:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_001f:  ldc.i4.1
        IL_0020:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters::set_SwapEffect(valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.SwapEffect)
        IL_0025:  nop
        IL_0026:  ldarg.0
        IL_0027:  ldc.i4.0
        IL_0028:  ldc.i4.1
        IL_0029:  ldarg.0
        IL_002a:  ldc.i4.s   64
        IL_002c:  ldc.i4.1
        IL_002d:  newarr     [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters
        IL_0032:  stloc.1
        IL_0033:  ldloc.1
        IL_0034:  ldc.i4.0
        IL_0035:  ldarg.0
        IL_0036:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_003b:  stelem.ref
        IL_003c:  ldloc.1
        IL_003d:  newobj     instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::.ctor(int32,
                                                                                                                valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.DeviceType,
                                                                                                                class [System.Windows.Forms_5]System.Windows.Forms.Control,
                                                                                                                valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.CreateFlags,
                                                                                                                class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters[])
        IL_0042:  stfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0047:  ldarg.0
        IL_0048:  call       instance void HelloForm::InitFont()
        IL_004d:  nop
        IL_004e:  ldc.i4.1
        IL_004f:  stloc.0
        IL_0050:  br.s       IL_0052
     
        IL_0052:  ldloc.0
        IL_0053:  ret
      } // end of method HelloForm::InitD3D
     
      .method private hidebysig instance void 
              InitFont() cil managed
      {
        // コード サイズ       51 (0x33)
        .maxstack  4
        .locals init (valuetype [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription V_0)
        IL_0000:  nop
        IL_0001:  ldloca.s   V_0
        IL_0003:  call       instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription::.ctor()
        IL_0008:  nop
        IL_0009:  ldloca.s   V_0
        IL_000b:  ldc.i4.s   16
        IL_000d:  call       instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription::set_Height(int32)
        IL_0012:  nop
        IL_0013:  ldloca.s   V_0
        IL_0015:  ldstr      bytearray (2D FF 33 FF 20 00 B4 30 B7 30 C3 30 AF 30 )       // -.3. ..0.0.0.0
        IL_001a:  call       instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription::set_FaceName(string)
        IL_001f:  nop
        IL_0020:  ldarg.0
        IL_0021:  ldarg.0
        IL_0022:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0027:  ldloc.0
        IL_0028:  newobj     instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font::.ctor(class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device,
                                                                                                               valuetype [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription)
        IL_002d:  stfld      class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font HelloForm::font_
        IL_0032:  ret
      } // end of method HelloForm::InitFont
     
      .method public hidebysig instance void 
              Render() cil managed
      {
        // コード サイズ       111 (0x6f)
        .maxstack  6
        .locals init (bool V_0)
        IL_0000:  nop
        IL_0001:  ldarg.0
        IL_0002:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0007:  ldnull
        IL_0008:  call       bool [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::op_Equality(class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device,
                                                                                                             class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device)
        IL_000d:  ldc.i4.0
        IL_000e:  ceq
        IL_0010:  stloc.0
        IL_0011:  ldloc.0
        IL_0012:  brtrue.s   IL_0017
     
        IL_0014:  nop
        IL_0015:  br.s       IL_006e
     
        IL_0017:  ldarg.0
        IL_0018:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_001d:  ldc.i4.1
        IL_001e:  call       valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_Blue()
        IL_0023:  ldc.r4     1.
        IL_0028:  ldc.i4.0
        IL_0029:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::Clear(valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.ClearFlags,
                                                                                                                valuetype [System.Drawing_6]System.Drawing.Color,
                                                                                                                float32,
                                                                                                                int32)
        IL_002e:  nop
        IL_002f:  ldarg.0
        IL_0030:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0035:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::BeginScene()
        IL_003a:  nop
        IL_003b:  ldarg.0
        IL_003c:  ldfld      class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font HelloForm::font_
        IL_0041:  ldnull
        IL_0042:  ldstr      "Hello, DirectX(MSIL) World!"
        IL_0047:  ldc.i4.s   10
        IL_0049:  ldc.i4.s   10
        IL_004b:  call       valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_White()
        IL_0050:  callvirt   instance int32 [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font::DrawText(class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Sprite,
                                                                                                                   string,
                                                                                                                   int32,
                                                                                                                   int32,
                                                                                                                   valuetype [System.Drawing_6]System.Drawing.Color)
        IL_0055:  pop
        IL_0056:  ldarg.0
        IL_0057:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_005c:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::EndScene()
        IL_0061:  nop
        IL_0062:  ldarg.0
        IL_0063:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0068:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::Present()
        IL_006d:  nop
        IL_006e:  ret
      } // end of method HelloForm::Render
     
      .method public hidebysig static void  Main() cil managed
      {
        .entrypoint
        .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
        // コード サイズ       70 (0x46)
        .maxstack  2
        .locals init (class HelloForm V_0,
                 bool V_1)
        IL_0000:  nop
        IL_0001:  newobj     instance void HelloForm::.ctor()
        IL_0006:  stloc.0
        .try
        {
          IL_0007:  nop
          IL_0008:  ldloc.0
          IL_0009:  callvirt   instance bool HelloForm::InitD3D()
          IL_000e:  pop
          IL_000f:  ldloc.0
          IL_0010:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::Show()
          IL_0015:  nop
          IL_0016:  br.s       IL_0027
     
          IL_0018:  nop
          IL_0019:  ldloc.0
          IL_001a:  callvirt   instance void HelloForm::Render()
          IL_001f:  nop
          IL_0020:  call       void [System.Windows.Forms]System.Windows.Forms.Application::DoEvents()
          IL_0025:  nop
          IL_0026:  nop
          IL_0027:  ldloc.0
          IL_0028:  callvirt   instance bool [System.Windows.Forms]System.Windows.Forms.Control::get_Created()
          IL_002d:  stloc.1
          IL_002e:  ldloc.1
          IL_002f:  brtrue.s   IL_0018
     
          IL_0031:  nop
          IL_0032:  leave.s    IL_0044
     
        }  // end .try
        finally
        {
          IL_0034:  ldloc.0
          IL_0035:  ldnull
          IL_0036:  ceq
          IL_0038:  stloc.1
          IL_0039:  ldloc.1
          IL_003a:  brtrue.s   IL_0043
     
          IL_003c:  ldloc.0
          IL_003d:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
          IL_0042:  nop
          IL_0043:  endfinally
        }  // end handler
        IL_0044:  nop
        IL_0045:  ret
      } // end of method HelloForm::Main
     
    } // end of class HelloForm
     
     
    // =============================================================
     
    // *********** 逆アセンブルが完了しました ***********************
    // 警告: Win32 リソース ファイル Hello.res を作成しました。

    上記コードは、以下の C# の実行ファイルを ildasm.exe で逆アセンブルしたものである。

    ソースコード

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
     
    public class HelloForm : Form
    {
        private Device device_;
        private PresentParameters presentParam_;
        private Microsoft.DirectX.Direct3D.Font font_;
     
        public HelloForm()
        {
            this.Size = new Size(640, 480);
            this.Text = "Hello, World!";
        }
     
        public bool InitD3D()
        {
            presentParam_ = new PresentParameters();
     
            presentParam_.Windowed = true;
            presentParam_.SwapEffect = SwapEffect.Discard;
     
            device_ = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParam_);
     
            InitFont();
     
            return true;
        }
     
        private void InitFont()
        {
            FontDescription fd = new FontDescription();
     
            fd.Height = 16;
            fd.FaceName = "MS ゴシック";
     
            font_ = new Microsoft.DirectX.Direct3D.Font(device_, fd);
        }
     
        public void Render()
        {
            if (device_ == null)
            {
                return;
            }
     
            device_.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
            device_.BeginScene();
     
            font_.DrawText(null, "Hello, DirectX(MSIL) World!", 10, 10, Color.White);
     
            device_.EndScene();
            device_.Present();
        }
     
        [STAThread]
        public static void Main()
        {
            using (HelloForm form = new HelloForm())
            {
                form.InitD3D();
                form.Show();
     
                while (form.Created)
                {
                    form.Render();
                    Application.DoEvents();
                }
            }
        }
    }

    コンパイル方法

    C:¥> ilasm Hello.il

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(MSIL) World!               |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, Win32 GUI(MSIL) World!

    Posted on 7月 11th, 2012 by cx20

    Win32 GUI(MSIL)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は MSIL において Win32 API を使用した GUI アプリケーション の例となっている。

    ソースコード

    //  Microsoft (R) .NET Framework IL Disassembler.  Version 4.0.30319.1
    //  Copyright (c) Microsoft Corporation. All rights reserved.
     
    // Metadata version: v2.0.50727
    .module extern user32.dll
    .module extern gdi32.dll
    .module extern kernel32.dll
    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly Hello
    {
      .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
      .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module Hello.exe
    // MVID: {BEAF0F56-F7B6-4F51-B26E-C0D153A452A2}
    .imagebase 0x00400000
    .file alignment 0x00000200
    .stackreserve 0x00100000
    .subsystem 0x0002       // WINDOWS_GUI
    .corflags 0x00000001    //  ILONLY
    // Image base: 0x00410000
     
     
    // =============== CLASS MEMBERS DECLARATION ===================
     
    .class private auto ansi beforefieldinit Hello
           extends [mscorlib]System.Object
    {
      .class sequential ansi sealed nested private beforefieldinit POINT
             extends [mscorlib]System.ValueType
      {
        .field public int32 x
        .field public int32 y
      } // end of class POINT
     
      .class sequential ansi sealed nested private beforefieldinit MSG
             extends [mscorlib]System.ValueType
      {
        .field public native int hwnd
        .field public uint32 message
        .field public native int wParam
        .field public native int lParam
        .field public uint32 time
        .field public valuetype Hello/POINT pt
      } // end of class MSG
     
      .class auto ansi sealed nested private WndProcDelgate
             extends [mscorlib]System.MulticastDelegate
      {
        .method public hidebysig specialname rtspecialname 
                instance void  .ctor(object 'object',
                                     native int 'method') runtime managed
        {
        } // end of method WndProcDelgate::.ctor
     
        .method public hidebysig newslot virtual 
                instance native int  Invoke(native int hWnd,
                                            uint32 uMsg,
                                            native int wParam,
                                            native int lParam) runtime managed
        {
        } // end of method WndProcDelgate::Invoke
     
        .method public hidebysig newslot virtual 
                instance class [mscorlib]System.IAsyncResult 
                BeginInvoke(native int hWnd,
                            uint32 uMsg,
                            native int wParam,
                            native int lParam,
                            class [mscorlib]System.AsyncCallback callback,
                            object 'object') runtime managed
        {
        } // end of method WndProcDelgate::BeginInvoke
     
        .method public hidebysig newslot virtual 
                instance native int  EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
        {
        } // end of method WndProcDelgate::EndInvoke
     
      } // end of class WndProcDelgate
     
      .class sequential autochar sealed nested private beforefieldinit WNDCLASSEX
             extends [mscorlib]System.ValueType
      {
        .field public uint32 cbSize
        .field public uint32 style
        .field public class Hello/WndProcDelgate lpfnWndProc
        .field public int32 cbClsExtra
        .field public int32 cbWndExtra
        .field public native int hInstance
        .field public native int hIcon
        .field public native int hCursor
        .field public native int hbrBackground
        .field public string lpszMenuName
        .field public string lpszClassName
        .field public native int hIconSm
      } // end of class WNDCLASSEX
     
      .class sequential ansi sealed nested private beforefieldinit RECT
             extends [mscorlib]System.ValueType
      {
        .field public int32 Left
        .field public int32 Top
        .field public int32 Right
        .field public int32 Bottom
      } // end of class RECT
     
      .class sequential ansi sealed nested private beforefieldinit PAINTSTRUCT
             extends [mscorlib]System.ValueType
      {
        .field public native int hdc
        .field public int32 fErase
        .field public valuetype Hello/RECT rcPaint
        .field public int32 fRestore
        .field public int32 fIncUpdate
        .field public  marshal( fixed array [32]) uint8[] rgbReserved
      } // end of class PAINTSTRUCT
     
      .field private static literal uint32 WS_OVERLAPPED = uint32(0x00000000)
      .field private static literal uint32 WS_POPUP = uint32(0x80000000)
      .field private static literal uint32 WS_CHILD = uint32(0x40000000)
      .field private static literal uint32 WS_MINIMIZE = uint32(0x20000000)
      .field private static literal uint32 WS_VISIBLE = uint32(0x10000000)
      .field private static literal uint32 WS_DISABLED = uint32(0x08000000)
      .field private static literal uint32 WS_CLIPSIBLINGS = uint32(0x04000000)
      .field private static literal uint32 WS_CLIPCHILDREN = uint32(0x02000000)
      .field private static literal uint32 WS_MAXIMIZE = uint32(0x01000000)
      .field private static literal uint32 WS_CAPTION = uint32(0x00C00000)
      .field private static literal uint32 WS_BORDER = uint32(0x00800000)
      .field private static literal uint32 WS_DLGFRAME = uint32(0x00400000)
      .field private static literal uint32 WS_VSCROLL = uint32(0x00200000)
      .field private static literal uint32 WS_HSCROLL = uint32(0x00100000)
      .field private static literal uint32 WS_SYSMENU = uint32(0x00080000)
      .field private static literal uint32 WS_THICKFRAME = uint32(0x00040000)
      .field private static literal uint32 WS_GROUP = uint32(0x00020000)
      .field private static literal uint32 WS_TABSTOP = uint32(0x00010000)
      .field private static literal uint32 WS_MINIMIZEBOX = uint32(0x00020000)
      .field private static literal uint32 WS_MAXIMIZEBOX = uint32(0x00010000)
      .field private static literal uint32 WS_TILED = uint32(0x00000000)
      .field private static literal uint32 WS_ICONIC = uint32(0x20000000)
      .field private static literal uint32 WS_SIZEBOX = uint32(0x00040000)
      .field private static literal uint32 WS_TILEDWINDOW = uint32(0x00CF0000)
      .field private static literal uint32 WS_OVERLAPPEDWINDOW = uint32(0x00CF0000)
      .field private static literal uint32 WS_POPUPWINDOW = uint32(0x80880000)
      .field private static literal uint32 WS_CHILDWINDOW = uint32(0x40000000)
      .field private static literal uint32 WM_CREATE = uint32(0x00000001)
      .field private static literal uint32 WM_DESTROY = uint32(0x00000002)
      .field private static literal uint32 WM_PAINT = uint32(0x0000000F)
      .field private static literal uint32 WM_CLOSE = uint32(0x00000010)
      .field private static literal uint32 WM_COMMAND = uint32(0x00000111)
      .field private static literal uint32 COLOR_WINDOW = uint32(0x00000005)
      .field private static literal uint32 COLOR_BTNFACE = uint32(0x0000000F)
      .field private static literal uint32 CS_VREDRAW = uint32(0x00000001)
      .field private static literal uint32 CS_HREDRAW = uint32(0x00000002)
      .field private static literal int32 CW_USEDEFAULT = int32(0x80000000)
      .field private static literal uint32 SW_SHOWDEFAULT = uint32(0x0000000A)
      .field private static literal int32 IDI_APPLICATION = int32(0x00007F00)
      .field private static literal int32 IDC_ARROW = int32(0x00007F00)
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  LoadCursor(native int hInstance,
                                     native int lpCursorName) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  LoadIcon(native int hInstance,
                                   native int lpIconName) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              int16  RegisterClassEx(valuetype Hello/WNDCLASSEX& pcWndClassEx) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  CreateWindowEx(uint32 dwExStyle,
                                         string lpClassName,
                                         string lpWindowName,
                                         uint32 dwStyle,
                                         int32 x,
                                         int32 y,
                                         int32 nWidth,
                                         int32 nHeight,
                                         native int hWndParent,
                                         native int hMenu,
                                         native int hInstance,
                                         native int lpParam) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              bool  ShowWindow(native int hWnd,
                               uint32 nCmdShow) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              bool  UpdateWindow(native int hWnd) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              int32  GetMessage([out] valuetype Hello/MSG& lpMsg,
                                native int hWnd,
                                uint32 wMsgFilterMin,
                                uint32 wMsgFilterMax) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              int32  TranslateMessage([in] valuetype Hello/MSG& lpMsg) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  DispatchMessage([in] valuetype Hello/MSG& lpMsg) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              void  PostQuitMessage(int32 nExitCode) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  DefWindowProc(native int hWnd,
                                        uint32 uMsg,
                                        native int wParam,
                                        native int lParam) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  BeginPaint(native int hWnd,
                                     [out] valuetype Hello/PAINTSTRUCT& lpPaint) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) 
              native int  EndPaint(native int hWnd,
                                   valuetype Hello/PAINTSTRUCT& lpPaint) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("gdi32.dll" autochar winapi) 
              native int  TextOut(native int hdc,
                                  int32 x,
                                  int32 y,
                                  string lpString,
                                  int32 nCount) cil managed preservesig
      {
      }
      .method private hidebysig static pinvokeimpl("kernel32.dll" autochar winapi) 
              native int  GetModuleHandle(string lpModuleName) cil managed preservesig
      {
      }
      .method private hidebysig static native int 
              WndProc(native int hWnd,
                      uint32 uMsg,
                      native int wParam,
                      native int lParam) cil managed
      {
        // コード サイズ       98 (0x62)
        .maxstack  5
        .locals init (valuetype Hello/PAINTSTRUCT V_0,
                 native int V_1,
                 string V_2,
                 native int V_3,
                 uint32 V_4)
        IL_0000:  nop
        IL_0001:  ldloca.s   V_0
        IL_0003:  initobj    Hello/PAINTSTRUCT
        IL_0009:  ldstr      "Hello, Win32 GUI(MSIL) World!"
        IL_000e:  stloc.2
        IL_000f:  ldarg.1
        IL_0010:  stloc.s    V_4
        IL_0012:  ldloc.s    V_4
        IL_0014:  ldc.i4.2
        IL_0015:  beq.s      IL_0043
     
        IL_0017:  ldloc.s    V_4
        IL_0019:  ldc.i4.s   15
        IL_001b:  beq.s      IL_001f
     
        IL_001d:  br.s       IL_004c
     
        IL_001f:  ldarg.0
        IL_0020:  ldloca.s   V_0
        IL_0022:  call       native int Hello::BeginPaint(native int,
                                                          valuetype Hello/PAINTSTRUCT&)
        IL_0027:  stloc.1
        IL_0028:  ldloc.1
        IL_0029:  ldc.i4.0
        IL_002a:  ldc.i4.0
        IL_002b:  ldloc.2
        IL_002c:  ldloc.2
        IL_002d:  callvirt   instance int32 [mscorlib]System.String::get_Length()
        IL_0032:  call       native int Hello::TextOut(native int,
                                                       int32,
                                                       int32,
                                                       string,
                                                       int32)
        IL_0037:  pop
        IL_0038:  ldarg.0
        IL_0039:  ldloca.s   V_0
        IL_003b:  call       native int Hello::EndPaint(native int,
                                                        valuetype Hello/PAINTSTRUCT&)
        IL_0040:  pop
        IL_0041:  br.s       IL_0058
     
        IL_0043:  ldc.i4.0
        IL_0044:  call       void Hello::PostQuitMessage(int32)
        IL_0049:  nop
        IL_004a:  br.s       IL_0058
     
        IL_004c:  ldarg.0
        IL_004d:  ldarg.1
        IL_004e:  ldarg.2
        IL_004f:  ldarg.3
        IL_0050:  call       native int Hello::DefWindowProc(native int,
                                                             uint32,
                                                             native int,
                                                             native int)
        IL_0055:  stloc.3
        IL_0056:  br.s       IL_0060
     
        IL_0058:  ldsfld     native int [mscorlib]System.IntPtr::Zero
        IL_005d:  stloc.3
        IL_005e:  br.s       IL_0060
     
        IL_0060:  ldloc.3
        IL_0061:  ret
      } // end of method Hello::WndProc
     
      .method private hidebysig static int32 
              WinMain(string[] args) cil managed
      {
        // コード サイズ       358 (0x166)
        .maxstack  12
        .locals init (native int V_0,
                 valuetype Hello/WNDCLASSEX V_1,
                 native int V_2,
                 valuetype Hello/MSG V_3,
                 int32 V_4,
                 bool V_5)
        IL_0000:  nop
        IL_0001:  ldtoken    Hello
        IL_0006:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
        IL_000b:  callvirt   instance class [mscorlib]System.Reflection.Module [mscorlib]System.Type::get_Module()
        IL_0010:  call       native int [mscorlib]System.Runtime.InteropServices.Marshal::GetHINSTANCE(class [mscorlib]System.Reflection.Module)
        IL_0015:  stloc.0
        IL_0016:  ldloca.s   V_1
        IL_0018:  initobj    Hello/WNDCLASSEX
        IL_001e:  ldloca.s   V_1
        IL_0020:  ldloc.1
        IL_0021:  box        Hello/WNDCLASSEX
        IL_0026:  call       int32 [mscorlib]System.Runtime.InteropServices.Marshal::SizeOf(object)
        IL_002b:  stfld      uint32 Hello/WNDCLASSEX::cbSize
        IL_0030:  ldloca.s   V_1
        IL_0032:  ldc.i4.3
        IL_0033:  stfld      uint32 Hello/WNDCLASSEX::style
        IL_0038:  ldloca.s   V_1
        IL_003a:  ldnull
        IL_003b:  ldftn      native int Hello::WndProc(native int,
                                                       uint32,
                                                       native int,
                                                       native int)
        IL_0041:  newobj     instance void Hello/WndProcDelgate::.ctor(object,
                                                                       native int)
        IL_0046:  stfld      class Hello/WndProcDelgate Hello/WNDCLASSEX::lpfnWndProc
        IL_004b:  ldloca.s   V_1
        IL_004d:  ldc.i4.0
        IL_004e:  stfld      int32 Hello/WNDCLASSEX::cbClsExtra
        IL_0053:  ldloca.s   V_1
        IL_0055:  ldc.i4.0
        IL_0056:  stfld      int32 Hello/WNDCLASSEX::cbWndExtra
        IL_005b:  ldloca.s   V_1
        IL_005d:  ldloc.0
        IL_005e:  stfld      native int Hello/WNDCLASSEX::hInstance
        IL_0063:  ldloca.s   V_1
        IL_0065:  ldloc.0
        IL_0066:  ldc.i4     0x7f00
        IL_006b:  newobj     instance void [mscorlib]System.IntPtr::.ctor(int32)
        IL_0070:  call       native int Hello::LoadIcon(native int,
                                                        native int)
        IL_0075:  stfld      native int Hello/WNDCLASSEX::hIcon
        IL_007a:  ldloca.s   V_1
        IL_007c:  ldloc.0
        IL_007d:  ldc.i4     0x7f00
        IL_0082:  newobj     instance void [mscorlib]System.IntPtr::.ctor(int32)
        IL_0087:  call       native int Hello::LoadIcon(native int,
                                                        native int)
        IL_008c:  stfld      native int Hello/WNDCLASSEX::hCursor
        IL_0091:  ldloca.s   V_1
        IL_0093:  ldc.i4.6
        IL_0094:  conv.i8
        IL_0095:  newobj     instance void [mscorlib]System.IntPtr::.ctor(int64)
        IL_009a:  stfld      native int Hello/WNDCLASSEX::hbrBackground
        IL_009f:  ldloca.s   V_1
        IL_00a1:  ldstr      ""
        IL_00a6:  stfld      string Hello/WNDCLASSEX::lpszMenuName
        IL_00ab:  ldloca.s   V_1
        IL_00ad:  ldstr      "helloWindow"
        IL_00b2:  stfld      string Hello/WNDCLASSEX::lpszClassName
        IL_00b7:  ldloca.s   V_1
        IL_00b9:  ldsfld     native int [mscorlib]System.IntPtr::Zero
        IL_00be:  stfld      native int Hello/WNDCLASSEX::hIconSm
        IL_00c3:  ldloca.s   V_1
        IL_00c5:  call       int16 Hello::RegisterClassEx(valuetype Hello/WNDCLASSEX&)
        IL_00ca:  pop
        IL_00cb:  ldc.i4.0
        IL_00cc:  ldloca.s   V_1
        IL_00ce:  ldfld      string Hello/WNDCLASSEX::lpszClassName
        IL_00d3:  ldstr      "Hello, World!"
        IL_00d8:  ldc.i4     0xcf0000
        IL_00dd:  ldc.i4     0x80000000
        IL_00e2:  ldc.i4     0x80000000
        IL_00e7:  ldc.i4     0x280
        IL_00ec:  ldc.i4     0x1e0
        IL_00f1:  ldsfld     native int [mscorlib]System.IntPtr::Zero
        IL_00f6:  ldsfld     native int [mscorlib]System.IntPtr::Zero
        IL_00fb:  ldloca.s   V_1
        IL_00fd:  ldfld      native int Hello/WNDCLASSEX::hInstance
        IL_0102:  ldsfld     native int [mscorlib]System.IntPtr::Zero
        IL_0107:  call       native int Hello::CreateWindowEx(uint32,
                                                              string,
                                                              string,
                                                              uint32,
                                                              int32,
                                                              int32,
                                                              int32,
                                                              int32,
                                                              native int,
                                                              native int,
                                                              native int,
                                                              native int)
        IL_010c:  stloc.2
        IL_010d:  ldloc.2
        IL_010e:  ldc.i4.s   10
        IL_0110:  call       bool Hello::ShowWindow(native int,
                                                    uint32)
        IL_0115:  pop
        IL_0116:  ldloc.2
        IL_0117:  call       bool Hello::UpdateWindow(native int)
        IL_011c:  pop
        IL_011d:  ldloca.s   V_3
        IL_011f:  initobj    Hello/MSG
        IL_0125:  br.s       IL_0139
     
        IL_0127:  nop
        IL_0128:  ldloca.s   V_3
        IL_012a:  call       int32 Hello::TranslateMessage(valuetype Hello/MSG&)
        IL_012f:  pop
        IL_0130:  ldloca.s   V_3
        IL_0132:  call       native int Hello::DispatchMessage(valuetype Hello/MSG&)
        IL_0137:  pop
        IL_0138:  nop
        IL_0139:  ldloca.s   V_3
        IL_013b:  ldsfld     native int [mscorlib]System.IntPtr::Zero
        IL_0140:  ldc.i4.0
        IL_0141:  ldc.i4.0
        IL_0142:  call       int32 Hello::GetMessage(valuetype Hello/MSG&,
                                                     native int,
                                                     uint32,
                                                     uint32)
        IL_0147:  ldc.i4.0
        IL_0148:  ceq
        IL_014a:  ldc.i4.0
        IL_014b:  ceq
        IL_014d:  stloc.s    V_5
        IL_014f:  ldloc.s    V_5
        IL_0151:  brtrue.s   IL_0127
     
        IL_0153:  ldloca.s   V_3
        IL_0155:  ldfld      native int Hello/MSG::wParam
        IL_015a:  call       int32 [mscorlib]System.IntPtr::op_Explicit(native int)
        IL_015f:  stloc.s    V_4
        IL_0161:  br.s       IL_0163
     
        IL_0163:  ldloc.s    V_4
        IL_0165:  ret
      } // end of method Hello::WinMain
     
      .method private hidebysig static int32 
              Main(string[] args) cil managed
      {
        .entrypoint
        .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
        // コード サイズ       12 (0xc)
        .maxstack  1
        .locals init (int32 V_0)
        IL_0000:  nop
        IL_0001:  ldarg.0
        IL_0002:  call       int32 Hello::WinMain(string[])
        IL_0007:  stloc.0
        IL_0008:  br.s       IL_000a
     
        IL_000a:  ldloc.0
        IL_000b:  ret
      } // end of method Hello::Main
     
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // コード サイズ       7 (0x7)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
        IL_0006:  ret
      } // end of method Hello::.ctor
     
    } // end of class Hello
     
    // =============================================================
     
    // *********** 逆アセンブルが完了しました ***********************
    // 警告: Win32 リソース ファイル Hello.res を作成しました。

    上記コードは、以下の C# の実行ファイルを ildadm.exe で逆アセンブルしたものである。

    ソースコード

    using System;
    using System.Runtime.InteropServices;
     
    class Hello
    {
        [StructLayout(LayoutKind.Sequential)]
        struct POINT
        {
            public int x;
            public int y;
        }
     
        [StructLayout(LayoutKind.Sequential)]
        struct MSG
        {
            public IntPtr hwnd;
            public uint message;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public POINT pt;
        }
     
        delegate IntPtr WndProcDelgate(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct WNDCLASSEX
        {
            public uint cbSize;
            public uint style;
            public WndProcDelgate lpfnWndProc;
            public Int32 cbClsExtra;
            public Int32 cbWndExtra;
            public IntPtr hInstance;
            public IntPtr hIcon;
            public IntPtr hCursor;
            public IntPtr hbrBackground;
            public string lpszMenuName;
            public string lpszClassName;
            public IntPtr hIconSm;
        }
     
        [StructLayout(LayoutKind.Sequential)]
        struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
     
        [StructLayout(LayoutKind.Sequential)]
        struct PAINTSTRUCT
        {
            public IntPtr hdc;
            public int fErase;
            public RECT rcPaint;
            public int fRestore;
            public int fIncUpdate;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public byte[] rgbReserved;
        }
     
        const uint WS_OVERLAPPED = 0x00000000;
        const uint WS_POPUP = 0x80000000;
        const uint WS_CHILD = 0x40000000;
        const uint WS_MINIMIZE = 0x20000000;
        const uint WS_VISIBLE = 0x10000000;
        const uint WS_DISABLED = 0x08000000;
        const uint WS_CLIPSIBLINGS = 0x04000000;
        const uint WS_CLIPCHILDREN = 0x02000000;
        const uint WS_MAXIMIZE = 0x01000000;
        const uint WS_CAPTION = 0x00C00000; // WS_BORDER | WS_DLGFRAME
        const uint WS_BORDER = 0x00800000;
        const uint WS_DLGFRAME = 0x00400000;
        const uint WS_VSCROLL = 0x00200000;
        const uint WS_HSCROLL = 0x00100000;
        const uint WS_SYSMENU = 0x00080000;
        const uint WS_THICKFRAME = 0x00040000;
        const uint WS_GROUP = 0x00020000;
        const uint WS_TABSTOP = 0x00010000;
     
        const uint WS_MINIMIZEBOX = 0x00020000;
        const uint WS_MAXIMIZEBOX = 0x00010000;
     
        const uint WS_TILED = WS_OVERLAPPED;
        const uint WS_ICONIC = WS_MINIMIZE;
        const uint WS_SIZEBOX = WS_THICKFRAME;
        const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW;
     
        const uint WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
        const uint WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU;
        const uint WS_CHILDWINDOW = WS_CHILD;
     
        const uint WM_CREATE = 0x0001;
        const uint WM_DESTROY = 0x0002;
        const uint WM_PAINT = 0x000F;
        const uint WM_CLOSE = 0x0010;
        const uint WM_COMMAND = 0x0111;
     
        const uint COLOR_WINDOW = 5;
        const uint COLOR_BTNFACE = 15;
     
        const uint CS_VREDRAW = 0x0001;
        const uint CS_HREDRAW = 0x0002;
     
        const int CW_USEDEFAULT = -2147483648; // ((uint)0x80000000)
     
        const uint SW_SHOWDEFAULT = 10;
     
        const int IDI_APPLICATION = 32512;
        const int IDC_ARROW = 32512;
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern short RegisterClassEx(ref WNDCLASSEX pcWndClassEx);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool UpdateWindow(IntPtr hWnd);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int TranslateMessage([In] ref MSG lpMsg);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr DispatchMessage([In] ref MSG lpMsg);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern void PostQuitMessage(int nExitCode);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr BeginPaint(IntPtr hWnd, out PAINTSTRUCT lpPaint);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
     
        [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr TextOut( IntPtr hdc, int x, int y, string lpString, int nCount );
     
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr GetModuleHandle(string lpModuleName);
     
        static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
        {
            PAINTSTRUCT ps = new PAINTSTRUCT();
            IntPtr hdc;
            string strMessage = "Hello, Win32 GUI(MSIL) World!";
     
            switch (uMsg)
            {
                case WM_PAINT:
                    hdc = BeginPaint( hWnd, out ps );
                    TextOut( hdc, 0, 0, strMessage, strMessage.Length );
                    EndPaint( hWnd, ref ps );
                    break;
                case WM_DESTROY:
                    PostQuitMessage(0);
                    break;
                default:
                    return DefWindowProc(hWnd, uMsg, wParam, lParam);
            }
     
            return IntPtr.Zero;
        }
     
        static int WinMain(string[] args)
        {
            IntPtr hInstance = Marshal.GetHINSTANCE(typeof(Hello).Module);
            const string CLASS_NAME = "helloWindow";
            const string WINDOW_NAME = "Hello, World!";
     
            WNDCLASSEX wcex = new WNDCLASSEX();
            wcex.cbSize = (uint)Marshal.SizeOf(wcex);
            wcex.style = CS_HREDRAW | CS_VREDRAW;
            wcex.lpfnWndProc = new WndProcDelgate(WndProc);
            wcex.cbClsExtra = 0;
            wcex.cbWndExtra = 0;
            wcex.hInstance = hInstance;
            wcex.hIcon = LoadIcon(hInstance, new IntPtr(IDI_APPLICATION));
            wcex.hCursor = LoadIcon(hInstance, new IntPtr(IDC_ARROW));
            wcex.hbrBackground = new IntPtr(COLOR_WINDOW + 1);
            wcex.lpszMenuName = "";
            wcex.lpszClassName = CLASS_NAME;
            wcex.hIconSm = IntPtr.Zero;
     
            RegisterClassEx(ref wcex);
     
            IntPtr hWnd = CreateWindowEx(
                0,
                wcex.lpszClassName,
                WINDOW_NAME,
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                640,
                480,
                IntPtr.Zero,
                IntPtr.Zero,
                wcex.hInstance,
                IntPtr.Zero);
     
            ShowWindow(hWnd, SW_SHOWDEFAULT);
            UpdateWindow(hWnd);
     
            MSG msg = new MSG();
            while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0)
            {
                TranslateMessage(ref msg);
                DispatchMessage(ref msg);
            }
     
            return (int)msg.wParam;
        }
     
        [STAThread]
        static int Main(string[] args)
        {
            return WinMain( args );
        }
    }

    コンパイル方法

    C:¥> ilasm Hello.il

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(MSIL) World!             |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, Windows Forms(MSIL) World!

    Posted on 6月 6th, 2012 by cx20

    Windows Forms(MSIL)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は MSIL における Windows フォーム の例となっている。

    ソースコード

    //  Microsoft (R) .NET Framework IL Disassembler.  Version 4.0.30319.1
    //  Copyright (c) Microsoft Corporation. All rights reserved.
     
    // Metadata version: v4.0.30319
    .assembly extern System.Windows.Forms
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 4:0:0:0
    }
    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 4:0:0:0
    }
    .assembly extern System.Drawing
    {
      .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
      .ver 4:0:0:0
    }
    .assembly Hello
    {
      .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
      .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module Hello.exe
    // MVID: {FF5266CD-116D-4A87-B4A5-1B94604BA0FE}
    .imagebase 0x00400000
    .file alignment 0x00000200
    .stackreserve 0x00100000
    .subsystem 0x0002       // WINDOWS_GUI
    .corflags 0x00000001    //  ILONLY
    // Image base: 0x001E0000
     
     
    // =============== CLASS MEMBERS DECLARATION ===================
     
    .class private auto ansi beforefieldinit HelloForm
           extends [System.Windows.Forms]System.Windows.Forms.Form
    {
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // コード サイズ       94 (0x5e)
        .maxstack  4
        .locals init (class [System.Windows.Forms]System.Windows.Forms.Label V_0)
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
        IL_0006:  nop
        IL_0007:  nop
        IL_0008:  ldarg.0
        IL_0009:  ldc.i4     0x280
        IL_000e:  ldc.i4     0x1e0
        IL_0013:  newobj     instance void [System.Drawing]System.Drawing.Size::.ctor(int32,
                                                                                      int32)
        IL_0018:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::set_Size(valuetype [System.Drawing]System.Drawing.Size)
        IL_001d:  nop
        IL_001e:  ldarg.0
        IL_001f:  ldstr      "Hello, World!"
        IL_0024:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
        IL_0029:  nop
        IL_002a:  newobj     instance void [System.Windows.Forms]System.Windows.Forms.Label::.ctor()
        IL_002f:  stloc.0
        IL_0030:  ldloc.0
        IL_0031:  ldc.i4     0x140
        IL_0036:  ldc.i4.s   20
        IL_0038:  newobj     instance void [System.Drawing]System.Drawing.Size::.ctor(int32,
                                                                                      int32)
        IL_003d:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Size(valuetype [System.Drawing]System.Drawing.Size)
        IL_0042:  nop
        IL_0043:  ldloc.0
        IL_0044:  ldstr      "Hello, Windows Forms(MSIL) World!"
        IL_0049:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
        IL_004e:  nop
        IL_004f:  ldarg.0
        IL_0050:  call       instance class [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection [System.Windows.Forms]System.Windows.Forms.Control::get_Controls()
        IL_0055:  ldloc.0
        IL_0056:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection::Add(class [System.Windows.Forms]System.Windows.Forms.Control)
        IL_005b:  nop
        IL_005c:  nop
        IL_005d:  ret
      } // end of method HelloForm::.ctor
     
      .method private hidebysig static void  Main() cil managed
      {
        .entrypoint
        .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
        // コード サイズ       15 (0xf)
        .maxstack  1
        .locals init (class HelloForm V_0)
        IL_0000:  nop
        IL_0001:  newobj     instance void HelloForm::.ctor()
        IL_0006:  stloc.0
        IL_0007:  ldloc.0
        IL_0008:  call       void [System.Windows.Forms]System.Windows.Forms.Application::Run(class [System.Windows.Forms]System.Windows.Forms.Form)
        IL_000d:  nop
        IL_000e:  ret
      } // end of method HelloForm::Main
     
    } // end of class HelloForm
     
     
    // =============================================================
     
    // *********** 逆アセンブルが完了しました ***********************
    // 警告: Win32 リソース ファイル hello.res を作成しました。

    上記コードは、以下の C# の実行ファイルを ildasm.exe で逆アセンブルしたものである。

    ソースコード

    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    class HelloForm : Form
    {
        public HelloForm()
        {
            this.Size = new Size( 640, 480 );
            this.Text = "Hello, World!";
            Label label1 = new Label();
            label1.Size = new Size( 320, 20 );
            label1.Text = "Hello, Windows Forms(MSIL) World!";
            this.Controls.Add( label1 );
        }
        [STAThread]
        static void Main()
        {
            HelloForm form = new HelloForm();
            Application.Run(form);
        }
    }

    コンパイル方法

    C:¥> ilasm Hello.il
    

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(MSIL) World!         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. Hello, COM(MSIL) World!

    Posted on 5月 22nd, 2012 by cx20

    MSIL

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は MSIL による COM クライアントの例となっている。

    ソースコード

     
    //  Microsoft (R) .NET Framework IL Disassembler.  Version 4.0.30319.1
    //  Copyright (c) Microsoft Corporation. All rights reserved.
     
     
    // Metadata version: v2.0.50727
    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly extern Shell32
    {
      .ver 1:0:0:0
    }
    .assembly Hello
    {
      .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
      .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module Hello.exe
    // MVID: {558CDDD7-4AF4-48D6-A830-0DA2C5C15D19}
    .imagebase 0x00400000
    .file alignment 0x00000200
    .stackreserve 0x00100000
    .subsystem 0x0003       // WINDOWS_CUI
    .corflags 0x00000001    //  ILONLY
    // Image base: 0x01D10000
     
     
    // =============== CLASS MEMBERS DECLARATION ===================
     
    .class private auto ansi beforefieldinit Hello
           extends [mscorlib]System.Object
    {
      .method private hidebysig static void  Main(string[] args) cil managed
      {
        .entrypoint
        // コード サイズ       56 (0x38)
        .maxstack  5
        .locals init (class [Shell32]Shell32.Shell V_0,
                 class [Shell32]Shell32.Folder V_1,
                 object V_2,
                 bool V_3)
        IL_0000:  nop
        IL_0001:  newobj     instance void [Shell32]Shell32.ShellClass::.ctor()
        IL_0006:  stloc.0
        IL_0007:  ldc.i4.s   36
        IL_0009:  conv.i8
        IL_000a:  box        [mscorlib]System.Int64
        IL_000f:  stloc.2
        IL_0010:  ldloc.0
        IL_0011:  ldc.i4.0
        IL_0012:  ldstr      "Hello, COM(MSIL) World!"
        IL_0017:  ldc.i4.0
        IL_0018:  ldloc.2
        IL_0019:  callvirt   instance class [Shell32]Shell32.Folder [Shell32]Shell32.IShellDispatch5::BrowseForFolder(int32,
                                                                                                                      string,
                                                                                                                      int32,
                                                                                                                      object)
        IL_001e:  stloc.1
        IL_001f:  ldloc.1
        IL_0020:  ldnull
        IL_0021:  ceq
        IL_0023:  stloc.3
        IL_0024:  ldloc.3
        IL_0025:  brtrue.s   IL_0030
     
        IL_0027:  nop
        IL_0028:  ldloc.1
        IL_0029:  call       int32 [mscorlib]System.Runtime.InteropServices.Marshal::ReleaseComObject(object)
        IL_002e:  pop
        IL_002f:  nop
        IL_0030:  ldloc.0
        IL_0031:  call       int32 [mscorlib]System.Runtime.InteropServices.Marshal::ReleaseComObject(object)
        IL_0036:  pop
        IL_0037:  ret
      } // end of method Hello::Main
     
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // コード サイズ       7 (0x7)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
        IL_0006:  ret
      } // end of method Hello::.ctor
     
    } // end of class Hello
     
     
    // =============================================================
     
    // *********** 逆アセンブルが完了しました ***********************
    // 警告: Win32 リソース ファイル Hello.res を作成しました。

    上記コードは、以下の C# の実行ファイルを ildadm.exe で逆アセンブルしたものである。

    ソースコード

    using System;
    using System.Runtime.InteropServices;
    using Shell32;
     
    class Hello
    {
        static void Main(string[] args)
        {
            Shell shell = new Shell();
            Folder folder;
            object vRootFolder = (long)ShellSpecialFolderConstants.ssfWINDOWS;
            folder = shell.BrowseForFolder(0, "Hello, COM(MSIL) World!", 0, vRootFolder);
            if (folder != null)
            {
                Marshal.ReleaseComObject(folder);
            }
            Marshal.ReleaseComObject(shell);
        }
    }

    コンパイル&実行方法(.NET Framework)

    C:¥> ilasm Hello.il
    C:¥> Hello

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(MSIL) Wolrd!                |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+
  5. Hello, Win32 API(MSIL) World!

    Posted on 4月 26th, 2012 by cx20

    Win32 API(MSIL)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は MSIL による呼出し例である。

    ソースコード

    //  Microsoft (R) .NET Framework IL Disassembler.  Version 4.0.30319.1
    //  Copyright (c) Microsoft Corporation. All rights reserved.
     
    // Metadata version: v2.0.50727
    .module extern user32.dll
    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly Hello
    {
      .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
      .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module Hello.exe
    // MVID: {412A0088-7664-4361-B59D-98DD5760B045}
    .imagebase 0x00400000
    .file alignment 0x00000200
    .stackreserve 0x00100000
    .subsystem 0x0003       // WINDOWS_CUI
    .corflags 0x00000001    //  ILONLY
    // Image base: 0x00360000
     
    // =============== CLASS MEMBERS DECLARATION ===================
    .class private auto ansi beforefieldinit Hello
           extends [mscorlib]System.Object
    {
      .method private hidebysig static pinvokeimpl("user32.dll" winapi) 
              uint32  MessageBox(uint32 hWnd,
                                 string lpText,
                                 string lpTitle,
                                 uint32 uType) cil managed preservesig
      {
      }
      .method private hidebysig static void  Main(string[] args) cil managed
      {
        .entrypoint
        // コード サイズ       20 (0x14)
        .maxstack  8
        IL_0000:  nop
        IL_0001:  ldc.i4.0
        IL_0002:  ldstr      "Hello, Windows API(MSIL) World!"
        IL_0007:  ldstr      "Hello, World!"
        IL_000c:  ldc.i4.0
        IL_000d:  call       uint32 Hello::MessageBox(uint32,
                                                      string,
                                                      string,
                                                      uint32)
        IL_0012:  pop
        IL_0013:  ret
      } // end of method Hello::Main
     
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // コード サイズ       7 (0x7)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
        IL_0006:  ret
      } // end of method Hello::.ctor
     
    } // end of class Hello

    上記のコードは、以下の C# のプログラムを ildasm.exe で逆アセンブルしたものである。

    ソースコード

    using System.Runtime.InteropServices;
     
    class Hello
    {
         [DllImport("user32.dll")]
         private extern static uint MessageBox(uint hWnd, string lpText, string lpTitle, uint uType);
         static void Main(string[] args)
        {
            MessageBox( 0, "Hello, Win32 API(MSIL) World!", "Hello, World!", 0 );
        }
    }

    コンパイル方法

    C:¥> ilasm Hello.il

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(MSIL) World!
    ---------------------------
    OK   
    ---------------------------
  6. Hello, MSIL World!

    Posted on 12月 31st, 2011 by cx20

    MSIL

    MSIL は .NET Framework で使用されている中間言語(Intermediate Language)である。

    ソースコード

    .assembly extern mscorlib {}
    .assembly hello {}
     
    .method static void Main() cil managed {
        .entrypoint
        ldstr "Hello, MSIL World!"
        call void [mscorlib]System.Console::WriteLine(string)
        ret
    }

    上記コードは以下の C# のソースに対応する。
    アセンブリファイル(.NET でコンパイルしたモジュール)を ildasm で逆アセンブル(ildasdm hello.exe /out:hello.il)することで MSIL のコードを得られる。

    using System;
     
    class Hello 
    {
        static void Main( String[] args ) 
        {
            Console.WriteLine( "Hello, MSIL World!" );
        }
    }

    コンパイル&実行方法(Mono)

    $ ilasm hello.il
    $ mono hello.exe
    

    コンパイル&実行方法(.NET Framework)

    C:¥> ilasm hello.il
    C:¥> hello

    実行結果

    Hello, MSIL World!