I have succesfully tested the following code with VB Messageboxes from a Microsoft Word template that have no notification icon. I am able to get the text and close the messagebox.
However, when the messageboxes have an icon, sb.ToString()
returns an empty string.
Also, GetWindowTextLength(txtHandle)
returns 0
.
The text handle is identified correctly because the Messageboxes closes at the SendMessage
command.
I have used Spy++ to analyze the messagebox. Here is a screenshot of the output.
From what it looks like, there are two windows with class name "Statis" and the code seems to take the first one. I need to get the "O eroare la salvarea documentului in baza de date." text.
What should I change in order to get this text?
[DllImport("user32.dll")] static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetWindowTextLength(IntPtr hWnd);
public static void KillMbox()
{
for (int h = 0; h == 0;)
{
Thread.Sleep(1000);
h = FindWindow(null, "Microsoft Word");
if (h != 0)
{
IntPtr hAsIntPtr = new IntPtr(h);
IntPtr txtHandle = FindWindowEx(hAsIntPtr, IntPtr.Zero, "Static", null);
int len = GetWindowTextLength(txtHandle);
StringBuilder sb = new StringBuilder();
GetWindowText(txtHandle, sb, len + 1);
SendMessage(h, 16, 0, 0);
MessageBox.Show(sb.ToString());
}
}
}