Do you know a clean way to close a window given

1- handle.
2- classe name.
3- ProcessID.
4- ThreadID.

thanks all .
Posted on 2002-05-25 05:16:09 by Sa6ry
SendMessage with WM_CLOSE + handle?
Posted on 2002-05-25 05:19:30 by bdjames
Sending WM_CLOSE is sometimes helpless, imagine sending WM_CLOSE to Notepad with an unsaved document open.

In my opinion, if you interfere with other process's window it can't be guaranteed clean.
Posted on 2002-05-25 05:57:51 by C.Z.
Aslo WM_CLOSE will cause error if the window has child .
Posted on 2002-05-25 07:29:54 by Sa6ry

Aslo WM_CLOSE will cause error if the window has child .


Why??????? :confused:
Posted on 2002-05-25 08:11:03 by C.Z.
Posted on 2002-05-25 08:35:40 by smurf
may be i discribed it wrong .

but this is my notice.

if u open the browser , then file->open

leave the dialog opened .

then send WM_CLOSE , you will get error.

i think it is the same with Notepade .. etc

may be it is not because of the child window,

maybe this is beacuse the main windows lost the focus ?
Posted on 2002-05-25 08:36:13 by Sa6ry
If u know the PID u can do the following :

;==============================================
KillTask proc PID:dword
local hSnapshot:dword
local pe:PROCESSENTRY32
local hProc:dword

invoke CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0
.if eax==-1
;HANDLE ERROR
ret
.endif
mov hSnapshot,eax

invoke Process32First,hSnapshot,addr pe
@@:
cmp eax,0
je Done

cmp PID,pe.th32ProcessID
je Kill

invoke Process32Next,hSnapshot,addr pe
jmp @B
Kill:
invoke OpenProcess,PROCESS_TERMINATE,0,PID
.if eax==NULL
;HANDLE ERROR
.else
mov hProc,eax
invoke TerminateProcess,hProc,0
.if eax==0
;HANDLE ERROR
.else
;OK
.endif
.endif


Done:

ret
KillTask endp
;==============================================

Hope it is correct. I wrote it on the fly. Anyway u can make an idea.
Posted on 2002-05-25 09:20:20 by ViperV`
this is very simple. open your browser again and click file-->open then leave the dialog open. you a simple utility such as the one iczelion's creates with his hook tutorial ( http://spiff.tripnet.se/~iczelion/files/tut24.zip ) to determine the handle to the dialog window. the handle is displayed when you move the mouse over the title bar. the handle will be something like 1803763 but yours will be different.

to test closing the dialog create a small app. you can use the following code:
.586

.model flat, stdcall
option casemap:none

include /masm32/include/windows.inc
include /masm32/include/kernel32.inc
include /masm32/include/user32.inc
includelib /masm32/lib/kernel32.lib
includelib /masm32/lib/user32.lib

.code
start:
invoke SendMessage,**place your handle here***,WM_CLOSE,0,0

end start


then run this code and it will close the dialog. to make sure it was completely closed properly you need to following the instructions with the link in my last post.
Posted on 2002-05-25 11:39:55 by smurf
hello smurf,

i have tried your example .

it work . the dialog has been closed if i sent message to it

it didn't work if i send the message to the browser ( main window). i only got ding sound

so u mean to cleany close a window . you have to search for its child and close them one by one ???
Posted on 2002-05-25 11:54:57 by Sa6ry
use PostMessage instead of SendMessage.
Posted on 2002-05-25 13:02:40 by smurf
invoke findwindow, ByClassName1, 0
mov myhand, eax
invoke SendMessage, offset myhand,WM_CLOSE,0,0

This is the way that i have always done it but for the hard to close windows or whatever i think **ViperV** is on to something. Process32 SnapShot used right will shut down **ANYTHING**

Question: to ViperV or someone who know , will Process32 work on 2000 and XP also....
Posted on 2002-05-25 13:15:59 by cmax

invoke findwindow, ByClassName1, 0
mov myhand, eax
invoke SendMessage, offset myhand,WM_CLOSE,0,0


Why *offset* myhand? You want the window handle, not the address of the window handle. Better still, use eax directly:


invoke FindWindow, addr className, NULL
invoke SendMessage, eax, WM_CLOSE, NULL, NULL


Question: to ViperV or someone who know , will Process32 work on 2000 and XP also....


TerminateProcess will work for normal processes (as long as you have admin priviledges), only some processes (especially services) can't be shut down unless your program got debug priviliges. This can be set though using OpenThreadToken/AdjustTokenPrivileges.

Thomas
Posted on 2002-05-25 14:10:41 by Thomas
Thanks Mr. Thomas,

I got a LOT of dumb stuff that can be avoided by using eax directly. I will fix it all up today... The only thing i knew about was offset, offset and more offset.... What was i thinking....
Posted on 2002-05-25 14:28:01 by cmax
thomas,

works on 2k dont know about xp.
Posted on 2002-05-25 14:35:05 by smurf

use PostMessage instead of SendMessage.


The same result ding sound :(

here is my source code. it will get the defualt browser from the
registry and then close it. you can run it and test if you have
time.

you can download it from here
http://sa6ry.tripod.com/CloseWindow.zip 3.3 Kb

Just copy it in your clipboard and paste it in the browser window, tripod doesn't allow to link to files from outside.
so just select copy paste

You will notice that it work fine but if you open a browser window then . File -> Open . it will not close .

i have tried also.
invoke PostMessage, hwnd, WM_NCDESTROY, NULL, NULL
and it work even if File -> open.

but it is unclean . it doesn't release the memory.
Posted on 2002-05-25 17:11:55 by Sa6ry
your link to tripod doesnt work. you can attach code to a post by using the browse button at the bottom.
Posted on 2002-05-25 18:52:43 by smurf
thanks smurf for the browse hint .
i search for it before but i didnt find it !
i start to think now i'm blind :o


attached code that read the default browser from the registry then close all the windows of this browser .


btw tripod link in working but u have to copy and paste it in the browser.

NOT Clinking on it.

just copy the link and paste in your browser
Posted on 2002-05-25 19:05:50 by Sa6ry
ok i coded this up and it works but i didnt have any other browser installed so i couldnt test it on whether it works when the registry changed when a new default browser is used. but i dont see why it wouldnt so here you go. i didnt comment but if you dont understand what i did i can comment it for you.

there was a quicker way of doing this but it involved terminating the process instead of closing it which might not be too nice when cleaning things up.

gets default browser from the registry and closes all instances
.686 

.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
include \masm32\include\advapi32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\advapi32.lib

.data
subkey db "htmlfile\shell\open\command",0
size1 dd 128

.data?
hSnapshot HANDLE ?
hregkey HANDLE ?
bFileName db 32 dup (?)
DBrwoserName db 256 dup (?)
datatype dw ?
processInfo PROCESSENTRY32 <>

.code
start:

invoke RegOpenKeyEx,HKEY_CLASSES_ROOT,addr subkey,0,KEY_ALL_ACCESS,addr hregkey
invoke RegQueryValue,hregkey,NULL,addr DBrwoserName,addr size1
invoke RegCloseKey, hregkey

mov ecx,size1
.while DBrwoserName[ecx] != '"' || ecx == 0
dec ecx
.endw
mov DBrwoserName[ecx],0

invoke NameFromPath,addr DBrwoserName,addr bFileName

invoke CreateToolhelp32Snapshot, TH32CS_SNAPPROCESS, 0
mov hSnapshot,eax
mov processInfo.dwSize, sizeof PROCESSENTRY32
@@:
invoke Process32Next, hSnapshot, addr processInfo
.if eax == FALSE
jmp @end
.else
invoke lstrcmpi, addr processInfo.szExeFile, addr bFileName
.if eax == 0
mov ecx,EnumWindowsProc
invoke EnumWindows,ecx,processInfo.th32ProcessID
jmp @B
.else
jmp @B
.endif
.endif
@end:
invoke CloseHandle, hSnapshot
invoke ExitProcess, 0

;######################################
EnumWindowsProc PROC hwnd:DWORD,processId:DWORD
local wProcessId:dword

invoke GetWindowThreadProcessId,hwnd,addr wProcessId
mov ecx,wProcessId
.if ecx == processId
invoke PostMessage,hwnd,WM_CLOSE,0,0
.endif

ret TRUE
EnumWindowsProc ENDP
;######################################

end start
Posted on 2002-05-26 13:18:00 by smurf

ok i coded this up and it works but i didnt have any other browser installed so i couldnt test it on whether it works when the registry changed when a new default browser is used. but i dont see why it wouldnt so here you go. i didnt comment but if you dont understand what i did i can comment it for you.

there was a quicker way of doing this but it involved terminating the process instead of closing it which might not be too nice when cleaning things up.

gets default browser from the registry and closes all instances


Coded this UP ???? how come ?
Sorry i cann't figure WHAT IS THE difference between your code and the code i just sent before you ???

Sorry, but i think u stealed my Code . :confused:
Posted on 2002-05-26 22:48:21 by Sa6ry