Autor Beitrag
FD-83
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: So 02.03.03 16:18 
Ich benutze folgenden Code, um eine Liste aller zur Zeit laufenden Prozesse zu bekommen:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
uses
  tlhelp32;

procedure FillAppList(Applist: Tstrings);
Var
  Snap:THandle;
  ProcessE:TProcessEntry32;
begin
  Applist.Clear;
  Snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  ProcessE.dwSize := SizeOf(ProcessE);

  if Process32First(Snap,ProcessE) then
    begin
      Applist.Add(string(ProcessE.szExeFile));
      while Process32Next(Snap,ProcessE) do
        Applist.Add(string(ProcessE.szExeFile));
    end
  else
    ShowMessage('Fehler!');

  CloseHandle(Snap);
end;

Das funktioniert soweit auch gut! Aber ich hätte gerne den Kompletten Pfad zur EXE-Datei des Prozesses und nicht nur den Dateinamen.

Wie mache ich das am besten?

Gruss Frederik
FD-83 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: So 02.03.03 17:18 
Ach ich hab die Lösung schon sleber gefunden...

Unter win9x wird der komplette pfad zurückgegeben.

Bei Windows NT, 2000 und XP geht mit den Funktionen der psAPI.

Nun gehts...

Gruss Frederik
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 02.03.03 18:44 
Jupp. Dem ist so. Aber wenn du die PSAPI benutzt könntest du unter Win9x Probleme bekommen. Nimm für den Pfad plus Exe-Name TModuleEntry32. Ist auch in der tlhelp32 drin.
FD-83 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: So 02.03.03 22:47 
OK. Bin nun wieder Bei der tlhelp32 :).

Ich habe aus der MSDN Library folgenden Code (hab in von C++ in Delphi umgeschrieben).
Zuerst wird ein Snapshot von allen Prozessen gemacht. Dann wird zu jedem Prozess ein Snapshot der Module gemacht. Und das primäre Modul gesucht (die Exe-Datei).

ABER: Bei mir ist der Wert th32ModuleID beim TProcessEntry32 immer null! Und beim TModuleEntry immer 1!!

Folglich funktioniert das nicht. Aber warum ist dem so!?!??!

ausblenden volle Höhe 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:
function GetProcessModule(dwPID, dwModuleID: Cardinal; LPMODULEENTRY32: Pointer; cbMe32: Cardinal): boolean;
Var
  hModuleSnap: Cardinal;
  ModuleEntry: TModuleEntry32;
  bFound: Boolean;
begin
  hModuleSnap := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
  bFound := false;

  if (hModuleSnap <> INVALID_HANDLE_VALUE) then
    begin
      ModuleEntry.dwSize := sizeof(TModuleEntry32);

      if (Module32First(hModuleSnap, ModuleEntry)) then
        repeat
          if (ModuleEntry.th32ModuleID = dwModuleID) then
            begin
              CopyMemory (LPMODULEENTRY32, @ModuleEntry, cbMe32);
              bFound := true;
              break;
            end;

        until not Module32Next(hModuleSnap, ModuleEntry);

        Result := bFound;
    end
  else
    Result := false;

  CloseHandle (hModuleSnap);

end;


procedure tform1.Button1Click(Sender: TObject);
Var
  hSnapShot: Cardinal;
  ProcessEntry: TProcessEntry32;
  ModuleEntry: TModuleEntry32;
  hProcess: Cardinal;
begin
   hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   if (hSnapShot <> INVALID_HANDLE_VALUE) then
     begin
       ProcessEntry.dwSize := sizeof(TProcessEntry32);

       if (Process32First(hSnapShot, ProcessEntry)) then
         repeat
           if GetProcessModule(ProcessEntry.th32ProcessID, ProcessEntry.th32ModuleID, @ModuleEntry, sizeof(TModuleEntry32)) then
             begin
               with lstProcesses.Items.Add do begin
                 Caption := ModuleEntry.szExePath;
               end;
             end;
         until not Process32Next(hSnapShot, ProcessEntry);
       CloseHandle (hSnapShot);
     end;
end;


Gruss Frederik
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 02.03.03 23:03 
Wo ist was null bzw. eins? zeig mal die Stellen im Code bitte. Ansonsten kuck mal auf meiner Seite, da gibt es auch einen Prozess-Betrachter mit Code.
FD-83 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: So 02.03.03 23:25 
Also ich gehe Ja alle Prozesse durch... Aber via TProcessEntry32.szExeName bekomme ich ja nur den Dateinemen ohne Pfad. Daher lasse ich nun via GetToolHelpSnapshot(TH32CS_SNAPMODULE,ProzessID) alle Module des Prozesses zurückliefern. Und jetzt Vergleiche ich jede TModuleEntry32.th32ModuleID mit der th32ModuleID des Prozesses. Wenn die ID gleich ist, dass ist es das primär Modul (die Exe-Datei) und von diesem kann ich dann den Kompletten ExePath bekommen.

So ist es auch in der MSDN Library beschrieben!
Aber Komischerweise haben alle ModuleEntrys die Gleiche th32ModuleID nämlich "1"! Und Alle ProcessEntrys haben die thModuleID = 0!

Deher ist diese Bedungung niemals Erfüllt:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
if (ModuleEntry.th32ModuleID = dwModuleID) then 
            begin 
              CopyMemory (LPMODULEENTRY32, @ModuleEntry, cbMe32); 
              bFound := true; 
              break; 
            end;


(dwModuleID ist die th32ModuleID des ProcessEntrys!)

Also kutzum: die th32ModuleIDs sind falsch :( Warum Nur?

Gruss Frederik
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 02.03.03 23:37 
Bist du eventuell kein Admin?
ausblenden Quelltext
1:
hModuleSnap := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);					

Bekommst du hier ein gültiges Handle?
FD-83 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: So 02.03.03 23:40 
Doch bin Administrator!

Aber ich habe gearde gemerkt, dass ich bei Prozessen wie svchost.exe und dem Leerlaufprozess kein gültiges Handle bekomme!

Aber den "normalen" Tasks bekomme ich aber eins! Also für den Deplhi Task bekomme ich z.B. eins...

Gruss Frederik
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 02.03.03 23:46 
Hehe. Für die Dinger brauchst du Debugg-Rechte. Siehe mein Prozess-Betrachter.
FD-83 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: So 02.03.03 23:56 
Ahja, hab die Besagte stelle in deinem Programm gefunden. Nur wär es nett wenn du mir das Prinzip mal etwas erläuern könntest...

Als was ich schon Kapiert habe: Die Funktion bricht gleich ab wenn es sich um Windows9x handelt, weil da sowieso jeder alles darf :D

Aber mit meinen ModulHandles hat das wohl nichts zu tun oder vllt doch?

[edit]Achso, auch sehr interessant, dass die MSDN Libraray über diese Debug Privileges im Tollhelp Abschnitt kein Wort verliert![/edit]

Gruss Frederik
FD-83 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Mo 03.03.03 20:27 
Btw: Weiss zufällig jemand wie der Windows Task Manager von Win2000 und XP die Prozessinformationen bezieht? psAPI oder tlhelp32 ?

Gruss Frederik