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
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