IL汇编语言做一个窗体

网上看到一段代码,

.assembly extern

mscorlib {} .assembly Classes

{ .ver 1:0:1:0    } 

.namespace MyForm
{
    .class public TestForm extends 
        [System.Windows.Forms]System.Windows.Forms.Form
    {
        .field private class [System]System.ComponentModel.IContainer components
        .method public static void Main() cil managed
        {
            .entrypoint
            .maxstack  1
        
            //Create New Object of TestForm Class and Call the Constructor     
            newobj     instance void MyForm.TestForm::.ctor()
            call       void [System.Windows.Forms]
              System.Windows.Forms.Application::Run(
              class [System.Windows.Forms]System.Windows.Forms.Form)
             ret
        }
    }
}

似乎是做一个窗体出来;拿去构建一下,没什么效果;光有这一段应该是还不能做出窗体;

用VS做一个窗体exe,然后ildasm,加/text参数,获取全部的il代码保存下来;

看一下代码相当多;

逐步的删除一些,只要先构建通过,运行出现一个窗体即可;

资源相关的删掉;

IL汇编语言做一个窗体_第1张图片

.assembly test { ... } 整个这一段删掉;

IL汇编语言做一个窗体_第2张图片 

可以构建成功,但是运行窗体不会出现;那保留这一段;

IL汇编语言做一个窗体_第3张图片 

dispose删掉,销毁先不管;

IL汇编语言做一个窗体_第4张图片 

资源Resource相关的删掉,应该没什么资源,

IL汇编语言做一个窗体_第5张图片 

Culture相关的删掉,不知道是什么,

IL汇编语言做一个窗体_第6张图片 

Settings相关的删掉,一个窗体没有用到,

IL汇编语言做一个窗体_第7张图片 

一直删到这里,

IL汇编语言做一个窗体_第8张图片 

下面看上去是Properties里有个Resources类,删掉,

IL汇编语言做一个窗体_第9张图片 

.assembly test { ... } 里面看一下,版权和版本相关的删掉,

IL汇编语言做一个窗体_第10张图片 

 

 到此剩下的代码如下,



.assembly extern System.Windows.Forms
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern System
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly test
{
  .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 )
  .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )
  .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 )


  .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 04 74 65 73 74 00 00 )                      // ...test..
  .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
  .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 04 74 65 73 74 00 00 )                      // ...test..
  .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 )

  .hash algorithm 0x00008004
  .ver 1:0:0:0
}

.module test.exe
// MVID: {AC8E5C6C-7768-4BFE-9524-E94CA472F376}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0002       // WINDOWS_GUI
.corflags 0x00020003    //  ILONLY 32BITREQUIRED
// Image base: 0x02460000


// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit test.Form1
       extends [System.Windows.Forms]System.Windows.Forms.Form
{
  .field private class [System]System.ComponentModel.IContainer components
  .method public hidebysig specialname rtspecialname
          instance void  .ctor() cil managed
  {
    // Code size       24 (0x18)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  ldnull
    IL_0002:  stfld      class [System]System.ComponentModel.IContainer test.Form1::components
    IL_0007:  ldarg.0
    IL_0008:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
    IL_000d:  nop
    IL_000e:  nop
    IL_000f:  ldarg.0
    IL_0010:  call       instance void test.Form1::InitializeComponent()
    IL_0015:  nop
    IL_0016:  nop
    IL_0017:  ret
  } // end of method Form1::.ctor



  .method private hidebysig instance void
          InitializeComponent() cil managed
  {
    // Code size       33 (0x21)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  newobj     instance void [System]System.ComponentModel.Container::.ctor()
    IL_0007:  stfld      class [System]System.ComponentModel.IContainer test.Form1::components
    IL_000c:  ldarg.0
    IL_000d:  ldc.i4.1
    IL_000e:  call       instance void [System.Windows.Forms]System.Windows.Forms.ContainerControl::set_AutoScaleMode(valuetype [System.Windows.Forms]System.Windows.Forms.AutoScaleMode)
    IL_0013:  nop
    IL_0014:  ldarg.0
    IL_0015:  ldstr      "Form1"
    IL_001a:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
    IL_001f:  nop
    IL_0020:  ret
  } // end of method Form1::InitializeComponent

} // end of class test.Form1

.class private abstract auto ansi sealed beforefieldinit test.Program
       extends [mscorlib]System.Object
{
  .method private hidebysig static void  Main() cil managed
  {
    .entrypoint
    .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
    // Code size       26 (0x1a)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  call       void [System.Windows.Forms]System.Windows.Forms.Application::EnableVisualStyles()
    IL_0006:  nop
    IL_0007:  ldc.i4.0
    IL_0008:  call       void [System.Windows.Forms]System.Windows.Forms.Application::SetCompatibleTextRenderingDefault(bool)
    IL_000d:  nop
    IL_000e:  newobj     instance void test.Form1::.ctor()
    IL_0013:  call       void [System.Windows.Forms]System.Windows.Forms.Application::Run(class [System.Windows.Forms]System.Windows.Forms.Form)
    IL_0018:  nop
    IL_0019:  ret
  } // end of method Program::Main

} // end of class test.Program

ilasm一下,可以构建成功并运行出现一个窗体;下回继续;

你可能感兴趣的:(.Net,汇编语言,ilasm,ctor)