9

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;                  
         }    
    }
} 
Omar
  • 16,329
  • 10
  • 48
  • 66
Pyroesque
  • 143
  • 2
  • 6
  • 2
    @reggie: Read the question again. It sounds as if the message box is created by the VB6 compiler, and they don't know what code is making the compiler whine. – Ben Voigt Jul 30 '12 at 20:49
  • could you send an key to it? also, are you sure that whatever is bringing up the message box doesn't have a "silent" or "quiet" mode? – jglouie Jul 30 '12 at 20:50
  • I guess you could raise the click event on the ok button if you know what that event is. – Hogan Jul 30 '12 at 20:50

2 Answers2

13

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

dknaack
  • 60,192
  • 27
  • 155
  • 202
  • The SC_CLOSE message will be the equivalent of clicking the "X" on the top right hand corner of the window. Note that some windows don't have an "X" - so if SC_CLOSE doesn't work, instead try Ben Voigt's solution below telling the user the default button has been clicked. – Ted Spence Jul 30 '12 at 20:57
  • Yes, but in this case it should work. Ben should tell the OP how he can find out the ID of the OK Button. A sample would be interesting. – dknaack Jul 30 '12 at 21:00
  • Agreed - it's been so long since I wrote Petzold-C that I can't remember the solution. Is it this? http://www.codeproject.com/Articles/11262/Enumerate-Controls-In-a-Dialog-Box-or-FormView – Ted Spence Jul 30 '12 at 21:08
  • Its long time ago for me too. The link looks good, but its c not c#. – dknaack Jul 30 '12 at 21:10
  • _DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lp1, string lp2);_ – Narayan May 24 '13 at 06:58
  • If title of dialog the same with title of App (Ex: Microsoft Excel) it will close app. – D T Mar 21 '19 at 05:06
1

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

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720