阻止进程子窗口?
我正在 C# .NET 中启动一个进程,由于某种原因,该进程在最初启动时会打开一个消息框。我需要从我的应用程序中做的是找到某种方法来阻止此消息框被打开,或者基本上从我的代码中“单击”消息框上的“确定”按钮。
当我们将其作为服务运行时,消息框导致进程挂起,因此我现在需要找到某种方法来阻止该框打开或从我的代码中将其关闭(选择“确定”)。
我一直在查看一些 Win32 API 示例,但我以前从未使用过它,而且看起来有点奇怪。任何建议都会很棒!
编辑
这是作为解决该问题而提供的批处理文件。不过,如果可能的话,我更希望让它运行可执行文件。解释是使用 -supw 参数(在服务器上设置密码)会导致弹出此消息框。该批处理文件已经过测试并且可以工作,但它需要您创建一个重复的可执行文件(murmur2.exe),它是 ghetto。
set /p VAR= < superadmin.txt
start murmur2.exe -supw %var%
ping 0.0.0.0 -n 3 > NUL
tskill murmur2
murmur.exe
I am launching a process in C# .NET that for some reason opens a message box when it is initially launched. What I need to do from my application is find some way to either prevent this message box from ever being opened, or basically "click" the OK button on the message box from my code.
The message box is causing the process to hang when we run it as a service, so I now need to find some way to either prevent that box from ever opening or just close it (select OK) from my code.
I have been looking at some Win32 API samples, but I have never had to use it before and it looks a bit strange. Any suggestions would be great!
Edit
Here is a batch file that was supplied as a work around for the issue. However I would much prefer to have it running an executable if possible. The explanation was that using the -supw parameter (to set a password on the server) causes this message box to popup. This batch file is tested and works, but it requires you have a duplicated executable (murmur2.exe) created which is ghetto.
set /p VAR= < superadmin.txt
start murmur2.exe -supw %var%
ping 0.0.0.0 -n 3 > NUL
tskill murmur2
murmur.exe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PostMessage(FindWindow("#32770",*消息框标题*),WM_CLOSE,NULL,NULL);
因为可能(机会很小)同时打开具有相同类名和相同标题的同一个窗口,这不是一种万无一失的方法。然而,其他替代方案简直是矫枉过正。如果您确实想让它防失败,则必须调用
EnumWindows
来枚举所有顶级窗口,然后对于每个窗口,调用GetWindowThreadProcessId
并比较窗口的进程 ID 与您启动的进程 ID。如果相等,则可以调用GetClassName
和GetWindowText
来比较类名(对于普通对话框始终为“#32770”)和消息框的标题来验证您正在尝试关闭右侧的窗口。完成后,您可以使用找到的 hWnd 通过 WM_CLOSE 调用 PostMessage。PostMessage(FindWindow("#32770",*Title of the message box*),WM_CLOSE,NULL,NULL);
Since there might(with a very slim chance) be a same window opened with the same class name with the same title at the same time, this is not a fail proof way. However the alternatives are simply an overkill. If you really want to make it fail proof, you'll have to call
EnumWindows
to enumerate all top-level windows, then for each window, callGetWindowThreadProcessId
and compare the window's process id with your launched process' id. If they are equal, you can then callGetClassName
andGetWindowText
to compare the class names(always "#32770" for common dialogs) and the title of the message box to verify that you're trying to close the right window. Once it's done, you can call PostMessage with WM_CLOSE using the hWnd that you found.