1

I am creating a console application that should call a VB application MyProj.exe and triggers a button click event on the same.

As of now I am able to run the executable file of the VB project but I wanted to fire the certain button click event from console application.

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"C:\New\MyProj.exe";
System.Diagnostics.Process.Start(startInfo);

i have tried with the below link - that is not working for me http://www.codeproject.com/Articles/14519/Using-Windows-APIs-from-C-again

-- the hwndChild after executing every statement is coming as "zero"

    //Get a handle for the "5" button
            hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","5");

            //send BN_CLICKED message
            SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

            //Get a handle for the "+" button
            hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","*");

            //send BN_CLICKED message
            SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

            //Get a handle for the "2" button
            hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","2");

            //send BN_CLICKED message
            SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

            //Get a handle for the "=" button
            hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","=");

            //send BN_CLICKED message
            SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

Thanks so much codecaster - but i would require little more help

     #define AMP_PAUSE 40046
    HWND hwnd = FindWindow("Winamp v1.x", 0);
    if(hwnd) SendMessage(hwnd, WM_COMMAND, AMP_PAUSE, 0);

button1 is the id of the button; and Call_Method() is the procedure which is called when we click button1.

can you pls help how to write the above code in c#

Code's
  • 208
  • 2
  • 18
  • i have checked that utility - but i am not able to figure out how to call the Button click event of my vb.exe - that too i want it to be in background. – Code's Nov 03 '14 at 11:31
  • Please show the actual code you used. "Not working for me" is not clear enough to reopen your question. Using the SendMessage API is the way to do this. – CodeCaster Nov 03 '14 at 13:40
  • i have downloaded the same code and when i ran it - the calculator open up but with "0" value.So this means there button event ared not fired. every time hwndChild value is coming as"0" – Code's Nov 03 '14 at 14:55
  • Yeah but you still should use the `FindWindow(Ex)` and `Send/PostMessage` functions to fake button clicks in other programs. There's not enough information in your question to troubleshoot the specific issue you're encountering. The CodeProject project is 8 years old, perhaps it won't work as intended anymore, but it does show you how it should be done. See also [how to programmatically click on a button in running app using C# code](http://stackoverflow.com/questions/4705039/how-to-programmatically-click-on-a-button-in-running-app-using-c-sharp-code). – CodeCaster Nov 03 '14 at 15:12

1 Answers1

2

I'm suggesting you a slightly different approach. Add a shortcut key to your button. This is done by putting a & before the letter you want to use as shortcut key in the button text. Then you can activate this button by entering Alt-X, where X is your shortcut key.

[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

With this declaration you can then send the shortcut key to your application:

// Start your process
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\New\MyProj.exe";
Process process = Process.Start(startInfo);

// Wait for your process to be idle, sometimes an additional
// Thread.Sleep(...); is required for the application to be ready.
process.WaitForInputIdle();

// Make the started application the foreground window.
IntPtr h = process.MainWindowHandle;
SetForegroundWindow(h);

// Send it Alt-X
SendKeys.SendWait("%x");
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188