Close a message box from another program using C #

Here is my problem: we have an automated build process for our product. When compiling one of the VB6 projects, a message box appears that requires the user to click the OK button before he can move on. Being an automated process is bad because it can sit there for hours without moving until someone clicks normally. We looked at VB6 code to try to suppress the message box, but no one can figure out how to do it right now. Since the temp fix, I am working on a program that will run in the background and when the message box appears, closes it. So far I can detect when a message appears, but I cannot find a function to close it properly. The program is written in C #, and I use the FindWindow function in user32.dll to get a window pointer. So far I have tried closeWindow, endDialog and postMessage to try to close it, but none of them seem to work. closeWindow simply minimizes it, endDialog appears with a memory error, and postMessage does nothing. Does anyone know of any other features that will take care of this, or any other way to get rid of this message? Thanks in advance.

here is the code i have:

class Program { [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); static void Main(string[] args) { IntPtr window = FindWindow(null, "Location Browser Error"); while(window != IntPtr.Zero) { Console.WriteLine("Window found, closing..."); //use some function to close the window window = IntPtr.Zero; } } } 
+8
source share
2 answers

You should find the window, this is the first step. After sending the SC_CLOSE message using SendMessage .

Sample

 [DllImport("user32.dll")] Public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_CLOSE = 0xF060; IntPtr window = FindWindow(null, "Location Browser Error"); if (window != IntPtr.Zero) { Console.WriteLine("Window found, closing..."); SendMessage((int) window, WM_SYSCOMMAND, SC_CLOSE, 0); } 

More information

+9
source

When you find the message box, try sending it WM_NOTIFY with type BN_CLICKED and the identifier of the OK button.

+1
source

All Articles