IL汇编语言做一个窗体

165 篇文章 18 订阅
文章描述了作者通过逐步简化CIL(CommonIntermediateLanguage)代码,创建和运行一个WindowsForm应用程序的过程,强调了在编译和优化过程中哪些部分可以删除以实现基本功能。
摘要由CSDN通过智能技术生成

网上看到一段代码,

.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代码保存下来;

看一下代码相当多;

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

资源相关的删掉;

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

 

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

 

dispose删掉,销毁先不管;

 

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

 

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

 

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

 

一直删到这里,

 

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

 

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

 

 

 到此剩下的代码如下,



.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一下,可以构建成功并运行出现一个窗体;下回继续;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值