How to automate response to msgbox

I am developing a C # application to automate running an outdated VBScript (vbs) file that calls multiple VB6.exe files. The .exe files have message box pop-ups that I need to “reply” to allow the VBScript process to run unattended. The answer should be the Enter key. I have no source for the .exe files, and I don’t know exactly what they are doing. I would really appreciate any help on this ...

+1
source share
6 answers

After spending the last 2 days trying to achieve this, I finally gave up and decided on a different approach. I will interrogate the data that is sent to the external process and looks at it for conditions that cause pop-ups of the message box. Thanks to everyone who answered with the answers!

0
source

You can find AutoIt .

AutoIt v3 is a free OS-OS scripting language designed to automate the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement, and window / control controls to automate tasks in a way that is possible or reliable with other languages ​​(like VBScript and SendKeys).

-, AutoIt, . .

+2

wsh SendKeys(). , , , AppActivate() SendKeys.

, , , , , [Enter], .

+2

# - . "". , . :

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MessageBoxClicker : IDisposable {
  private Timer mTimer;

  public MessageBoxClicker() {
    mTimer = new Timer();
    mTimer.Interval = 50;
    mTimer.Enabled = true;
    mTimer.Tick += new EventHandler(findDialog);
  }

  private void findDialog(object sender, EventArgs e) {
    // Enumerate windows to find the message box
    EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
    EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero);
    GC.KeepAlive(callback);
  }

  private bool checkWindow(IntPtr hWnd, IntPtr lp) {
    // Checks if <hWnd> is a dialog
    StringBuilder sb = new StringBuilder(260);
    GetClassName(hWnd, sb, sb.Capacity);
    if (sb.ToString() != "#32770") return true;
    // Got it, send the BN_CLICKED message for the OK button
    SendMessage(hWnd, WM_COMMAND, (IntPtr)IDC_OK, IntPtr.Zero);
    // Done
    return false;
  }

  public void Dispose() {
    mTimer.Enabled = false;
  }

  // P/Invoke declarations
  private const int WM_COMMAND = 0x111;
  private const int IDC_OK = 2;
  private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
  [DllImport("user32.dll")]
  private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
  [DllImport("kernel32.dll")]
  private static extern int GetCurrentThreadId();
  [DllImport("user32.dll")]
  private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
  [DllImport("user32.dll")]
  private static extern IntPtr GetDlgItem(IntPtr hWnd, int item);
  [DllImport("user32.dll")]
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

:

private void button1_Click(object sender, EventArgs e) {
  using (new MessageBoxClicker()) {
    MessageBox.Show("gonzo");
  }
}
+1

, , SetWinEventHook PInvoke , . , . WINEVENT_OUTOFCONTEXT, , , . , , EVENT_SYSTEM_DIALOGSTART.

Once you get hwnd from the dialog box (from the event list), you can use SendMesssage with WM_COMMAND or WM_SYSCOMMAND to get rid of it.

+1
source

Using the sendkey method, pass the keyboard key value and continue.

0
source

Source: https://habr.com/ru/post/1770161/


All Articles