Autor Beitrag
webmaker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 205



BeitragVerfasst: Mi 09.07.03 14:27 
Hi,

wie kann ich den Pfad zu einem laufenden Programm finden? Ich habe nur das HAndle zur Verfügung. Sollte aber ausreichen oder? Ich habe echt keine Ahnung wie das gehen soll. Könnt ihr mir vielleicht etwas Code geben? Wäre echt super von euch!!

_________________
.::Wissen ist Macht, nichts wissen macht nichts::.
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Mi 09.07.03 14:41 
Code gibt es keinen, aber einen Link zur Suchfunktion des Forums. Die Stichwörter sind Suche in: Delphi-Forum, Delphi-Library TLHELP32 und Suche in: Delphi-Forum, Delphi-Library CREATETOOLHELP32SNAPSHOT

Moderiert von user profile iconTino: DF-Tags hinzugefügt.

_________________
Ist Zeit wirklich Geld?
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 09.07.03 15:24 
Eigentlich ist der Suchbegriff nur Suche in: Delphi-Forum, Delphi-Library TLHELP32. Mit CreateToolhelp32Snapshot findet man nämlich nichts. :)
webmaker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 205



BeitragVerfasst: Do 10.07.03 16:30 
Dieser Thread hier scheint ja recht nürtzlich zu sein.

Doch habe ich noch ein kleines Problem damit... In welcher Komponente werden denn die Prozesse mit Pfad hinzugefügt? Wahrscheinlich ListView oder? Doch irgendwie will das nicht so wirklich. Er zeigt nämlich nichts an.

Könnt ihr mir nochmal helfen? Wäre super von euch!!

Moderiert von user profile iconTino: Absätze entfernt.

_________________
.::Wissen ist Macht, nichts wissen macht nichts::.
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 10.07.03 16:40 
Das könnte auch eine Listbox oder ein Memo sein. Aber ich denke mal, am Code zum Einfügen (ob nun LV, LB, Memo) wird´s doch nicht scheitern? Wenn du schon weißt, wo was eingefügt wird, dann benutz doch den richtigen Code für die von dir bevorzugte Komponente. :wink:
webmaker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 205



BeitragVerfasst: Do 10.07.03 18:49 
doch leider scheitert's daran...
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:
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;

das ist die Funktion aus dem anderen thread. Nur was soll lstProcesses sein? eine Listview? Würde ja passen. Doch fügt er nichts ein...
Woran liegt es?

Danke schon mal!

_________________
.::Wissen ist Macht, nichts wissen macht nichts::.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 10.07.03 19:28 
Ich würde da eher auf eine Listbox timmen.
webmaker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 205



BeitragVerfasst: Do 10.07.03 19:45 
dennoch klappt der Code einfach nicht.
Es wird kein einziges Item hinzugefügt. Woran kann das liegen?

_________________
.::Wissen ist Macht, nichts wissen macht nichts::.
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 10.07.03 21:46 
@Luckie, & webmaker: Anhand von "Caption" in

Zitat:
ausblenden Delphi-Quelltext
1:
2:
3:
with lstProcesses.Items.Add do begin 
  Caption := ModuleEntry.szExePath; 
end;

würde ich auf eine List-View tippen.

@webmaker: Ich habe den Code 1:1 kopiert und unter Windows 98 Erfolg gehabt. Arbeitest du zufällig mit einem NT-System (2000, XP)? Da blieb die Liste bei mir auch leer. Aber so ging es dann:
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:
var
  hProcessSnap,
  hProcess        : THandle;
  pe32            : TProcessEntry32;
  buf             : array[0..MAX_PATH]of char;
begin
  // TListview leeren
  lstprocesses.Items.Clear;
  lstprocesses.Items.BeginUpdate;


  hProcessSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  if(hProcessSnap = INVALID_HANDLE_VALUE) then exit;

  ZeroMemory(@pe32,sizeof(pe32));
  pe32.dwSize  := sizeof(TProcessEntry32);

  if(Process32First(hProcessSnap,pe32)) then begin
    repeat
      ZeroMemory(@buf,sizeof(buf));

      // Pfad unter NT herausfinden
      if(Win32Platform = VER_PLATFORM_WIN32_NT) then begin
        hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
          false,pe32.th32ProcessID);
        if(hProcess <> INVALID_HANDLE_VALUE) then
          try
            if(GetModuleFileNameEx(hProcess,0,buf,MAX_PATH) = 0then
              lstrcpy(buf,pe32.szExeFile);
          finally
            CloseHandle(hProcess);
          end;
      // unter 9x ist der Pfad schon drin
      end else
        lstrcpy(buf,pe32.szExeFile);

      // Item in die TListView
      with lstProcesses.Items.Add do
        Caption := string(buf);
    until not(Process32Next(hProcessSnap,pe32));

    CloseHandle (hProcessSnap);
  end;

  lstprocesses.Items.EndUpdate;
end;

Das funktioniert unter 98 und XP. Ich würde sagen, die Chancen für andere Win-Versionen stehen also nicht schlecht.

Um aber mal auf deine Ausgangsfrage zurückzukommen:
Zitat:
wie kann ich den Pfad zu einem laufenden Programm finden? Ich habe nur das HAndle zur Verfügung.

Was für ein Handle ist das? Das Fenster-Handle, bspw. gefunden mit findwindow? In dem Fall geht´s mit folgender Funktion relativ simpel. Die entstand vor ein paar Jahren schon mit tatkräftiger Unterstützung von Nico. (Vielleicht ein Fall für die FAQs? @Tino):
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:
uses
  Windows, tlhelp32, psapi;

// find .EXE path via Handle
function GetEXEFromHandle(wnd: HWND) : string;
var
  pid             : dword;
  wv              : TOSVersionInfo;
  hProcess        : THandle;
  ContinueLoop    : boolean;
  aProcessEntry32 : TProcessEntry32;
  buf             : array[0..MAX_PATH]of char;
begin
  // re-set
  Result := '';

  // nothing, exit
  if(wnd = 0then exit;

  // get running OS
  ZeroMemory(@wv,sizeof(TOSVersionInfo));
  wv.dwOSVersionInfoSize := sizeof(TOSVersionInfo);
  GetVersionEx(wv);

  // get process ID
  GetWindowThreadProcessID(wnd,@pid);

  // get filename
  case wv.dwPlatformId of
    VER_PLATFORM_WIN32_NT:
      begin
        hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
          false,pid);
        if(hProcess <> INVALID_HANDLE_VALUE) then
          try
            if(GetModuleFileNameEx(hProcess,0,buf,MAX_PATH) > 0then
              Result := string(buf);
          finally
            CloseHandle(hProcess);
          end;
      end;
    VER_PLATFORM_WIN32_WINDOWS:
      begin
        hProcess := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
        if(hProcess <> INVALID_HANDLE_VALUE) then
          try
            aProcessEntry32.dwSize := sizeof(aProcessEntry32);
            ContinueLoop := Process32First(hProcess,aProcessEntry32);

            while(ContinueLoop) do
              begin
                if(aProcessEntry32.th32ProcessID = pid) then
                  Result     := aProcessEntry32.szExeFile;
                ContinueLoop := Process32Next(hProcess,aProcessEntry32);
              end;
          finally
            CloseHandle(hProcess);
          end;
      end;
  end;
end;

Anwendungsbeispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button2Click(Sender: TObject);
var
  notepad : HWND;
begin
  notepad := findwindow('notepad',nil);
  if(notepad <> 0then
    ShowMessage(GetEXEFromHandle(notepad));
end;


Grüße.


Zuletzt bearbeitet von MathiasSimmack am Fr 11.07.03 07:29, insgesamt 1-mal bearbeitet
DaFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189



BeitragVerfasst: Do 10.07.03 22:22 
Hi Mathias!

Was'n das? :wink:
Hab' ich heute schon wieder zu viel Bier gesoffen, dass ich das nicht verstehe oder liege ich richtig in der Annahme, dass man dieses Konstrukt unter dem Motto "von-hinten-durch-die-Brust-ins-Auge" ansehen kann?

MathiasSimmack hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function GetEXEFromHandle(wnd: HWND) : string;
var
// ...
  ContinueLoop    : boolean;
// ...
begin
// ...
            while(integer(ContinueLoop) <> 0do
              begin
// ...



Viele Grüße,
Markus
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 11.07.03 07:36 
DaFox hat folgendes geschrieben:
Was'n das? :wink:

:oops: Na ja, ...

Zitat:
Hab' ich heute schon wieder zu viel Bier gesoffen, [...]

Why would a robot need to drink?
I don't need to drink. I can quit any time I want.

:mrgreen:
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 11.07.03 08:45 
ausblenden Delphi-Quelltext
1:
while(IntToStr(integer(ContinueLoop)) <> '0'do					

:mrgreen:

@Mathias: Mal wieder zu viel Futurama gekuckt? :roll:
webmaker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 205



BeitragVerfasst: Fr 11.07.03 10:09 
Jepp nutze WinXP
grrr....
Danke, aber beide Codes funktionieren.

_________________
.::Wissen ist Macht, nichts wissen macht nichts::.
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 11.07.03 16:04 
Luckie hat folgendes geschrieben:
@Mathias: Mal wieder zu viel Futurama gekuckt? :roll:

Here, try my Smell-o-scope.
... Man, this is great. As long as you don't make me smell Uranus.
I'm sorry, Fry. But astronomers renamed Uranus in 2620 to end that stupid joke once and for all.
What's it called now?
Urectum.


So, genug jetzt. :wink:
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mo 14.07.03 09:18 
MathiasSimmack hat folgendes geschrieben:
Vielleicht ein Fall für die FAQs? @Tino

Ist ja nur ne Frage... also auch nur ne Antwort: Ja, das wäre ein guter FAQ Beitrag. :-D

Gruß
Tino