Urgent... I need to invoke method from an exe file.

R

RickDee

Please help, anybody.

I am trying to write a program so that it can launch an exe file ( which is
also genereated in C# ) and then simulate the button clicking and invoke the
methods inside the exe. The button clicking part is working fine, but I just
cannot get the method call.

Here is my program snippet.

**************************************************************

private Assembly testAssembly = null;
Type[] tarray;
MethodInfo[] globalMethodInfo;


[STAThread]
static void Main()
{
Application.Run(new frmAutoTester()); // This program that I wrote
is called AutoTester
}
private void btnLoad_Click(object sender, System.EventArgs e) //
When user click this Load button, it will let user select which exe to load.
{
openFileDialog1.InitialDirectory = "..\\..\\DummyApp\\bin\\debug";
openFileDialog1.Filter = "Executable (*.exe)|*.exe";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtLoadExe.Text = openFileDialog1.FileName;
}
}
private void btnLaunch_Click(object sender, System.EventArgs e) // When
user click this Launch button, it will launch the exe to the screen.
{
testAssembly = Assembly.LoadFrom(txtLoadExe.Text);
tarray = testAssembly.GetTypes();
foreach (Type t in tarray)
{
Type frmType = testAssembly.GetType(t.FullName);
lstType.Items.Add(frmType.FullName);
testForm = (Form)testAssembly.CreateInstance(frmType.FullName);
ThreadPool.QueueUserWorkItem(new WaitCallback(RunApp), testForm);
// Run the launched exe and the AutoTester in the same thread.
}
}
static void RunApp(Object state)
{
Application.Run((Form)state);
}

// This is the place where I tried to invoke methods and simulate button
click on the exe that get launched.

public void InvokeMethod (Form form, string methodName, params object[]
parms)
{
EventHandler eh =
(EventHandler)Delegate.CreateDelegate(typeof(EventHandler), form,
methodName);

if (eh != null)
form.Invoke(eh, parms);
}

// When user click this Start Button, it will then simulate the button
click and invoke the method named "Method1" in the exe.

private void btnStart_Click(object sender, System.EventArgs e) {
int index = lstType.SelectedIndex;
Type ty = tarray[index];

object[] p = {this, new EventArgs()};
InvokeMethod(testForm, "btnChangeText_Click", p); // There is a
ChangeText button in the exe. This part is working.
InvokeMethod(testForm, "Method1", p); // Method1 is the method in
the exe file. This is the part that is not working. ( HELP... )
}

**************************************************************


Thanks
Regards
 
S

Sijin Joseph

Does Method1 also have the signature void(object sender,EventArge e)? .
If not then you will have to use a different delegate with the correct
signature to invoke the method.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Please help, anybody.

I am trying to write a program so that it can launch an exe file ( which is
also genereated in C# ) and then simulate the button clicking and invoke the
methods inside the exe. The button clicking part is working fine, but I just
cannot get the method call.

Here is my program snippet.

**************************************************************

private Assembly testAssembly = null;
Type[] tarray;
MethodInfo[] globalMethodInfo;


[STAThread]
static void Main()
{
Application.Run(new frmAutoTester()); // This program that I wrote
is called AutoTester
}
private void btnLoad_Click(object sender, System.EventArgs e) //
When user click this Load button, it will let user select which exe to load.
{
openFileDialog1.InitialDirectory = "..\\..\\DummyApp\\bin\\debug";
openFileDialog1.Filter = "Executable (*.exe)|*.exe";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtLoadExe.Text = openFileDialog1.FileName;
}
}
private void btnLaunch_Click(object sender, System.EventArgs e) // When
user click this Launch button, it will launch the exe to the screen.
{
testAssembly = Assembly.LoadFrom(txtLoadExe.Text);
tarray = testAssembly.GetTypes();
foreach (Type t in tarray)
{
Type frmType = testAssembly.GetType(t.FullName);
lstType.Items.Add(frmType.FullName);
testForm = (Form)testAssembly.CreateInstance(frmType.FullName);
ThreadPool.QueueUserWorkItem(new WaitCallback(RunApp), testForm);
// Run the launched exe and the AutoTester in the same thread.
}
}
static void RunApp(Object state)
{
Application.Run((Form)state);
}

// This is the place where I tried to invoke methods and simulate button
click on the exe that get launched.

public void InvokeMethod (Form form, string methodName, params object[]
parms)
{
EventHandler eh =
(EventHandler)Delegate.CreateDelegate(typeof(EventHandler), form,
methodName);

if (eh != null)
form.Invoke(eh, parms);
}

// When user click this Start Button, it will then simulate the button
click and invoke the method named "Method1" in the exe.

private void btnStart_Click(object sender, System.EventArgs e) {
int index = lstType.SelectedIndex;
Type ty = tarray[index];

object[] p = {this, new EventArgs()};
InvokeMethod(testForm, "btnChangeText_Click", p); // There is a
ChangeText button in the exe. This part is working.
InvokeMethod(testForm, "Method1", p); // Method1 is the method in
the exe file. This is the part that is not working. ( HELP... )
}

**************************************************************


Thanks
Regards
 
R

RickDee

Sijin Joseph and Michel Walsh,

Thanks for your help. I have managed to figure out how to resolve the
problem. I figured out that I have to use :

1. MethodInvoker casting to invoke Methods from assembly.
2. EventHandler casting to invoke EventHandler like button clicking from
assembly.

Now I am faced with another challenge. How do I find out from the assembly
that it is a method or an eventhandler? What I found out is that when the
assembly is loaded, both the button clicking event handler and normal method
declaration are considered as "Method".

Thanks
Regards


Sijin Joseph said:
Does Method1 also have the signature void(object sender,EventArge e)? .
If not then you will have to use a different delegate with the correct
signature to invoke the method.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Please help, anybody.

I am trying to write a program so that it can launch an exe file ( which is
also genereated in C# ) and then simulate the button clicking and invoke the
methods inside the exe. The button clicking part is working fine, but I just
cannot get the method call.

Here is my program snippet.

**************************************************************

private Assembly testAssembly = null;
Type[] tarray;
MethodInfo[] globalMethodInfo;


[STAThread]
static void Main()
{
Application.Run(new frmAutoTester()); // This program that I wrote
is called AutoTester
}
private void btnLoad_Click(object sender, System.EventArgs e) //
When user click this Load button, it will let user select which exe to load.
{
openFileDialog1.InitialDirectory = "..\\..\\DummyApp\\bin\\debug";
openFileDialog1.Filter = "Executable (*.exe)|*.exe";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtLoadExe.Text = openFileDialog1.FileName;
}
}
private void btnLaunch_Click(object sender, System.EventArgs e) // When
user click this Launch button, it will launch the exe to the screen.
{
testAssembly = Assembly.LoadFrom(txtLoadExe.Text);
tarray = testAssembly.GetTypes();
foreach (Type t in tarray)
{
Type frmType = testAssembly.GetType(t.FullName);
lstType.Items.Add(frmType.FullName);
testForm = (Form)testAssembly.CreateInstance(frmType.FullName);
ThreadPool.QueueUserWorkItem(new WaitCallback(RunApp), testForm);
// Run the launched exe and the AutoTester in the same thread.
}
}
static void RunApp(Object state)
{
Application.Run((Form)state);
}

// This is the place where I tried to invoke methods and simulate button
click on the exe that get launched.

public void InvokeMethod (Form form, string methodName, params object[]
parms)
{
EventHandler eh =
(EventHandler)Delegate.CreateDelegate(typeof(EventHandler), form,
methodName);

if (eh != null)
form.Invoke(eh, parms);
}

// When user click this Start Button, it will then simulate the button
click and invoke the method named "Method1" in the exe.

private void btnStart_Click(object sender, System.EventArgs e) {
int index = lstType.SelectedIndex;
Type ty = tarray[index];

object[] p = {this, new EventArgs()};
InvokeMethod(testForm, "btnChangeText_Click", p); // There is a
ChangeText button in the exe. This part is working.
InvokeMethod(testForm, "Method1", p); // Method1 is the method in
the exe file. This is the part that is not working. ( HELP... )
}

**************************************************************


Thanks
Regards
 
S

Sijin Joseph

An easy way would be to try and cast the method as an EventHandler
first, if the cast fails then invoke it as a normal method.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Sijin Joseph and Michel Walsh,

Thanks for your help. I have managed to figure out how to resolve the
problem. I figured out that I have to use :

1. MethodInvoker casting to invoke Methods from assembly.
2. EventHandler casting to invoke EventHandler like button clicking from
assembly.

Now I am faced with another challenge. How do I find out from the assembly
that it is a method or an eventhandler? What I found out is that when the
assembly is loaded, both the button clicking event handler and normal method
declaration are considered as "Method".

Thanks
Regards


Does Method1 also have the signature void(object sender,EventArge e)? .
If not then you will have to use a different delegate with the correct
signature to invoke the method.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Please help, anybody.

I am trying to write a program so that it can launch an exe file ( which
is
also genereated in C# ) and then simulate the button clicking and invoke
the
methods inside the exe. The button clicking part is working fine, but I
just
cannot get the method call.

Here is my program snippet.

**************************************************************

private Assembly testAssembly = null;
Type[] tarray;
MethodInfo[] globalMethodInfo;


[STAThread]
static void Main()
{
Application.Run(new frmAutoTester()); // This program that I
wrote
is called AutoTester
}
private void btnLoad_Click(object sender, System.EventArgs e)
//
When user click this Load button, it will let user select which exe to
load.
{
openFileDialog1.InitialDirectory = "..\\..\\DummyApp\\bin\\debug";
openFileDialog1.Filter = "Executable (*.exe)|*.exe";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtLoadExe.Text = openFileDialog1.FileName;
}
}
private void btnLaunch_Click(object sender, System.EventArgs e) //
When
user click this Launch button, it will launch the exe to the screen.
{
testAssembly = Assembly.LoadFrom(txtLoadExe.Text);
tarray = testAssembly.GetTypes();
foreach (Type t in tarray)
{
Type frmType = testAssembly.GetType(t.FullName);
lstType.Items.Add(frmType.FullName);
testForm = (Form)testAssembly.CreateInstance(frmType.FullName);
ThreadPool.QueueUserWorkItem(new WaitCallback(RunApp), testForm);
// Run the launched exe and the AutoTester in the same thread.
}
}
static void RunApp(Object state)
{
Application.Run((Form)state);
}

// This is the place where I tried to invoke methods and simulate
button
click on the exe that get launched.

public void InvokeMethod (Form form, string methodName, params
object[]
parms)
{
EventHandler eh =
(EventHandler)Delegate.CreateDelegate(typeof(EventHandler), form,
methodName);

if (eh != null)
form.Invoke(eh, parms);
}

// When user click this Start Button, it will then simulate the
button
click and invoke the method named "Method1" in the exe.

private void btnStart_Click(object sender, System.EventArgs e) {
int index = lstType.SelectedIndex;
Type ty = tarray[index];

object[] p = {this, new EventArgs()};
InvokeMethod(testForm, "btnChangeText_Click", p); // There is
a
ChangeText button in the exe. This part is working.
InvokeMethod(testForm, "Method1", p); // Method1 is the method
in
the exe file. This is the part that is not working. ( HELP... )
}

**************************************************************


Thanks
Regards
 
J

Jon Skeet [C# MVP]

Sijin Joseph said:
An easy way would be to try and cast the method as an EventHandler
first, if the cast fails then invoke it as a normal method.

Actually casting and then catching the exception is a very bad way of
doing that, however. Use

EventHandler eventHandler = x as EventHandler;
if (x==null)
{
// Invoke as normal method
}
else
{
// Invoke as EventHandler
}
 

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