尽管窗口已经关闭,FindWindow 不返回 IntPtr.Zero

发布于 2024-09-25 02:08:22 字数 719 浏览 9 评论 0原文

我正在尝试从我的 C# 程序控制 Java 应用程序。在程序开始输入数据之前,它会检查是否有任何先前的数据输入窗口,并尝试关闭它们。

检查窗口是否存在的代码是:

public static bool WindowExists(string WindowName)
{
    IntPtr hWnd = FindWindow(null, WindowName);
    return (hWnd != IntPtr.Zero);
}

到目前为止,我只是在 while 循环中使用此方法(向窗口发送 Alt+F4),直到没有打开的输入窗口为止。

不久前,Java 应用程序进行了更新,此方法不再有效。由于某种原因,即使数据输入窗口关闭后,WindowExists 仍不断返回 true。仅当输入窗口至少存在一次时才会发生这种情况。

我什至尝试将 Thread.Sleep() 添加到循环中,以确保这不是由 Java 应用程序中的某些延迟引起的,但它不起作用。

有时,简单地调用 WindowExists 就会使输入窗口崩溃。

代码没有问题,因为之前是可以运行的,而且在其他程序中仍然可以运行,所以肯定是Java程序中更新的问题。

有没有其他/更可靠的方法来检查窗口是否存在? 将发送 Alt+F4 更改为“关闭窗口事件”可能也值得一试,但我不知道如何将此事件发送到另一个程序的窗口。

我是新手,所以请保持简单的答案。

提前致谢。

I'm trying to control a Java application from my C# program. Before the program begins to input data, it checks if there are any pervious data input windows, and tries to close them.

The code for checking if a window exists is:

public static bool WindowExists(string WindowName)
{
    IntPtr hWnd = FindWindow(null, WindowName);
    return (hWnd != IntPtr.Zero);
}

Until now, I simply used this method in a while loop (sending Alt+F4 to the windows), until there was no open input window left.

A while ago the Java application got an update, and this method no longer works. For some reason, WindowExists keeps returning true, even after the data input window is closed. This only happens if the input window existed at least once.

I even tried to add Thread.Sleep() to the loop, to make sure this isn't caused by some latency in the Java app, but it didn't work.

Sometimes simply calling WindowExists crashes the input window.

There's no problem with the code, because it has worked before, and it still works with other programs, so it must be a problem with the update in the Java program.

Is there any alternative/more reliable way to check if a window exists?
Changing sending Alt+F4 to "close window event" might also worth a try, but I have no idea how to send this event to another program's window.

I'm a newbie, so please keep the answer simple.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫九 2024-10-02 02:08:22

我会使用 Spy++ 来观看窗口句柄Java 应用程序的,看看你是否能弄清楚还发生了什么 - 我同意必须有一种方法来告诉它已关闭。

我认为观看进程列表是不可能的......

I would use Spy++ to watch the window handle of the Java app, and see if you can figure out what else is going on - I agree there has to be a way to tell that it is closed.

I assume watching the process list is out of the question...

愚人国度 2024-10-02 02:08:22

我大胆猜测,当 Java 应用程序以及 JVM 运行时,窗口的“句柄”尚未被垃圾收集,因此对于底层指针机制来说似乎仍然有效。

如果是我写这些东西(并且如果我能够更改 Java 代码),我可能会添加一种查询 java 应用程序以查看其窗口是否显示的方法。
套接字接口什么的。

I would hazard a guess that whilst the Java app is running and consequently, the JVM, the 'handle' to the window has not yet been garbaged collected and as such appears to the underlying pointer mechanism as still being valid.

If it was me writing that stuff (and if I was able to change the Java code) I'd probably add a means of querying the java app to see if its windows are showing.
A sockets interface or something.

水波映月 2024-10-02 02:08:22

我的猜测是,要么该窗口尚未被 Java 代码/VM 完全处理,要么它以某种特殊方式处理 Alt+F4(即可能使其自身不可见而不是关闭)。

My guess is that either the window hasn't been completely disposed of by the Java code/VM, or it's handling Alt+F4 in some special way (i.e. maybe making itself invisible rather than closing).

骄傲 2024-10-02 02:08:22

窗口的创建/删除不受您的控制。如果您想使用您想要的相同资源可靠地检测“其他人”的存在,请让另一方明确传达这一点。

另一方可以通过创建文件、打开端口等方式来表明其存在,并且可以在退出时释放/删除/关闭它。

Creation/deletion of windows is out of your control. If you want to reliably detect the presence of 'someone else' using the same resource a you want, have that other party communicate it explicitly.

The other party may signal it's presence by creating a file, opening a port, whatever, and can release/delete/close it on it's exit.

揽月 2024-10-02 02:08:22

尝试使用 IsWindow API 对 FindWindow 返回的 hWnd 值进行额外检查。

Try to make additional check using IsWindow API, on hWnd value returned by FindWindow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文