views:

185

answers:

2

Hi All

I've just got my own little custom c# compiler made, using the article from MSDN.

But, when I create a new Windows Forms application using my sample compiler, the MSDOS window also appears, and if I close the DOS window, my WinForms app closes too. How can I tell the Compiler? not to show the MSDOS window at all?

Thank you :)

Here's my code:

using System;

namespace JTS
{
    public class CSCompiler
    {
        protected string ot,
            rt,
            ss, es;

        protected bool rg, cg;

        public string Compile(String se, String fe, String[] rdas, String[] fs, Boolean rn)
        {
            System.CodeDom.Compiler.CodeDomProvider CODEPROV = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
            ot =
                fe;

            System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters();
            // Ensure the compiler generates an EXE file, not a DLL.
            PARAMS.GenerateExecutable = true;
            PARAMS.OutputAssembly = ot;
            PARAMS.CompilerOptions = "/target:winexe"; PARAMS.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location);
            PARAMS.LinkedResources.Add("this.ico");

            foreach (String ay in rdas)
            {
                if (ay.Contains(".dll"))
                    PARAMS.ReferencedAssemblies.Add(ay);
                else
                {
                    string refd = ay;
                    refd = refd + ".dll";
                    PARAMS.ReferencedAssemblies.Add(refd);
                }

            }

            System.CodeDom.Compiler.CompilerResults rs = CODEPROV.CompileAssemblyFromFile(PARAMS, fs);

            if (rs.Errors.Count > 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError COMERR in rs.Errors)
                {
                    es = es +
                        "Line number: " + COMERR.Line +
                        ", Error number: " + COMERR.ErrorNumber +
                        ", '" + COMERR.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                // Compilation succeeded.
                es = "Compilation Succeeded.";

                if (rn) System.Diagnostics.Process.Start(ot);
            }
            return es;
        }
    }
}
+3  A: 

In C# compiler Console window is shown when /target switch is exe. When /target=winexe, Console window is not shown. http://msdn.microsoft.com/en-us/library/6h25dztx.aspx

Try this:

System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters(); 

PARAMS->CompilerOptions = "/target-winexe";

See: http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.compileroptions.aspx

Alex Farber
Where can I change these properties? I've posted my code in my question. Thanks
lucifer
Thanks heaps! But it is still showing the Command Prompt window.
lucifer
Sorry, it should be: /target:winexe
Alex Farber
lol. No worries, trying it now. :)
lucifer
It still doesn't work with: /target:winexe.
lucifer
@Alex @j-t-s Try "-target:winexe" instead.
Michael Stum
+1  A: 

I don't know which MSDN Article you are referring to, but if you use the AssemblyBuilder then the "magic" is in the call to SetEntryPoint.

If you have a Windows Forms application, you need to specify PEFileKinds.WindowApplication:

var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(
    new AssemblyName(assemblyName), AssemblyBuilderAccess.Save);
var mod = asm.DefineDynamicModule(assemblyName, fileName);        
var type = mod.DefineType("Program",
    TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public);
var mainMethod = type.DefineMethod("Main",
    MethodAttributes.Public | MethodAttributes.Static);
// ... Code for Main method and the rest ...
type.CreateType();
asm.SetEntryPoint(mainMethod,PEFileKinds.WindowApplication);
asm.Save(fileName);

Other PEFileKinds are ConsoleApplication and Dll, although I think the AssemblyBuilder automatically assumes it's a Dll if you don't specify an EntryPoint.

Michael Stum
Thank you for your detailed answer, I am trying it out now. Here's the link to the MSDN article I was referring to: http://support.microsoft.com/kb/304655
lucifer
Um, unless I'm pasting your code in the wrong place, I have a teeny feeling that it may not exactly be relevant, because it had absolutely no effect at all. Please see my question, as I've edited it to contain my code so you can see how I've created the compiler. :)
lucifer
Ah, you're using CodeDom. No, my code wouldn't work, I added it before I saw your code example and wasn't sure what you used.
Michael Stum
Thanks for tryin to help :) I should've added my code first anyway. :)
lucifer
@Michael Stum, would you know how to do this with CodeDom?
lucifer