Mega Code Archive

 
Categories / Delphi / System
 

How to kill something with a handle

Title: How to kill something with a handle Question: How can I terminate a program that won''t close? How ca I get rid of that annoying watch-dog Answer: So you got something with a handle and you're mad at it. So mad you want to kill it. First of all you get it's handle ex: your own form: hWnd:= Form1.Handle for the top-most window: hWnd:= GetForegroundWindow for a specific window-class (you'll find that using WinSight): hWnd:= FindWindow(pchar('mIRC32'),nil) for a window with a specific caption: FindWindow(nil,pchar('mIRC32 - [Status]')) ... etc. Now you could try sending it a mesage: PostMessage(hWnd, WM_CLOSE, 0, 0); Or if you want to be rude and destroy it's window: PostMessage(hWnd, WM_DESTROY, 0, 0); Or terminate all running apps by logging off ExitWindowSEx(ewx_force or ewx_logoff, 0); {or even ewx_shutdown or ewx_reboot} Or a more fiendish way - terminate it's process: var wnd : hwnd; ProcessID,cpid : Cardinal; begin wnd:=form1.handle; {whatever; handle of victim window} ProcessID := GetWindowThreadProcessID(wnd,@cpid); TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,cpid),1); end; {for that rebel Form1 too} BTW: A good way to terminate/bypass a program is to prevent it from running ever again (move/rename it - if launched at startup Windows won't recognize it). That's all the sadismus for now Bogdan Grigorescu - BogdanG@gmail.com BG Remote Programming Group