473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
5 4272
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ahmet | last post by:
Hi all; I have one application in which I read form names from database to be opened. I open form with the code below, and call its show method for form to be shown but before this, I must set...
0
by: Bsiang Tan | last post by:
Dear all experts, I have this: An assembly (proxy.dll) which is a WebService proxcy client Case 1 : Loading proxy.dll using Assembly.LoadFrom(...), invoke a static method of a class which...
1
by: RickDee | last post by:
Hi. I need help badly here. I need to write a program to invoke methods ( static, public, nonstatic, with parameters, and etc ) from an exe file, but I keep facing problem. From all the notes...
5
by: Vishal | last post by:
Hello, I already asked this question in the ASP.NET forums, but no help came. So I am hoping that somebody can help me out. This is really very URGENT me. For my e-commerce application, I...
2
by: Chandrakant Shinde | last post by:
Hi there, I want to copy a Image to the clipboard. When i try to do so following exception occurs : "The current thread must set to Single Thread Apartment (STA) mode before OLE calls can be...
0
by: Matt Wood | last post by:
Hi, I have written a Web Service for a customer which expects a SOAP message with Document/Literal encoding, and uses RoutingStyle=SoapServiceRoutingStyle.RequestElement to route the SOAP body...
6
by: ajit | last post by:
I am working on web service which in turn call com components. if # of users using the web service increases. Web service fails is there some why I can prevent max # of concurrent users using the...
1
by: roadbai | last post by:
Hi all, This is the first time to post question here, hopefully experts of perl here can give me a hand, to be honest, I am kind of new to perl, and I am struggling with the "Out of memory" issue I...
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.