IL 汇编学习笔记(四)

原文:http://www.codeproject.com/dotnet/ilassembly.asp

创建 Windows Form 程序


下列代码给出一个简单的例子,其中简单的设定了窗体的一些属性,BackColor, Text 和 WindowState.

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


这段代码里,我们看到,在 Main() 函数里通过调用 Application.Run 方法来 launch 了这个窗体程序。

以下是其构造器的定义:

    .method public specialname rtspecialname instance
        
void  .ctor() cil managed
    {     
      .maxstack  
4
            
      ldarg.
0
      
// 调用基类的构造器
      call       instance void [System.Windows.Forms]
                        System.Windows.Forms.Form::.ctor()
      
      
//Initialize the Components
      ldarg.0
      newobj   instance 
void [System]System.ComponentModel.Container::.ctor()
      stfld    
class [System]System.ComponentModel.IContainer
                        MyForm.TestForm::components

      
//Set the Title of the Window (Form)
      ldarg.0
      ldstr      
"This is the Title of the Form."
      call   instance 
void [System.Windows.Forms]
                       System.Windows.Forms.Control::set_Text(
string)
      
      
//Set the Back Color of the Form
      ldarg.0
      ldc.i4 
0xff
      ldc.i4 
0
      ldc.i4 
0
      call valuetype [System.Drawing]System.Drawing.Color
                     [System.Drawing]System.Drawing.Color::FromArgb(
                     int32, int32, int32)

      call   instance 
void [System.Windows.Forms]
                     System.Windows.Forms.Control::set_BackColor(
                     valuetype [System.Drawing]System.Drawing.Color)
      
      
      
//Maximize the Form using WindowState Property
      ldarg.0
      ldc.i4 
2        //2 for Maximize
      call instance void [System.Windows.Forms]
                     System.Windows.Forms.Form::set_WindowState(
                     valuetype [System.Windows.Forms]
                     System.Windows.Forms.FormWindowState)
      ret
    }


这段代码非常简单,不需要太多解释。注意其中首先需要去手工调用基类的构造器,另一点就是,枚举的初始化只需要简单的赋一个整数值就可以了。

我们还可以给这个窗体定义一个 Dispose 方法如下:

    .method family virtual instance void Dispose(bool disposing) cil managed
    {
      .maxstack  
2

      ldarg.
0
      ldfld      
class [System]System.ComponentModel.IContainer
                   MyForm.TestForm::components
      callvirt   instance 
void [mscorlib]System.IDisposable::Dispose()
        
        
//Call Base Class's Dispose Event
      ldarg.0
      ldarg.
1
      call       instance 
void [System.Windows.Forms]
                    System.Windows.Forms.Form::Dispose(
bool)
      ret
    }


错误处理和调试

下面会介绍一些常见的调试方法。
首先,我们可以在编译 IL 代码的时候生成调试信息:

ILAsm.exe Test.il /debug

这个命令除了生成 Test.exe,还会生成一个 Test.pdb, 其中包含调试信息。

peverify.exe test.exe

这个命令可以检查 PE 文件中的错误。

ILDasm.exe 用于反汇编 exe 为 IL 代码。如果要输出,可以这样:

ILDasm.exe SomeProject.exe /out:SomeProject.il

其他还有一些很有用的工具,如 DbgClr.exe, CorDbg.exe

posted on 2006-04-05 12:34  NeilChen  阅读(666)  评论(0编辑  收藏  举报

导航