Select Page

I played around with using csc /out:outfile.exe program.cs but the thing is you can use an executable or a DLL just the same using the Assembly.Load() method which comes with the System.Inflection namespace. This method does the following – Loads an assembly.

Pretty simple stuff. Its output is a little more detailed – Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller. Blah, blah.

Basically in this tutorial I will show you how to run a C# program inside another C# program.

HOW TO COMPILE .NET PROGRAMS TO EXE ON MACS

You may have a Macbook (likely) if you are developing .NET apps. By default the new solution will be compiled into a DLL and placed in the /users/{you}/Projects folder. However by using Visual Studio for Mac Community to code the program and csc to compile it, you can compile to executables instead of dll files.

csc /out:test.exe Program.cs

When I go to confirm the file is indeed an executable, file Program.exe the output says yes! Program.exe: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows

Start by making a new C# console application and put this in the file.

using System;

namespace helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
        }
    }
}

Now build the solution and remember where the heck it got built to.

using System;
using System.IO;
using System.Reflection;

namespace LoadAssembly
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Byte[] bytes = File.ReadAllBytes(@"/Users/guru/test.exe");

           
            Assembly assembly = Assembly.Load(bytes);
            MethodInfo method = assembly.EntryPoint;
            string[] param = new string[] { "hi" };
            object[] parameters = new[] { param };

            object execute = method.Invoke(null,parameters);

        }
    }
}

That will work and print out “hello world!”. Now remember how i said this works for DLLs too? This works too, change the file to have this instead and build it again.

using System;
using System.IO;
using System.Reflection;

namespace LoadAssembly
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Byte[] bytes = File.ReadAllBytes(@"/Users/guru/helloworld.dll");

           
            Assembly assembly = Assembly.Load(bytes);
            MethodInfo method = assembly.EntryPoint;
            string[] param = new string[] { "hi" };
            object[] parameters = new[] { param };

            object execute = method.Invoke(null,parameters);

        }

   
    }
}

That’s how easy it is to call a .NET assembly (a compiled C# program – exe or DLL) from within another one.

Want to learn more ethical hacking? I highly recommend buying my book made for beginners to Pentesting Become An Ethical Hacker. Check the price on Amazon.


error: