Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hey !
can someone help me to know how generate a .exe file from a c# program?
i tried these codes , but its not succesfull

C#
AppDomain ad = AppDomain.CurrentDomain;
System.Reflection.AssemblyName an = new System.Reflection.AssemblyName();
an.Name = "Childprogram";
System.Reflection.Emit.AssemblyBuilder ab = ad.DefineDynamicAssembly(an, System.Reflection.Emit.AssemblyBuilderAccess.Save);
System.Reflection.Emit.ModuleBuilder mb = ab.DefineDynamicModule("testmodule", "Childprogram.exe");
System.Reflection.Emit.TypeBuilder tb = mb.DefineType("testtype", System.Reflection.TypeAttributes.Public);
System.Reflection.Emit.MethodBuilder metb = tb.DefineMethod("hi", System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Static,null, null);
            //mb.SetUserEntryPoint(metb);
            ab.SetEntryPoint(metb);
            System.Reflection.Emit.ILGenerator ilg = metb.GetILGenerator();
            ilg.EmitWriteLine("hiii worrld");
            ilg.Emit(System.Reflection.Emit.OpCodes.Ret);
            tb.IsCreated();
            ab.Save("Childprogram.exe",System.Reflection.PortableExecutableKinds.ILOnly,System.Reflection.ImageFileMachine.IA64);


but it gives an error :
HTML
Type 'testtype' was not completed.
\

what is problem?

i also tried these codes :
C#
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.GenerateInMemory = false;
cp.GenerateExecutable = true;
cp.OutputAssembly = "Childprogram.exe";
System.CodeDom.Compiler.CompilerResults cr=  cscp.CreateCompiler().CompileAssemblyFromFile(cp, @"a.cs");
if (cr.Errors.Count > 0)            MessageBox.Show(cr.Errors.Count.ToString()+cr.Errors[0].ToString()+cr.Errors[0].FileName);


but this one give me error to that is saying i dnt have a static main method in Childprogram.exe !
wheras this is my sourcefile code :
C#
using System;
namespace RDPCracker
{
    static class Program
    {
        	static void Main()
	{
		Console.WriteLine("asd");
	}
     }
}


what is problem?
very thanks to spend time for me!
Posted

1 solution

Quote:
but this one give me error to that is saying i dnt have a static main method in Childprogram.exe !
And it has right!
Look at the hello world app here: http://msdn.microsoft.com/en-us/library/aa288463(v=vs.71).aspx[^]
See the difference?
Your class and main method is static, but not public!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900