Autor Beitrag
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: So 29.09.02 16:00 
Hi,
ich habe ein Fensterhandle eines Programms ermittelt und brauche nun aber auch den EXE-Dateinamen des Programms. Wie kann ich dies bewerkstelligen?

maxk

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 29.09.02 19:43 
ausblenden Quelltext
1:
2:
// alter Unit-Code entfernt um das Posting klein
// zu halten und Peinlichkeiten zu verstecken ;o)

Erfolgreich getestet unter 98, NT, 2000, & XP.
Mit Dank an NicoDE für sachkundige Hilfe.


Zuletzt bearbeitet von MathiasSimmack am Mo 30.09.02 11:19, insgesamt 1-mal bearbeitet
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: So 29.09.02 19:51 
Unter Windows 2000 brauchst du dir gar nicht mühsam alle Prozesse auflisten sondern kannst einfach GetWindowThreadProcessID verwenden..!

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 30.09.02 02:21 
Nicht nur in Windows2000:
Zitat:

Minimum operating systems: Included in Windows 95, Windows NT 3.1
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 30.09.02 08:04 
Motzi hat folgendes geschrieben:
Unter Windows 2000 brauchst du dir gar nicht mühsam alle Prozesse auflisten sondern kannst einfach GetWindowThreadProcessID verwenden..!

Luckie hat folgendes geschrieben:
Nicht nur in Windows2000

Solltet ihr da was übersehen haben?

ausblenden Quelltext
1:
2:
// get process ID
GetWindowThreadProcessID(hWnd,@pid);

Steht im Unit-Code - noch vor der OS-Verzweigung. :?

Möglich, dass man die Funktion vereinfachen oder optimieren kann. Aber zumindest zeigt sie den tatsächlichen Dateinamen des jeweiligen Fenster-Handles an. Ich weiß nicht mehr, welches Programm das war - aber irgendein Prozessviewer zeigte mir Norton AntiVirus grundsätzlich als Pfad zur "explorer.exe" an.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 30.09.02 10:21 
Was ich gemeint hab.. du kannst unter Win2k gleich die ProcessID verwenden um dir mit OpenProcess und GetModuleFileNameEx den Modul-Pfad zu besorgen.

Unter Win9x gibts die Funktion GetModuleFileNameEx ja leider nicht, also kommst du dort um die Process-Auflistung per ToolHelp Funktionen nicht herum, aber unter Win2k kannst du dir das sparen..

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 30.09.02 10:36 
Bitte schön. Zur Ansicht und Kontrolle:
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:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
unit pathfinder;

interface

uses
  Windows, tlhelp32, psapi;

// find path via Handle
function GetEXEFromHandle(wnd: HWND) : string;

implementation

// 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 = 0) then 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) > 0) then
              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(integer(ContinueLoop) <> 0) do
              begin
                if(aProcessEntry32.th32ProcessID = pid) then
                  Result     := aProcessEntry32.szExeFile;
                ContinueLoop := Process32Next(hProcess,aProcessEntry32);
              end;
          finally
            CloseHandle(hProcess);
          end;
      end;
  end;
end;

end.
maxk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Di 08.10.02 11:58 
:P Danke, es funzt...

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.