0

I have a .EXE that performs several calculations and was implemented by another company. I do not have access to the source code of this executable and it is out of question trying to modify its behaviour.

My problem is:

I am writing a code in c# to call this EXE and read its file output. After the EXE is done with the calculation, it opens a MessageBox -> "Calculation Done", and only after clicking in a "OK Button" the output file is written. That really sucks, since it is necessary the user manually click with the mouse to close the MessageBox. I am wondering if it is possible to close this MessageBox programmatically?

I have googled it and my guess is that is it possible using MainWindowHandle, but I am not sure. Any help?

guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
  • 1
    That is the only solution as far as i know, mixed with `SendKeys`. – Asad Ali May 26 '14 at 17:24
  • [Check this out](http://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds/14522952#14522952) – Sriram Sakthivel May 26 '14 at 17:25
  • @SriramSakthivel The OP said that it can't modify the other exe and i don't see the utility of what you linked in *his* app. – Asad Ali May 26 '14 at 17:26
  • @AsadAli Who asked OP to modify the code. Accepted answer should work cross process. – Sriram Sakthivel May 26 '14 at 17:30
  • Thanks @AsadAli. I will try Process.GetProcesses()[0].CloseMainWindow(); – guilhermecgs May 26 '14 at 17:30
  • 1
    [This will Help full to you ][1] [1]: http://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds either create dynamic window with timer. – Buddhika May 26 '14 at 17:31
  • 1
    @guilhermecgs [This](http://stackoverflow.com/questions/9314524/c-sharp-sendkeys-sendwait-to-a-dialog-of-another-process-notepad-exe) is your identical case, give it a look. – Asad Ali May 26 '14 at 17:32

1 Answers1

2

You can call FindWindow to locate your MessageBox, and then use SendMessage to send a close message

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);

[DllImport("user32.dll", EntryPoint = "FindWindow")] 
private static extern IntPtr FindWindow(string lp1, string lp2);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;

IntPtr window = FindWindow(null, "MessageBox Title Here");
if (window != IntPtr.Zero)
{
   SendMessage(window, WM_SYSCOMMAND, SC_CLOSE, 0);  
}

Read more here

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321