开发者

Hiding Command Prompt with CodeDomProvider

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜