1. Получаешь список хендлов, примерно так:
Код:
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 сохраняем в переменную для последующих манипуляций
wnd := GetWindow(wnd, gw_hwndnext);
end;
ListBox1.ItemIndex := 0;
end;
2. Потом, если имя класса наше, определяем исполняемый файл:
1 способ
Код:
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 способ
Код:
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;