Thema: Delphi anderes Programm beenden

Einzelnen Beitrag anzeigen

Gambit

Registriert seit: 28. Mai 2003
680 Beiträge
 
Delphi 7 Professional
 
#6

Re: anderes Programm beenden

  Alt 11. Feb 2005, 20:26
@Sprint:

Habe mal versucht dein Beispiel, welches du hier im Forum mal gepostet hattest auf meines umzusetzten, funzt damit aber nicht, ich muss wohl auf terminateProcess zurückgreifen und die Keule nehmen...

Hier noch Mal dein Code:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);

  function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
  begin
    TList(lParam).Add(Pointer(hWnd));
    Result := True;
  end;

var
  SI: TStartupInfo;
  PI: TProcessInformation;
  List: TList;
  ProcessId: DWORD;
  AppHWnd: HWND;
  I: Integer;
begin

  AppHWnd := 0;
  FillChar(SI, SizeOf(TStartupInfo), 0);
  SI.cb := SizeOf(TStartupInfo);
  SI.dwFlags := STARTF_USESHOWWINDOW;
  SI.wShowWindow := SW_SHOW;
  if CreateProcess(nil, 'NOTEPAD.EXE', nil, nil, False, 0, nil, nil, SI, PI) then
  begin
    WaitForInputIdle(PI.hProcess, INFINITE);
    CloseHandle(PI.hProcess);
    CloseHandle(PI.hThread);
    List := TList.Create;
    try
      if EnumWindows(@EnumWindowsProc, Longint(List)) then
      begin
        for I := 0 to List.Count - 1 do
          if GetWindowThreadProcessId(HWND(List.Items[I]), @ProcessId) <> 0 then
            if ProcessId = PI.dwProcessId then
            begin
              AppHWnd := HWND(List.Items[I]);
              Break;
            end;
        if IsWindow(AppHWnd) then
        begin
          ShowMessage('Fensterhandle ist $' + IntToHex(AppHWnd, 8));
          SendMessage(AppHWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
        end;
      end;
    finally
      List.Free;
    end;
  end;

end;
Gruß

Gambit
  Mit Zitat antworten Zitat