Jump to content
Tuts 4 You

close window


Recommended Posts

i want to inject some code into a running process. This process have a gui and uses a window. (I know the handle of this window)


so i want to close this window, but let the process running.


i used the API DestroyWindow but get a 0x5 error ( acces denied ), because i dont know the thread which created the window. ( GetCurrentThreadId is not the answer).


so my question is do you know a way how to close/destroy a window knowing its handle, but NOT closing the process, just the window of it?



Link to comment


Closing the Window
When the user closes a window, that action triggers a sequence of window messages.
The user can close an application window by clicking the Close button, or by using a keyboard shortcut such as ALT+F4. Any of these actions causes the window to receive a WM_CLOSE message. The WM_CLOSE message gives you an opportunity to prompt the user before closing the window. If you really do want to close the window, call the DestroyWindow function. Otherwise, simply return zero from the WM_CLOSE message, and the operating system will ignore the message and not destroy the window.
Here is an example of how a program might handle WM_CLOSE.
case WM_CLOSE:
if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK)
// Else: User canceled. Do nothing.
return 0; In this example, the MessageBox function shows a modal dialog that contains OK and Cancel buttons. If the user clicks OK, the program calls DestroyWindow. Otherwise, if the user clicks Cancel, the call to DestroyWindow is skipped, and the window remains open. In either case, return zero to indicate that you handled the message.
If you want to close the window without prompting the user, you could simply call DestroyWindow without the call to MessageBox. However, there is a shortcut in this case. Recall that DefWindowProc executes the default action for any window message. In the case of WM_CLOSE, DefWindowProc automatically calls DestroyWindow. That means if you ignore the WM_CLOSE message in your switch statement, the window is destroyed by default.
When a window is about to be destroyed, it receives a WM_DESTROY message. This message is sent after the window is removed from the screen, but before the destruction occurs (in particular, before any child windows are destroyed).
In your main application window, you will typically respond to WM_DESTROY by calling PostQuitMessage. case WM_DESTROY:
return 0;
We saw in the Window Messages section that PostQuitMessage puts a WM_QUIT message on the message queue, causing the message loop to end.
Here is a flow chart showing the typical way to process WM_CLOSE and WM_DESTROY messages:



Flow chart showing how to handle WM_CLOSE and WM_DESTROY messages

Edited by Dreamer
Link to comment

thank you but already tried both ways, but result is the same; closing/terminating the process, so that is not what i want

Link to comment


You could try:

HWND hwndDlg; //your window handleEnableWindow(hwndDlg, FALSE);ShowWindow(hwndDlg, SW_HIDE);
This will keep the window running in the background, but it will be hidden and no input will be possible.


  • Like 1
Link to comment
Hadits follower

u can see  this video 


it will give u example.

Edited by Death
Link to comment
Hadits follower

i know a process window can terminate disable enable suspend kill or pause or like this  .



delete the window

Then inject code , nice question . xd

Edited by Death
Link to comment
Teddy Rogers

If you destroy the window and the program is still outputting to it (or possibly waiting for input) it's likely always going to crash. The safest option would be to hide the window and then at least the program still has a window to communicate with. After it has been hidden you can delete the tab from the taskbar with ITaskBarList and DeleteTab...





  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...