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>
in the main
HWND wh = FindWindow("Minesweeper", "Minesweeper");
CloseHandle (wh);
On the printf
of wh
I see the value is identical to this raised in spy++.
And I'm getting the error
"Exception address: 0x7c90e4ff"
What am I missing?
BTW: Closing Handle of a process works fine if I change the two lines above.
To close your current window using JS, do this. First open the current window to trick your current tab into thinking it has been opened by a script. Then close it using window. close().
open("URL_HERE", "my_window", "height=100,width=100"); and we need to close the second page automatically after login success. Okay so in the popup use window. close() to close the window like I mentoined in my example.
A handle is a unique identifier for an object managed by Windows. It's like a pointer, but not a pointer in the sence that it's not an address that could be dereferenced by user code to gain access to some data.
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
.
However, the documentation for DestroyWindow
states:
A thread cannot use DestroyWindow to destroy a window created by a different thread.
So you can't do that either.
What you can do is to send a WM_CLOSE
message to the window. That should be enough to persuade it to close gracefully.
Note that WM_CLOSE
is sent rather than posted. This can be discerned by this line from the documentation:
A window receives this message through its WindowProc function.
Update
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.
John's advice is:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With