1. Получаешь список хендлов, примерно так:
Код:
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 | function GetWindowClass( Handle : THandle ) : String;
var WText : PChar;
TxtBuf : String;
begin
GetMem( WText,255 );
GetClassName( Handle,WText,255 );
TxtBuf := WText;
Result := TxtBuf;
FreeMem( WText );
end;
var wnd: hwnd;
begin
ListBox1.clear;
wnd := GetWindow(handle, gw_hwndfirst);
while wnd <> 0 do
begin
if (wnd <> Application.Handle)
and IsWindowVisible(wnd)
and (GetWindow(wnd, gw_owner) = 0)
then
ListBox1.Items.Add(GetWindowClass(wnd));
wnd := GetWindow(wnd, gw_hwndnext);
end;
ListBox1.ItemIndex := 0;
end;
|
2. Потом, если имя класса наше, определяем исполняемый файл:
1 способ
Код:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function getfilepath(const winh: thandle): string;
var
snapshoth: thandle;
pe32: tprocessentry32;
prid: cardinal;
begin
result := '';
snapshoth := createtoolhelp32snapshot(th32cs_snapprocess, 0);
if snapshoth = -1 then exit;
pe32.dwsize := sizeof(pe32);
getwindowthreadprocessid(winh, prid);
if process32first(snapshoth, pe32) then
repeat
if pe32.th32processid = prid then begin
result := pe32.szexefile;
break;
end;
until not process32next(snapshoth, pe32);
closehandle(snapshoth)
end;
|
-------------------------------------------
2 способ
Код:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function getappfilename (hwnd: thandle) :string;
var dpid: dword;
hhandle: thandle;
begin
getwindowthreadprocessid(hwnd, @dpid);
setlength (result, max_path);
hhandle := openprocess(process_query_information or process_vm_read, true, dpid);
if getmodulefilenameex (hhandle, 0, pchar (result), max_path) > 0 then setlength (result, strlen (pchar (result)));
end;
procedure tform1.formcreate(sender: tobject);
var handle :thandle;
begin
handle := findwindow (nil, 'microsoft excel - Книга2');
edit1.text := getappfilename (handle);
end;
|