Autor Beitrag
tom1266
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Sa 04.01.03 16:55 
Hallo Leute,

ich komme einfach nicht klar damit, dass es keine Möglichkeit gibt externe Programme mit Delphi zu schliessen zB Internetexplorer oder MS Outlook, hier sind wohl die Meister gefragt!!! :lol:

Über Hilfe würde ich mich freuen !


Gruß Tom
torstenheinze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 461



BeitragVerfasst: Sa 04.01.03 17:13 
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
uses  
  Tlhelp32; 

function KillTask(ExeFileName: string): integer; 
const 
  PROCESS_TERMINATE=$0001;  
var 
  ContinueLoop: BOOL; 
  FSnapshotHandle: THandle; 
  FProcessEntry32: TProcessEntry32;  
begin 
  result := 0;  

  FSnapshotHandle := CreateToolhelp32Snapshot 
                     (TH32CS_SNAPPROCESS, 0);  
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32); 
  ContinueLoop := Process32First(FSnapshotHandle,  
                                 FProcessEntry32); 

  while integer(ContinueLoop) <> 0 do 
  begin 
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = 
         UpperCase(ExeFileName)) 
     or (UpperCase(FProcessEntry32.szExeFile) = 
         UpperCase(ExeFileName))) then 
      Result := Integer(TerminateProcess(OpenProcess( 
                        PROCESS_TERMINATE, BOOL(0), 
                        FProcessEntry32.th32ProcessID), 0));  
    ContinueLoop := Process32Next(FSnapshotHandle, 
                                  FProcessEntry32); 
  end;  

  CloseHandle(FSnapshotHandle);  
end



procedure TForm1.Button1Click(Sender: TObject); 
begin 
  KillTask('notepad.exe'); 
end;


Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
torstenheinze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 461



BeitragVerfasst: Sa 04.01.03 17:14 
hiermit kann man sich processe anzeigen lassen:

Wichtig ist die Einbindung der Unit TlHelp32
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
interface 
uses 
 {...,}TLHelp32 {important !} 

// Global Variables, Globale Variablen 

VAR aSnapshotHandle : THandle; 
    aProcessEntry32 : TProcessEntry32; 

procedure TFormMain.BtnRefreshClick(Sender: TObject); 
var i       : integer; 
    bLoop   : BOOL; 
    NewItem : TListItem; 
begin 
  ListView.Items.Clear; 
  aSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
  aProcessEntry32.dwSize:=Sizeof(aProcessEntry32); 
  bLoop:=Process32First(aSnapshotHandle,aProcessEntry32); 
  while integer(bLoop) <>0 do 
  begin 
    NewItem:=ListView.Items.add; 
    NewItem.Caption:=ExtractFileName(aProcessEntry32.szExeFile); 
    NewItem.subItems.Add(IntToHex(aProcessEntry32.th32ProcessID,4)); 
    NewItem.subItems.Add(aProcessEntry32.szExeFile); 
    ContinueLoop:=Process32Next(aSnapshotHandle,aProcessEntry32); 
  end
  CloseHandle(aSnapshotHandle); 
end

procedure TFormMain.ListViewDblClick(Sender: TObject); 
var  Ret : BOOL; 
     PrID : integer; //processidentifier 
     Ph : THandle;  //processhandle 
begin 
  try 
    with ListView do 
    begin 
      if MessageDlg('Do you want to Terminate "'+ItemFocused.Caption+'"?'+^J+ 
                    'posible the system is instabile or out of'+^J+ 
                    'control......'
          mtConfirmation,[mbYes,mbNo],0)=mrYes then 
       begin 
         PrID:=StrToInt('$'+ItemFocused.SubItems[0]); 
         Ph:=OpenProcess(1,BOOL(0),PrID); 
         Ret:=TerminateProcess(Ph,0); 
         if Integer(Ret)=0 Then 
           MessageDlg('Can Not Terminate "'+ItemFocused.Caption+'"',mtInformation,[mbOk],0
         else 
           ItemFocused.Delete; 
       end
     end
   except 
   end
end

procedure TFormMain.FormCreate(Sender: TObject); 
begin 
  //Application.OnHint := DisplayHint;  //If you want/need it... 
  BtnRefreshClick(Sender); 
end;


Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
tom1266 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Sa 04.01.03 17:55 
Hallo Torsten, mit notepad klappt es super, aber mit anderen Programmen leider wieder nicht, versuchs mal mit MS Outlook.

:cry: :wink:

Gruß Tom
torstenheinze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 461



BeitragVerfasst: Sa 04.01.03 18:00 
warum denn nicht bei den anderen?
was kommt den da für ein fehler?
vieleicht darf es feine datei mit einem lehrzeichen im namen sein!
:(

tut mir leid das es nicht klappt
torstenheinze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 461



BeitragVerfasst: Sa 04.01.03 18:02 
vieleicht weil sich bei outlock die überschrift ändert, und somit auch der eintrag in dem taskmanager.
obwohl bei notpad ist das ja auch so
tom1266 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Sa 04.01.03 18:09 
Fehler kommt keiner es passiert aber nichts. Das Programm geht nicht zu.
torstenheinze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 461



BeitragVerfasst: Sa 04.01.03 18:13 
merkwürdig :!: :!: :!: :?
ich weiß leider nicht weiter
tom1266 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Sa 04.01.03 20:05 
Danke alles OK, dein Skript ist schon OK du hast mir sehr geholfen!!
Klabautermann
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Veteran
Beiträge: 6366
Erhaltene Danke: 60

Windows 7, Ubuntu
Delphi 7 Prof.
BeitragVerfasst: Sa 04.01.03 20:47 
Hallo,

bitte halte dich an die richtlinien.

AUQ! - Richtlinien hat folgendes geschrieben:
1.1 Beiträge

Bitte formuliere den Betreff Deiner Beiträge so, dass andere Mitglieder anhand dieser bereits das eigentliche Thema festmachen können. Beiträge wie etwa "Eine Anfängerfrage" oder "Weiß jemand, wie das geht?" lassen den Leser im Unklaren darüber, was das Thema der Diskussion ist. Eine Pseudocodezeile oder die Nennung des Objektes, um welches es sich in dem Beitrag handelt, helfen da schon mehr weiter.


Gruß
Klabautermann
tom1266 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Mo 06.01.03 09:30 
HAllo,

habe das Script nun mal mit NT4 probiert leider Fehlanzeige, nun ich bräuchte es für NT4, auf 2000 und XP läuft es!!

Gruß Tom
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 06.01.03 09:47 
NT kennt die Toolhelp.dll nicht. Das Entwicklerteam hat sich damals aus unerfindlichen Gründen dazu entschlossen alles neu zu schreiben und in die PSAPI.dll zu packen.

Aber Assarbad arbeitet daran, siehe hier: www.assarbad.org/de/index.shtml

Auch wenn ihr es jetzt schon gelöst habt, will ich meine Version noch mal in den Topf schmeißen. Besondere Beachtung sollte der Prozedur KillProcess mit WaitForSingleObject geschenkt werden. Aber bitte vorsicht beim Debuggen, wenn man einen Prozess erwischt, der sich nicht beenden läßt, da scheint WaitForSingleObject etwas buggy zu sein. In der fertig kompüilierten Exe geht alles wunderbar.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
uses
  tlhelp32;

{******************************************************************************}
{**                                                                          **}
{** Prozesse in Stringliste schreiben                                        **}
{**                                                                          **}
{******************************************************************************}
procedure GetProcessList(sl: TStrings);
var
  hProcSnap: THandle;
  pe32: TProcessEntry32;
begin
  { Snapshot machen *PENG* }
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap = INVALID_HANDLE_VALUE then exit;

  pe32.dwSize := SizeOf(ProcessEntry32);

  { wenn es geklappt hat }
  if Process32First(hProcSnap, pe32) = true then
    { und los geht's }
    { Process32First liefert auch schon einen Prozess, den System-Prozess }
    sl.Add(pe32.szExeFile);
    while Process32Next(hProcSnap, pe32) = true do
    begin
      sl.Add(pe32.szExeFile);
    end;
  CloseHandle(hProcSnap);
end;

{******************************************************************************}
{**                                                                          **}
{** ProzessID an Hand der Exe-Datei ermittlen                                **}
{**                                                                          **}
{******************************************************************************}
function GetProcessID(sProcName: String): Integer;
var
  hProcSnap: THandle;
  pe32: TProcessEntry32;
begin
  result := -1;
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap = INVALID_HANDLE_VALUE then exit;

  pe32.dwSize := SizeOf(ProcessEntry32);

  { wenn es geklappt hat }
  if Process32First(hProcSnap, pe32) = true then
    { und los geht's: Prozess suchen}
    while Process32Next(hProcSnap, pe32) = true do
    begin
      if pos(sProcName, pe32.szExeFile) <> 0then
        result := pe32.th32ProcessID;
    end;
end;

{******************************************************************************}
{**                                                                          **}
{** Prozess beenden                                                          **}
{**                                                                          **}
{******************************************************************************}
procedure KillProcess(dwProcID: DWORD);
var
  hProcess : Cardinal;
  dw       : DWORD;
begin
  { open the process and store the process-handle }
  hProcess := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, dwProcID);
  { kill it }
  TerminateProcess(hProcess, 0);
  { TerminateProcess returns immediately, so wie have to verify the result via
    WaitForSingleObject }

  dw := WaitForSingleObject(hProcess, 5000);
  case dw of
    { everythings's all right, we killed the process }
    WAIT_OBJECT_0: Messagebox(Application.Handle, 'Prozess wurde beendet.''Prozess beenden',
      MB_ICONINFORMATION);
    { process could not be terminated after 5 seconds }
    WAIT_TIMEOUT:
    begin
      Messagebox(Application.Handle, 'Prozess konnte nicht innerhalb von 5 Sekunden beendet werden.',
        'Prozess beenden', MB_ICONSTOP);
      exit;
    end;
    { error in calling WaitForSingleObject }
    WAIT_FAILED:
    begin
      RaiseLastOSError;
      exit;
    end;
  end;
  { and refresh listbox contend }
  Form1.Button1Click(Form1);
end;

{******************************************************************************}
{**                                                                          **}
{** Button zum Auflisten der Prozesse und in Listbox schreiben               **}
{**                                                                          **}
{******************************************************************************}
procedure TForm1.Button1Click(Sender: TObject);
begin
  Listbox1.Clear;
  GetProcessList(Listbox1.Items);
end;

{******************************************************************************}
{**                                                                          **}
{** Button zum Prozess beenden                                               **}
{**                                                                          **}
{******************************************************************************}
procedure TForm1.Button2Click(Sender: TObject);
begin
  KillProcess(GetProcessID(Listbox1.Items.Strings[Listbox1.ItemIndex]));
end;


@thorstenheinze: Was sollen die globalen Variablen in deinem Code? :shock:

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
tom1266 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Mo 06.01.03 09:55 
Hallo Luckie, jetzt nochmal für mich als Anfänger, was kann ich tun...?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 06.01.03 10:00 
Entweder daraufwarten bis Assarbad fertig ist. (Das kann aber etwas dauern. Stell dich drauf ein, dass deine Enkel dann das Programm endlich benutzen können.) Oder selber versuchen es hinzubekommen mit der PSAPI. Delphi liefert dazu sogar eine Unit: PSAPI.pas, in der die Callback EnumProcesses drin ist. Damit sollte es also gehen.
tom1266 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Mo 06.01.03 10:04 
Danke Luckie für deine Bemühung.

Gruß Tom
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Sa 27.12.08 14:29 
mmh Ich möchte einen Pozess killen, von dem ich ein Fensterhandle weiss. Wie geht das?
[Ich weiss das der thread uralt is, aber ich wollte nich extra einen neuen erstellen...]
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 27.12.08 15:01 
Sende WM_CLOSE an das Handle, vielleicht funktioniert das bereits, das kommt darauf an von was das Handle ist.
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Sa 27.12.08 15:10 
mmh vom wmp... ja das könnte gehen... danke!
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Sa 27.12.08 19:34 
mmh nee geht doch nicht. Also es ist der WMP... Also im Prinzip will ich einfach alle laufenden WMP-Instanzen killen. Geht das irgendwie einfach?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 27.12.08 19:37 
Dann nimm doch einfach den fertigen Code dafür, den user profile iconLuckie oben gepostet hat: :nixweiss:
www.delphi-forum.de/...ic.php?p=27892#27892
Schließlich brauchst du dann das Fensterhandle ja nicht, wenn es um alle geht.