Wie kann aus meinem Programm heraus eine andere Anwendung schließen?


Diesmal die 16-bit und 32-bit Version in einem:

unit CloseApp;

{ By Duncan McNiven, duncan.mcniven@lecs.inet.fi }
{ Comments by Brad Stowers, bstowers@pobox.com   }

interface

uses WinTypes;

procedure CloseAppFromInst(HInst: THandle);

implementation

uses WinProcs, Messages;

{ Callback function that has each top-level window passed to it. }
{ Return true to continue enumerating, false to stop.            }
function EnumWindowsProc(Handle: HWND; Info: Pointer): boolean;
   {$IFDEF WIN32} stdcall; {$ELSE} export; {$ENDIF}
begin
  Result := TRUE; { continue enumeration }
  { Does this app have the same instance as what we are looking for? }
{$IFDEF WIN32}
  if GetWindowLong(Handle, GWL_HINSTANCE) = LongInt(Info) then begin
{$ELSE}
  if GetWindowWord(Handle, GWW_HINSTANCE) = LongInt(Info) then begin
{$ENDIF}
    PostMessage(Handle, WM_CLOSE, 0, 0); { Close the app }
    Result := FALSE; { stop enumerating windows, we are done. }
  end;
end;

procedure CloseAppFromInst(HInst: THandle);
begin
  EnumWindows(@EnumWindowsProc, LongInt(HInst));
end;

end.
Delphi 1 Delphi 2 Delphi 3 Delphi 4
nicht getestet Version nicht betroffen getestet

siehe auch:

Wie kann ich aus meiner Anwendung heraus ein anderes Programm starten, eine Datei mit der entsprechenden Anwendung öffnen, ein E-Mail-Programm oder Webbrowser starten?
Wie kann ich aus meiner Anwendung heraus ein anderes Programm starten und auf dessen Ende warten?
Wie kann ich die Programmdatei (*.exe) einer Anwendung über deren Fensterhandle ermittteln?


Delphi FAQ