Pyroesque
Pyroesque

Reputation: 143

close a message box from another program using 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;                  
         }    
    }
} 

Upvotes: 9

Views: 8358

Answers (2)

dknaack
dknaack

Reputation: 60556

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

Upvotes: 13

Ben Voigt
Ben Voigt

Reputation: 283803

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

Upvotes: 1

Related Questions