Autor Beitrag
renekr
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Win 2000 ,Win XP, Win 7 64Bit
Delphi 2007 Ent,VBA,C#,ASP.net,VS2009Pro,VS2010 RC
BeitragVerfasst: So 20.08.06 15:18 
Hallo,

Ich habe ien kleines Problem.
Ich habe in meinem Programm eine Function KillTask die per Filename externe Applications beendet.
Nun geht die nicht unter Win NT und auch andere wo ich getestet habe .

Hat jemand ev. etwas passendes?

Danke
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 20.08.06 16:12 
user profile iconrenekr hat folgendes geschrieben:
Nun geht die nicht unter Win NT und auch andere wo ich getestet habe .

Fehlermeldung? Quellcode? Ansonsten kann man dir wohl nicht großartig helfen bei deinem Problem.
renekr Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Win 2000 ,Win XP, Win 7 64Bit
Delphi 2007 Ent,VBA,C#,ASP.net,VS2009Pro,VS2010 RC
BeitragVerfasst: Mo 21.08.06 10:31 
Hallo,
hier meine Function die ich verwende:
W2K +XP super ohne Probleme.

ausblenden 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:
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;
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 21.08.06 10:42 
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:
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;

Funktioniert: Windows XP SP2

Was für Prozesses kannst du denn nicht beenden? Gehören dir die Prozesse überhaupt? Prozesse eines anderen Benutzers kann man nämlich nicht beenden.

Kann es sein, dass hier seit Jahren der gleiche unsinnige Code kopiert wird? Oder warum sehe ich jedes mal diesen überflüssigen Cast von ContiunueLoop auf Integer und dann der Vergleich auf ungleich null? :?

Ich würde es so machen?
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:
uses
  Tlhelp32;

function KillTask(ExeFileName: string): Boolean;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop      : BOOL;
  FSnapshotHandle   : THandle;
  FProcessEntry32   : TProcessEntry32;
begin
  Result := False;
  ZeroMemory(@FprocessEntry32, sizeof(TProcessEntry32));
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if FSnapshotHandle <> INVALID_HANDLE_VALUE then
  begin
    FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
    ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
    while ContinueLoop do
    begin
      if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
        UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
        UpperCase(ExeFileName))) then
        Result := TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, FProcessEntry32.th32ProcessID), 0);
      ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
    end;
    CloseHandle(FSnapshotHandle);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not KillTask('notepad.exe'then
    ShowMessage(SysErrorMessage(GetLastError));
end;
renekr Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Win 2000 ,Win XP, Win 7 64Bit
Delphi 2007 Ent,VBA,C#,ASP.net,VS2009Pro,VS2010 RC
BeitragVerfasst: Mo 21.08.06 11:50 
Hi,
XP Geht klaro.
Aber Win NT 4 nicht !!!
Das ist mein Problem.

Danke.
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mo 21.08.06 13:04 
Kann man hier nicht:
ausblenden Delphi-Quelltext
1:
ShowMessage(SysErrorMessage(GetLastError));					

benutzen? :gruebel:
Vielleicht willst du als eingeschränkter User ja auch Dienste beenden, was nicht möglich ist.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19315
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 21.08.06 15:17 
Ähh, unter Win NT 4 gibts CreateToolhelp32Snapshot doch noch gar nicht...
Und das steht im Internet auch in allen Referenzen (MSDN, etc.)...
Da steht nur Windows 95, 98, ME, 2000, XP, 2003...
renekr Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Win 2000 ,Win XP, Win 7 64Bit
Delphi 2007 Ent,VBA,C#,ASP.net,VS2009Pro,VS2010 RC
BeitragVerfasst: Mo 21.08.06 17:14 
Hi,
sowas war mir fast bewusst.
Fehler kommt nciht nur er beendet das Programm nciht.
Es ist kien Dienst,ich bin Admin auf der Ksite also alle Variablen ausgeschlossen.

Wäre euch dankbar wenn ich eine Funktionierende Version für Nt bekommen könnte,ich habe schon alles für mich mögliche getestet.

Danke
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mo 21.08.06 18:53 
Probiere bitte trotzdem mal meine Codezeile aus und schreib mal, was du als Ausgabe erhälst. ;)

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
renekr Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Win 2000 ,Win XP, Win 7 64Bit
Delphi 2007 Ent,VBA,C#,ASP.net,VS2009Pro,VS2010 RC
BeitragVerfasst: Mo 21.08.06 19:03 
Hi,
"Die Zugriffsnummer ist nicht definiert "
Sowohl die exe auf als auch wenn diese zu ist.


Hoffe das hilft dir.

Danke
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 21.08.06 23:24 
Das liegt eben daran, dass es die Funktion unter Windows NT nicht gibt. Unter NT musst du mit EnumProcesses arbeiten.
renekr Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Win 2000 ,Win XP, Win 7 64Bit
Delphi 2007 Ent,VBA,C#,ASP.net,VS2009Pro,VS2010 RC
BeitragVerfasst: Di 22.08.06 07:32 
Hi@ ,

Also ich habe mal gesucht und gefunden :

sogar heir im Forum :
[url]
www.delphi-forum.de/viewtopic.php?t=16042
[/url]


Danke