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

Tags:

Categories: .NET, DirectX, MSIL

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing