【发布时间】:2016-03-07 14:08:35
【问题描述】:
我在我们的应用程序中使用的系统存在问题:有时,当我们向该系统询问数据时,他会弹出一个 MessageBox 告诉我们类似以下内容的内容:“我无法检索您的数据,有太多数据要搜索”。
这样做的问题是用户可能无法看到或关闭弹出窗口,因此这会阻止整个应用程序(解释为什么用户无法关闭/看到弹出窗口会花费太多时间并且偏离主题,这很糟糕但我们必须处理它)。
所以,作为一个临时解决方案,我想阻止这个特定进程创建 MessageBox。
我在网上寻找了一个解决方案,发现 CBTProc 似乎提供了一种对特定 Windows 事件(来自进程的创建窗口的请求)作出反应并指示操作系统阻止该请求的方法。
这是要走的路吗?
我测试了SetWinEventHook 来检测请求创建窗口的进程和DestroyWindow 来销毁窗口:
public class PopupWatchdog {
#region constructor
public PopupWatchdog() {
SetWinEventHook(
EVENT_OBJECT_CREATED,
EVENT_OBJECT_CREATED,
IntPtr.Zero,
HookCallback,
0, //id process
0, //id thread
WINEVENT_OUTOFCONTEXT
);
}
#endregion
#region functions
private static void HookCallback(IntPtr hWinEventHook, int iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime) {
Console.WriteLine("window {0} requests creating an object, trying to destroy it...", idChild);
DestroyWindow(hWnd);
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, HookProc lpfnWinEventProc, int idProcess, int idThread, int dwflags);
[DllImport("user32.dll")]
private static extern bool DestroyWindow(IntPtr hWnd);
#endregion
#region events
#endregion
#region variables
#region const
private const int EVENT_OBJECT_CREATED = 0x8000;
private const int WINEVENT_OUTOFCONTEXT = 0;
#endregion
private delegate void HookProc(
IntPtr hWinEventHook,
int iEvent,
IntPtr hWnd,
int idObject,
int idChild,
int dwEventThread,
int dwmsEventTime
);
#endregion
}
DestroyWindow 不能像 msdn 文档所说的那样用于销毁由另一个线程创建的窗口,这是不稳定的。所以我的测试没有成功。我怎样才能解决这个问题?
我可能犯了错误,我不太了解 Windows api,只是听说过 CBTProc。
更新
我按照@DavidHeffernan 和@AlexK 的建议更改了代码,它可以正常工作:
public class BlockingPopupWatchdog {
#region ctor
public BlockingPopupWatchdog(int processId) {
_processId = processId;
}
#endregion
#region functions
internal bool Hook() {
if (_hookId != IntPtr.Zero) {
Unhook();
}
_hookId = SetWinEventHook(
EVENT_OBJECT_CREATED,
EVENT_OBJECT_CREATED,
IntPtr.Zero,
_hook,
_processId, //id process
0, //id thread
WINEVENT_OUTOFCONTEXT
);
if (_hookId == IntPtr.Zero) {
Logger.Log(String.Format("Error {0} while hooking", Marshal.GetLastWin32Error()), EventTypes.WARNING);
return false;
}
return true;
}
internal bool Unhook() {
if (_hookId == IntPtr.Zero) return false;
if (!UnhookWinEvent(_hookId)) {
Logger.Log(String.Format("Error {0} while unhooking", Marshal.GetLastWin32Error()), EventTypes.WARNING);
return false;
}
return true;
}
private static void HookCallback(IntPtr hWinEventHook, int iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime) {
if (hWnd == IntPtr.Zero) return;
try {
AutomationElement elem = AutomationElement.FromHandle(hWnd);
if (elem == null || !elem.Current.ClassName.Equals(MESSAGEBOX_CLASS_NAME)) {
return;
}
object pattern;
if (!elem.TryGetCurrentPattern(WindowPattern.Pattern, out pattern)) return;
WindowPattern window = (WindowPattern)pattern;
if (window.Current.WindowInteractionState == WindowInteractionState.ReadyForUserInteraction) {
window.Close();
}
} catch (Exception e) {
Console.WriteLine(e);
}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, HookProc lpfnWinEventProc, int idProcess, int idThread, int dwflags);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
#endregion
#region variables
#region const
private const String MESSAGEBOX_CLASS_NAME = "#32770";
private const int EVENT_OBJECT_CREATED = 0x8000;
private const int WINEVENT_OUTOFCONTEXT = 0;
#endregion
private delegate void HookProc(
IntPtr hWinEventHook,
int iEvent,
IntPtr hWnd,
int idObject,
int idChild,
int dwEventThread,
int dwmsEventTime
);
private static readonly HookProc _hook = HookCallback;
private IntPtr _hookId;
private readonly int _processId;
#endregion
}
【问题讨论】:
-
这段代码有不少错误。您不会进行任何错误检查,也不会通过在静态变量中保存引用来保护委托免受 GC。我认为您可以在对话框中发布
WM_QUIT消息,而不是调用DestroyWindow。 -
DestroyWindow 不合适,您可以发送 WM_CLOSE 或使用 UIAutomation,这是更好的选择,因为它也可以用于检测消息框的创建而无需挂钩。
-
@AlexK。好的!正是我的问题,谢谢!
-
对不起,你是对的。我还在看第一个代码。您的错误处理现在看起来不错。