Archive for 6月 6th, 2012

  1. 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!         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+