Autor Beitrag
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 14.07.03 12:24 
Der folgende Quellcode ist eine komplette Unit. Einfach kopieren und als "pathfinder.pas" speichern und dann in den gewünschten Programmen einsetzen. Die enthaltene Funktion "GetEXEFromHandle" entstand mit tatkräftiger Unterstützung von NicoDE. Angesichts der ... äh ... Kritik (@DaFox: :wink:) habe ich sie aber noch mal ein bisschen überarbeitet.
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:
unit pathfinder;

interface

uses
  Windows;

function GetEXEFromHandle(const wnd: HWND) : string;


implementation

uses
  tlhelp32, psapi;


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

  // R U kiddin', man?
  if(wnd = 0then exit;

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

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


  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 begin
        ZeroMemory(@buf,sizeof(buf));
        lstrcpy(buf,aProcessEntry32.szExeFile);

        if(wv.dwPlatformId = VER_PLATFORM_WIN32_NT) then begin
          hp := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
            false,pid);

          if(hProcess <> INVALID_HANDLE_VALUE) then try
            ZeroMemory(@buf,sizeof(buf));
            GetModuleFileNameEx(hp,0,buf,sizeof(buf));
          finally
            CloseHandle(hp);
          end;
        end;

        break;
      end;

      ContinueLoop := Process32Next(hProcess,aProcessEntry32);
    end;
  finally
    CloseHandle(hProcess);
  end;

  Result := string(buf);
end;

end.

Anwendungsbeispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
uses
  pathfinder;

procedure TForm1.Button1Click(Sender: TObject);
var
  notepad : HWND;
begin
  notepad := findwindow('notepad',nil);
  if(notepad <> 0then
    ShowMessage(GetEXEFromHandle(notepad));

  // oder die eigene Anwendung
  // (obwohl "paramstr(0)" einfacher ist :))
  ShowMessage(GetEXEFromHandle(self.Handle));
end;

Getestet unter Windows 98 und XP. Fazit: Funktioniert. :)


Zuletzt bearbeitet von MathiasSimmack am Mo 14.07.03 15:25, insgesamt 1-mal bearbeitet