Invoking Console Application's Program.Main from another project

G

Gregory A. Beamer

Hi,
Is it possible to invoke the entry point of a console application
project from another project. Or, do I have to invoke the associated
exe as per the post below
http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

TIA

Answer: As per the post below.


If you have control of the other program's source, you can refactor the
bits you want to call out of Main and call them. But, if not, you are
stuck with firing up via Process or using PInvoke to fire off the
process from the Win API (not wise if the process is .NET).


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
S

Scott Seligman

Shikari Shambu said:
Is it possible to invoke the entry point of a console application
project from another project. Or, do I have to invoke the associated exe as
per the post below
http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

You could use reflection. Untested code follows:

// Runner program
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start of runner.");
Assembly assembly = Assembly.LoadFrom("Example.exe");

foreach (Type type in assembly.GetTypes())
{
MethodInfo method = type.GetMethod("Main",
BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);

if (method != null)
{
List<object> nulls = new List<object>();
for (int i = 0; i < method.GetParameters().Length; i++)
{
nulls.Add(null);
}
method.Invoke(null, nulls.ToArray());
}

}

Console.WriteLine("End of runner.");
}
}

// Simple example app, named Example.exe
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Inside example app.");
}
}
 
B

Ben Voigt [C++ MVP]

Scott Seligman said:
Shikari Shambu said:
Is it possible to invoke the entry point of a console application
project from another project. Or, do I have to invoke the associated exe
as
per the post below
http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

You could use reflection. Untested code follows:

// Runner program
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start of runner.");
Assembly assembly = Assembly.LoadFrom("Example.exe");

foreach (Type type in assembly.GetTypes())
{
MethodInfo method = type.GetMethod("Main",
BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);

if (method != null)
{
List<object> nulls = new List<object>();
for (int i = 0; i < method.GetParameters().Length; i++)
{
nulls.Add(null);
}

Assembly has an EntryPoint property which lets you skip all that fragile
searching for "Main".
method.Invoke(null, nulls.ToArray());
}

}

Console.WriteLine("End of runner.");
}
}

// Simple example app, named Example.exe
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Inside example app.");
}
}

--
--------- Scott Seligman <scott at <firstname> and michelle dot
net> ---------
The trouble with computers, of course, is that they're very
sophisticated idiots.
-- The Doctor in Doctor Who:"The Robot"
 
S

Scott Seligman

Ben Voigt said:
Assembly has an EntryPoint property which lets you skip all that fragile
searching for "Main".

Right you are. That's what I get for typing without thinking.
 
B

Ben Voigt [C++ MVP]

Scott Seligman said:
Right you are. That's what I get for typing without thinking.

I probably would have done the exact same thing, if I didn't know that the
C++/CLI compiler lets you name the entry point something else. I think the
C# compiler does too, and I'm positive that ilasm can, but I've not used the
C# alternate name feature.
 
A

Arne Vajhøj

Ben said:
Scott Seligman said:
Shikari Shambu said:
Is it possible to invoke the entry point of a console application
project from another project. Or, do I have to invoke the associated
exe as
per the post below
http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

You could use reflection. Untested code follows:

// Runner program
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start of runner.");
Assembly assembly = Assembly.LoadFrom("Example.exe");

foreach (Type type in assembly.GetTypes())
{
MethodInfo method = type.GetMethod("Main",
BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);

if (method != null)
{
List<object> nulls = new List<object>();
for (int i = 0; i < method.GetParameters().Length; i++)
{
nulls.Add(null);
}

Assembly has an EntryPoint property which lets you skip all that fragile
searching for "Main".
method.Invoke(null, nulls.ToArray());
}

}

Console.WriteLine("End of runner.");
}
}

// Simple example app, named Example.exe
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Inside example app.");
}
}

I think the original poster were interested in running any EXE
not just .NET EXE's.

But if we focus on .NET EXE's, then there may be an easier
solution than reflection.

If the example.exe class and Main method has proper accessibility
it can be as simple as just calling it:

public class Runner
{
public static void Main(string[] args)
{
Program.Main(null);
}
}

Arne
 
B

Ben Voigt [C++ MVP]

I think the original poster were interested in running any EXE
not just .NET EXE's.

non-.NET EXEs don't have Program.Main methods to run.
But if we focus on .NET EXE's, then there may be an easier
solution than reflection.

If the example.exe class and Main method has proper accessibility
it can be as simple as just calling it:

public class Runner
{
public static void Main(string[] args)
{
Program.Main(null);
}
}

If you have the executable ahead of time to add as a reference, sure. If
you need to work with an arbitrary .NET executable, then reflection is
needed to load the assembly dynamically.
 
A

Arne Vajhøj

Ben said:
non-.NET EXEs don't have Program.Main methods to run.

True.

But Program.Main were not mentioned by OP.

The link he gave ran a BAT file.
But if we focus on .NET EXE's, then there may be an easier
solution than reflection.

If the example.exe class and Main method has proper accessibility
it can be as simple as just calling it:

public class Runner
{
public static void Main(string[] args)
{
Program.Main(null);
}
}

If you have the executable ahead of time to add as a reference, sure.
If you need to work with an arbitrary .NET executable, then reflection
is needed to load the assembly dynamically.

True.

Arne
 
B

Ben Voigt [C++ MVP]

Arne Vajhøj said:
Peter said:
Ben Voigt [C++ MVP] wrote:
I think the original poster were interested in running any EXE
not just .NET EXE's.
non-.NET EXEs don't have Program.Main methods to run.

True.

But Program.Main were not mentioned by OP.

Well, if you don't count the "Subject:" field. :)

Ooops.

I missed that.

A surprising number of posts here have crucial information only in the
subject field.

Some people even use it for their entire question.

Not that I approve or anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top