Получение текста из диалогового окна, которое не использует элемент управления надписью?
Это продолжение моего предыдущего вопроса. Как закрыть диалоговое окно, отображаемое Inproc COM Server.
Фон:
Резюме моей ситуации: у меня есть Inproc COM-сервер, написанный на Delphi от третьего лица. Одна из функций, которые я вызываю, отображает диалоговое окно сообщения об ошибке, если оно перехватывает определенный тип ошибки. Проблема в том, что я пытаюсь обработать данные в большом количестве, и источник данных, который я использую, вызывает появление этого диалогового окна с ошибками (благодаря ответу на мой предыдущий вопрос оно теперь автоматически закрывается, и я смог запустить его в В завершение было бы показано диалоговое окно и требовалось, чтобы кто-то нажал OK 9923 раза). Процесс блокируется до закрытия окна сообщения.
Вопрос:
Я хотел бы иметь лучшую регистрацию того, что сказал диалог ошибки. Однако любая попытка получить основной текст диалогового окна не удалась.
//Snip
private void StartWindowListener()
{
//Queue the watcher on the message pump if we are not watching.
if (_watcherRunning == false)
{
_watcherRunning = true;
_dummyForm.BeginInvoke(new Action(() =>
{
_watcherRunning = false;
//If we are not inside the com object don't enumerate.
if (_insideCom == false) return;
// Enumerate windows to find dialogs
EnumThreadWndProc callback = new EnumThreadWndProc(CheckWindow);
EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero);
GC.KeepAlive(callback);
}));
}
}
private bool CheckWindow(IntPtr hWnd, IntPtr lp)
{
// Checks if hWnd is the expected dialog
StringBuilder sb = new StringBuilder(260);
GetClassName(hWnd, sb, sb.Capacity);
if (sb.ToString() == "TMessageForm")
{
//This returns the dialog box's title
GetWindowText(hWnd, sb, sb.Capacity);
//This returns IntPtr.Zero
var hDialogText = GetDlgItem(hWnd, 0xFFFF);
if (hDialogText != IntPtr.Zero)
GetWindowText(hDialogText, sb, sb.Capacity);
//This returns a empty string
GetDlgItemText(hWnd, 0xFFFF, sb, sb.Capacity);
//Only sees the OK button.
IntPtr hCtl = IntPtr.Zero;
HashSet<IntPtr> seen = new HashSet<IntPtr>();
while ((hCtl = GetNextDlgGroupItem(hWnd, hCtl, false)) != IntPtr.Zero)
{
//When we see the same control twice, break out of the loop.
if (seen.Add(hCtl) == false)
break;
GetClassName(hCtl, sb, sb.Capacity);
SendMessage(hCtl, WM_GETTEXT, sb.Capacity, sb)
//Close the dialog by sending WM_CLOSE to the window
SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
//Snip...
}
return true;
}
//Snip...
// P/Invoke declarations
const int WM_CLOSE = 0x0010;
private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
Я подумал, что мог прервать диалог, прежде чем он добавит к нему текст (он еще не полностью закрашен, когда я нарушаю приведенный выше код). Однако положить Application.DoEvents
внутри StartWindowListener до его запуска перечисление позволяет диалоговому окну полностью рисовать, но я по-прежнему получаю те же результаты, что и опубликованные с приведенным выше кодом.
Выполнение Ctrl-C работает правильно в диалоговом окне, поэтому я мог бы использовать это в крайнем случае, но, поскольку я должен повторить это 9923 раза, я бы хотел избежать использования этого программно.
Есть ли другие способы, которые я могу попытаться получить текст из окна сообщения?
1 ответ
Благодаря комментарию Sertac я обнаружил, что текст в окнах сообщений Delphi не является объектами окна, они рисуются с помощью методов DrawText. Я использовал EasyHook для перехвата вызовов Windows API, и теперь я могу получить текст, который мне небезразличен.
////It appears that DrawText always calls DrawTextEx so it is getting intercepted twice.
//// Only need to hook DrawTextEx
static EasyHook.LocalHook _drawTextExAHook;
//Snip...
public override void Run()
{
//Snip...
IntPtr drawTextExAPtr = EasyHook.LocalHook.GetProcAddress("user32", "DrawTextExA");
_drawTextExAHook = EasyHook.LocalHook.Create(drawTextExAPtr, new DrawTextExDelegate(DrawTextEx_Hooked), null);
//The COM stuff must be run in a STA Thread so we can intercept the message boxes that it throws up.
var staThread = new Thread(() =>
{
try
{
var threadID = new[] { GetCurrentThreadId() };
//Enable the hook on the current thread.
_drawTextExAHook.ThreadACL.SetInclusiveACL(threadID);
//Tell the dummy form to start ComThread
_dummyForm = new DummyForm(ComThread);
Application.Run(_dummyForm);
}
finally
{
if(_drawTextExAHook != null)
_drawTextExAHook.Dispose();
}
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Name = "Com Thread";
staThread.Start();
//Wait for the Com Thread to finish.
staThread.Join();
}
//Snip...
private delegate int DrawTextExDelegate(IntPtr hdc, string lpchText, int cchText,
ref Rect lprc, uint dwDTFormat, ref DRAWTEXTPARAMS lpDTParams);
private int DrawTextEx_Hooked(IntPtr hdc, string lpchText, int cchText, ref Rect lprc,
uint dwDTFormat, ref DRAWTEXTPARAMS lpDTParams)
{
LogErrorText(lpchText);
return DrawTextEx(hdc, lpchText, cchText, ref lprc, dwDTFormat, ref lpDTParams);
}
[DllImport("user32.dll")]
static extern int DrawTextEx(IntPtr hdc, string lpchText, int cchText,
ref Rect lprc, uint dwDTFormat, ref DRAWTEXTPARAMS lpDTParams);