guilhermecgs
guilhermecgs

Reputation: 3061

Close a dialog programmatically after running an executable

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?

Upvotes: 0

Views: 169

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149608

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

Upvotes: 2

Related Questions