Есть handle запущенного PE файла. Как определить откуда он был запущен?
Я так предполагаю что getmodulefilename как и GetModuleHandle
работает в рамках только своего процесса.
А решить твою задачу .. можно так:
Тут парочка моих любимых функций:
uses
tlhelp32;
type
TModuleArray = arrayof TModuleEntry32;
// Возвращает список описаний (TModuleEntry32) модулей по идентификатору процессаfunction GetModulesListByProcessId(ProcessId: Cardinal): TModuleArray;
implementationfunction GetModulesListByProcessId(ProcessId: Cardinal): TModuleArray;
var
hSnapshot: THandle;
lpme: TModuleEntry32;
procedure AddModuleToList;
begin
SetLength(Result, High(Result) + 2);
Result[high(Result)] := lpme;
end;
begin
SetLength(Result, 0);
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId);
if hSnapshot = -1 then
RaiseLastWin32Error;
lpme.dwSize := SizeOf(lpme);
if Module32First(hSnapshot, lpme) thenbegin
AddModuleToList;
while Module32Next(hSnapshot, lpme) do
AddModuleToList;
end;
end;
Исходный код
var
Wnd: hWnd;
buff: array[0..127] of Char;
//------------------------------------
Pid: Cardinal;
modarr: TModuleArray;
Name: string;
//------------------------------------begin
StringGrid1.RowCount := 1;
Wnd := GetWindow(Handle, gw_HWndFirst);
while Wnd <> 0 dobeginif (GetWindowText(Wnd, buff, sizeof(buff)) <> 0) thenbegin
fillchar(name, sizeof(name), #0);
GetWindowText(wnd, buff, sizeof(buff));
// if getmodulefilename(GetWindowLong(wnd,GWL_HINSTANCE),name,sizeof(name))=0// then name:="Null";//-----------------------------------------
GetWindowThreadProcessId(Wnd, @Pid);
modarr := GetModulesListByProcessId(Pid);
name := "Null";
for i := 0 to High(modarr) dobeginif Integer(modarr[i].modBaseAddr) = $400000 thenbegin
name := modarr[i].szExePath;
break;
end;
end;
//-----------------------------------------
StringGrid1.Cells[0, StringGrid1.RowCount - 1] := StrPas(buff);
StringGrid1.Cells[1, StringGrid1.RowCount - 1] := StrPas(name);
StringGrid1.RowCount := StringGrid1.RowCount + 1;
end;
Wnd := GetWindow(Wnd, gw_hWndNext);
end;
StringGrid1.RowCount := StringGrid1.RowCount - 1;
end;
Если Вас заинтересовала или понравилась информация по разработке на Delph - "Как определить откуда был запущен процесс", Вы можете поставить закладку в социальной сети или в своём блоге на данную страницу: Так же Вы можете задать вопрос по работе этого модуля или примера через форму обратной связи, в сообщение обязательно указывайте название или ссылку на статью!