Posted by webmaster Guido on January 14, 2009
In Reply to Terminating external application posted by Thomas L on
January 14, 2009
: I have been successfully using ShellExecute to launch a separate exe in my delphi code (a movie player), but don't have as good a solution for terminating the player.
Closing (stopping) an application can be done by closing its main window. That's done by sending a
close-message to that window:
PostMessage(WindowHandle, WM_CLOSE, 0, 0)
way to get WindowHandle is with:
If you know the text in the titlebar of the application that you want to close, you can try the following Delphi code example:
procedure TForm1.CloseAppTitle(WinTitle: string); var H: HWND; begin H := FindWindow(nil, PChar(WinTitle)); if H 0 then PostMessage(H, WM_CLOSE, 0, 0); end;
For example, if you want to stop the program 'Calculator', you would write:
If you don't know the title of the window, maybe you know the window class name. Then try this code:
procedure TForm1.CloseAppClassName(CName: string); var H: HWND; begin H := FindWindow(PChar(CName), nil); if H 0 then PostMessage(H, WM_CLOSE, 0, 0); end;
For example, for closing 'Internet Explorer', of which we know that the class name of its main window
There are utilities to get the class names, such as WinDowse, WinSpector Spy, and others.
Use of WinDowse:
- start this utility and select the tab "Class";
- next, move your mouse cursor over the title bar of running applications and watch WinDowse :)
Post a Followup
DelphiLand Club members: enter your Membership password.
Guests can get a Forum Guest password by subscribing to the DelphiLand Newsletter.