Question

I coded a builder in C# which compiles a code with CodeDom. The compiled .exe has to show up an MessageBox, but it doesnt do that. Everything works fine, the .exe is compiled and generated, but the output doesnt work (MessageBox doesnt appear).

Here is my code :

private void button1_Click(object sender, EventArgs e)
    {
        String InputCode = String.Empty;
        //Unser TestCode, in dem Wir ein MessageBox aufrufen
        InputCode = "MessageBox.Show((1 + 2 + 3).ToString());";   //Here is the MSGBox

        System.CodeDom.Compiler.CodeDomProvider CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
        //Parameter für die Compilierung, wie die einzubindenen Bibliotheken usw.
        System.CodeDom.Compiler.CompilerParameters CompilerParameters = new System.CodeDom.Compiler.CompilerParameters();
        CompilerParameters.ReferencedAssemblies.Add("System.dll");
        CompilerParameters.OutputAssembly = "Outpu2t.exe";
        CompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        CompilerParameters.CompilerOptions += "/target:winexe" + " " + "/win32icon:" + "\"" + textBox6.Text + "\"";
        CompilerParameters.GenerateExecutable = true;
        CompilerParameters.GenerateInMemory = false;

        //Über den StringBuilder wird der Code zusammengesetzt
        StringBuilder Temp = new StringBuilder();
        Temp.AppendLine(@"using System;");
        Temp.AppendLine(@"using System.Windows.Forms;");
        Temp.AppendLine(@"using System.Diagnostics;");
        Temp.AppendLine(@"namespace RunTimeCompiler{");
        Temp.AppendLine(@"public class Test{");
        Temp.AppendLine(@"public static void Main(){");
        Temp.AppendLine(@"}");
        Temp.AppendLine(@"public void Ergebnis(){");
        if (checkBox7.Checked)
        {
            Temp.AppendLine(@"ProcessStartInfo startInfo = new ProcessStartInfo();");
            Temp.AppendLine(@"startInfo.WindowStyle = ProcessWindowStyle.Hidden;");
        }
        else
        {
        }
        Temp.AppendLine(@InputCode);                //Here is the MessageBox 
        Temp.AppendLine(@"}}}");


        System.CodeDom.Compiler.CompilerResults CompilerResults = CodeDomProvider.CompileAssemblyFromSource(CompilerParameters, Temp.ToString());
        if (CompilerResults.Errors.Count > 0)
        {
            MessageBox.Show(CompilerResults.Errors[0].ErrorText, "Fehler bei Laufzeitkompilierung", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        else
        {
            MessageBox.Show("Done","Finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
Was it helpful?

Solution

You're generating code that has an empty Main(). So, your code does nothing, because that's exactly what you tell it to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top