Search code examples
vb.netreflectionreflection.emitil

Use System.Reflection.Emit to show a MessageBox in Windows.Forms


I am trying to do a msgbox with ilgenerator.emit but I have an exception when I run the code:

exception generated from destination of a call

This is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim assemblyName As AssemblyName = New AssemblyName("SamAsm")
    Dim assemblyBuilder As AssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave)

    'Creating module
    Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("SamAsm.exe")

    Creating class file
    Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("SamAsmType", TypeAttributes.Public Or TypeAttributes.Class)

    Creating Main Method
    Dim methodBuilder1 As MethodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Static Or MethodAttributes.Public, GetType(Void), New System.Type() {GetType(String)})

    'Generating Code for method builder in IL
    Dim ilGenerator As ILGenerator = methodBuilder1.GetILGenerator
    ilGenerator.Emit(OpCodes.Ldstr, "test")
    ilGenerator.EmitCall(OpCodes.Call, GetType(System.Windows.Forms.MessageBox).GetMethod("Show", {GetType(String)}), {GetType(String)})
    ilGenerator.Emit(OpCodes.Ret)

    'Creating Class object (type) and accessing main method
    Dim SamAsmType As Type = typeBuilder.CreateType
    SamAsmType.GetMethod("Main").Invoke(Nothing, New String() {Nothing})

    'Creating Entry Point and saving assembly
    assemblyBuilder.SetEntryPoint(methodBuilder1, PEFileKinds.WindowApplication)
    assemblyBuilder.Save("SamAsm.exe")
End Sub

Solution

  • You have to ensure the stack is empty when returning from a Sub/void function.

    The Show-Method returns a DialogResult, so you have to call Pop to remove the DialogResult from the stack.

    ...
    ilGenerator.EmitCall(OpCodes.Call, GetType(System.Windows.Forms.MessageBox).GetMethod("Show", {GetType(String)}), {GetType(String)})
    ilGenerator.Emit(OpCodes.Pop) ' remove the return value of Show from the stack
    ilGenerator.Emit(OpCodes.Ret)
    ...
    

    Nonetheless, you can always use peverify to trace bugs in your dynamically created assemblies.

    So in your case, running peverify over SamAsm.exe would give you following error:

    [IL]: Error: [c:\yourpath\SamAsm.exe : SamAsmType::Main][offset 0x0000000A] Stack must be empty on return from a void function. 1 Error(s) Verifying SamAsm.exe