Reputation:
I need to invoke a c# application within my c# control, as I do not feel like rewriting the application as a control.
I am able to launch the application using System.Diagnostics.Process.Start.
Now how do I call the methods in my application from/via the c# control as this is where I invoked the application using System.Diagnostics.Process.Start
Upvotes: 0
Views: 246
Reputation: 378
You can use System.Reflection to get into any assembly, regardless of EXE or DLL format.
To run another application, for example,
string fileName = "MainApp.exe";
string className = "Program";
string methodName = "Main";
string[] args = {"arg1", "arg2"};
Assembly asm = Assembly.LoadFrom(fileName);
foreach (Type typ in asm.GetTypes())
{
if (typ.Name == className)
{
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;
MethodInfo method = typ.GetMethod(methodName, flags);
method.Invoke(null, new object[] { args });
}
}
To get to other methods, change the names accordingly.
This is based on actual working code that I originally derived from this example.
There might be better ways to write it, though -- this thread suggests using the Activator class for more concise code.
Upvotes: 0
Reputation: 6643
You could modify your .exe application and add a remoting interface to it, making it a server-process, and then let your "control" act as client-process and call methods on the server.
This is a hacky design and I wouldn't recommend it, but since you asked :)
Upvotes: 0
Reputation: 22414
Since you have the source code, copy it into the new project or create a DLL as suggested by @Nick Berardi.
Upvotes: 0
Reputation: 1533
You can't call "methods" of another process because its running in an entirely different OS process. You would have to essentially ask the process to execute a method for you, wait for the result, and then marshall the information back into your process. Of course, you would have to have to expose the methods through some kind of interprocess communication technology like WCF, COM+, etc. If you've got that kind of access to the executing program, you can also just use the assembly directly.
You really don't want to go through that if you can help it. It's going to be a lot more hassle.
Upvotes: 0
Reputation: 54894
The easiest way is to change the application from .exe to .dll and then just reference the application in your project like a normal library.
Upvotes: 1