Mega Code Archive

 
Categories / Delphi / Examples
 

How do I close any application

Title: How do I close any application? Question: How do I close any application? Answer: Here's the sure-fire way to close any application: Procedure KillHandle(hwnd : THandle); begin PostMessage(hwnd, WM_CLOSE, 0, 0); end; Now for the word of caution: This procedure terminates any windowed object. I.e. applications, forms, memo's, ect. So if you give it a windows handle, and nothing seamed to close, you probally just closed a controll on some form (which may or may not be hidden) Just make sure that your sending it a valid application handle, and it will close. Also note, this isn't nessicarily the fastest thing in the world. PostMessage sends a message to the window with 'hwnd' as it's handle. PostMessage dosn't wait for the application to reply to it's message ( unlike SendMessage, which waits for a return message, and can hang both applications). Take a look at EnumWindows and GetWindowLong in the SDK on how to get a applications Handle. You can get the handle of your application with application.handle;