Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close a message box from another program using c#

Tags:

c#

Here's my problem: we have an automated build process for our product. During the compilation of one of the VB6 projects a message box is spit out that requires the user to click 'ok' before it can move on. Being an automated process this is a bad thing because it can end up sitting there for hours not moving until someone clicks ok. We've looked into the VB6 code to try and suppress the message box, but no one can seem to figure out how to right now. So as a temp fix I'm working on a program that will run in the background and when the message box pops up, closes it. So far I'm able to detect when the message pops up but I can't seem to find a function to close it properly. The program is being written in C# and I'm using the FindWindow function in user32.dll to get a pointer to the window. So far I've tried closeWindow, endDialog, and postMessage to try and close it but none of them seem to work. closeWindow just minimizes it, endDialog comes up with a bad memory exception, and postMessage does nothing. Does anyone know of any other functions that will take care of this, or any other way of going about getting rid of this message? Thanks in advance.

here's the code I have currently:

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;                  
         }    
    }
} 
like image 387
Pyroesque Avatar asked Jul 30 '12 20:07

Pyroesque


People also ask

How do I close message box?

Right-click the icon referring to the dialog box from the Windows taskbar and click “Close”.

How to close message box in c# automatically?

Close(), TaskScheduler. FromCurrentSynchronizationContext()); MessageBox. Show(w, message, caption); Based on the effect that closing the form that owns the message box will close the box as well.

How do I close a different program in C#?

You can use the methods Process. Kill or Process. CloseMainWindow to stop a process. If the app has no GUI then just call Kill, otherwise a well-designed GUI application should respond to the CloseMainWindow method and allow the user to save his work before the application closes.


2 Answers

You have to found the window, that is the first step. After you can send 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

  • Find and Close the window using WIN API
like image 179
dknaack Avatar answered Oct 06 '22 17:10

dknaack


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

like image 35
Ben Voigt Avatar answered Oct 06 '22 17:10

Ben Voigt