How can I register the time when a message box from a third-party application appears?

Here is the situation:

We have a third-party application that periodically displays an error message. Usually an error occurs when no one is in the office, so we don’t know exactly what time the message box appears. We work with our supplier to determine what the problem is, and they want to know exactly when the error occurs, so we can provide them with network conditions, etc ... during the error. The error message is not logged by the application anywhere, so I was instructed to somehow register when this happens.

I have C # .NET as my tool. So far, the closest things I have found to solve are FindWindow and EnumChildWindows or are connected to Windows messages. I am a recent college grad and have just begun my work, so both of these routes will be quite difficult for me. Before investing a lot of time trying to find out what I need to try to do one of these methods, I wanted to check here and see if there is a simpler solution.

In fact, all I need to do is register when a message box appears and some identifying information about the message field. It is not necessary to register only messages from the application in question.

Thank you for your help. Please let me know if I need to clarify anything.

EDIT:

- . . , . . Notepad, findMessageBox, . EnumChildWindows EnumThreadWindows, . , PID Spy ++. , .

EDIT:

. . GetWindowProcessThreadId EnumThreadWindows. - , , , , . , , , - :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace LogDialog
{
class DialogLogger
{
    TextWriter log = new StreamWriter(@"C:\ErrorLog\ErrorLog.txt");
    private Timer mTimer;
    private uint lpdwProcessId;
    private IntPtr mhwnd;
    uint threadID;

    //*************P/Invoke Declarations*************//

    private delegate bool EnumThreadWndProc(IntPtr hwnd, IntPtr lp);
    private delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lp);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadWndProc callback, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern int GetClassName(IntPtr hwnd, StringBuilder buffer, int buflen);

    //Retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window. 
    [DllImport("user32.dll", SetLastError = true)]
    static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint lpdwProcessId);

    //***********************************************//

    //Constructor; initiates timer
    public DialogLogger(string processName)
    {
        setWindowProcessID(processName);                    //set process ID
        mTimer = new Timer();                               //create timer for logging
        mTimer.Interval = 50;                               //set interval
        mTimer.Enabled = true;                              //enable
        mTimer.Tick += new EventHandler(findMessageBox);    //set event handler
    }

    private void setWindowProcessID(string processName)
    {
        mhwnd = Process.GetProcessesByName(processName)[0].MainWindowHandle;
        threadID = GetWindowThreadProcessId(mhwnd, out lpdwProcessId);
    }

    //Enumerates windows to find a message box
    private void findMessageBox(object sender, EventArgs e)
    {
        EnumThreadWndProc callback = new EnumThreadWndProc(checkDialogWindow);
        EnumThreadWindows((int)threadID, callback, IntPtr.Zero);
        GC.KeepAlive(callback);
    }

    //Checks if hwnd is a dialog
    private bool checkDialogWindow(IntPtr hwnd, IntPtr lp)
    {
        StringBuilder sb = new StringBuilder(260);
        GetClassName(hwnd, sb, sb.Capacity);

        if (sb.ToString() != "#32770") return true;

        log.WriteLine("Error Logged: {0}", DateTime.Now.ToLongTimeString());

        return false;
    }
}
}
+3
3

, , . , , . , , , . . .

+1

: 2 ?

0

, :)

0

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


All Articles