Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# execution reflection
hey !
can someone help me to know how generate a .exe file from a c# program?
i tried these codes , but its not succesfull
 
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 :
Type 'testtype' was not completed.
\
 
what is problem?
 
i also tried these codes :
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 :
using System;
namespace RDPCracker
{
    static class Program
    {
        	static void Main()
	{
		Console.WriteLine("asd");
	}
     }
}
 
what is problem?
very thanks to spend time for me!
Posted 20-Apr-13 8:07am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100