为什么我不能关闭的窗口句柄在我的code? [英] Why can't I close the window handle in my code?
问题描述
我在我的主要这两行,但我不能在年底关闭句柄。
我试图得到一个处理窗口矿山催泪小说,之后关闭它,但它不工作。我有所有相关的,包括我所需要的。
I have these two lines in my main, but I can not close the the handle in the end. I am trying to get a handle to windows mines weeper and close it after that, but it doesn't work. And I have all the relevant includes I need.
#include <windows.h>
#include <stdio.h>
在主
HWND wh = FindWindow("Minesweeper", "Minesweeper");
CloseHandle (wh);
在的printf
WH
我看到价值是相同的这个间谍提出++。
On the printf
of wh
I see the value is identical to this raised in spy++.
和我得到的错误
异常地址:0x7c90e4ff
"Exception address: 0x7c90e4ff"
我是什么失踪?
BTW:
如果我改变上述两行的过程中关闭手柄正常工作。
BTW: Closing Handle of a process works fine if I change the two lines above.
推荐答案
有几个基本问题在这里。首先你不叫 CloseHandle的
有一个窗口句柄。它不是那种手柄。您可以使用 CloseHandle的
当你有一个 HANDLE
而是 HWND
是不是 HANDLE
。如果你想消灭你需要调用一个窗口句柄的DestroyWindow
。
There are a couple of basic problems here. First of all you don't call CloseHandle
with a window handle. It's not that kind of handle. You use CloseHandle
when you have a HANDLE
but an HWND
is not a HANDLE
. If you want to destroy a window handle you need to call DestroyWindow
.
但是,<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms632682%28v=vs.85%29.aspx\">documentation为的DestroyWindow
规定:
一个线程不能使用的DestroyWindow销毁由不同的线程创建的窗口。
A thread cannot use DestroyWindow to destroy a window created by a different thread.
所以,你不能这样做,要么。
So you can't do that either.
你可以做的是发送 WM_CLOSE
消息窗口。这应该足以说服它正常关闭。
What you can do is to send a WM_CLOSE
message to the window. That should be enough to persuade it to close gracefully.
注意 WM_CLOSE
发送,而不是公布。这可以通过这条线从<一可以看出端倪href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms632617%28v=vs.85%29.aspx\">documentation:
Note that WM_CLOSE
is sent rather than posted. This can be discerned by this line from the documentation:
一个窗口,通过它的WindowProc函数接收该消息。
A window receives this message through its WindowProc function.
更新
约翰Knoller指出,我misinter preting这是不写入涵盖其中一个应用程序尝试关闭其他应用程序的情况Windows文档。
John Knoller points out that I am misinterpreting the Windows documentation which was not written to cover the situation where one application attempts to close down another application.
约翰的建议是:
在事实上,它是个聪明的利用来发送 WM_CLOSE
到另一个进程 PostMessage的
或 SendNotifyMessage
。如果你使用 SendMessage函数
,你会如果过程是不抽的消息会被卡住。它甚至更好,使用 WM_SYSCOMMAND
/ SCCLOSE
这基本上是同样的事情点击窗口标题的关闭按钮。
In fact it's wiser to send
WM_CLOSE
to another process usingPostMessage
orSendNotifyMessage
. If you useSendMessage
, you will get stuck if the process isn't pumping messages. It's even better to useWM_SYSCOMMAND
/SCCLOSE
which is basically the same thing as clicking on the window caption's close button.
这篇关于为什么我不能关闭的窗口句柄在我的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!