Получение имени модуля по хендлу окна
Материал из DRKB
Получение имени модуля по хендлу окна[править | править код]
function GetModuleFileNameExW(hProcess: THandle; hModule: HMODULE; lpFilename: PWideChar;
nSize: DWORD): DWORD; stdcall; external 'PSAPI.DLL';
function WindowGetEXE(wnd: HWND): string;
var
wt: array[0..MAX_PATH-1] of WChar;
r: Integer;
prc: THandle;
prcID: Cardinal;
begin
Result := '';
if GetWindowThreadProcessID(wnd, prcID) <> 0 then
begin
prc := OpenProcess(PROCESS_ALL_ACCESS, False, prcID);
if prc <> 0 then
try
r := GetModuleFileNameExW(prc, GetWindowLong(wnd, GWL_HINSTANCE), wt, MAX_PATH*2);
if r = 0 then
r := GetModuleFileNameExW(prc, 0, wt, MAX_PATH*2);
if r <> 0 then
Result := wt;
finally
CloseHandle(prc)
end
end
end;
function SetProcessDebugPrivelege(): Boolean;
var
hToken: THandle;
tp: TTokenPrivileges;
rl: Cardinal;
begin
Result := False;
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken) then exit;
try
if not LookupPrivilegeValue(nil, 'SeDebugPrivilege', tp.Privileges[0].Luid) then exit;
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
tp.PrivilegeCount := 1;
Result := AdjustTokenPrivileges(hToken, False, tp, 0, nil, rl) and (GetLastError=0);
finally
CloseHandle(hToken);
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetProcessDebugPrivelege();
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(WindowGetExe(hWnd));
end;
PS только для NT4 и выше. Для Win9x юзать GetWindowModuleFileName.
Author: Krid
Source: Взято из http://forum.sources.ru
ID: 02133