By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(Event Handler), 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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

RickDee wrote:
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(Event Handler), 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

Nov 16 '05 #2

P: n/a
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" <si***************@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

RickDee wrote:
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(Event Handler), 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

Nov 16 '05 #3

P: n/a
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

RickDee wrote:
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" <si***************@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
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

RickDee wrote:
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(Ev entHandler), 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


Nov 16 '05 #4

P: n/a
Sijin Joseph <si***************@hotmail.com> wrote:
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
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
Well that's what i meant :D :D i wasn't refering to to the try/catch
method of casting.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Jon Skeet [C# MVP] wrote:
Sijin Joseph <si***************@hotmail.com> wrote:
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
}

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.