Archive for 4月 26th, 2012

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