Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:51:06+00:00 2026-06-15T08:51:06+00:00

I am trying to do a msgbox with ilgenerator.emit but I have an exception

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:51:07+00:00Added an answer on June 15, 2026 at 8:51 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to figure out why this is happening - I have an input text
Hey all i am trying to delete a key from the registery but i
(I'm trying to create my own custom MSGBOX from wpf...) How do I show
I'm trying to download a file in vb.net but the url I'm downloading from
I'm trying to serialize a LINQ result in this way: Private Sub btnXML_Click(ByVal sender
I am trying to make a call from my Application on a click of
I am trying to get this sub to work but the operationalRange variable is
I have been trying to make a VBA code to do the following. I
Trying to get this bit of code to enter a player name fill it
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.