Ответ 1
Delphi имеет встроенную функцию FindControl()
, которая возвращает TWinControl
указанного hWnd. Но он работает для того же экземпляра VCL. Я думаю, вам следует его исследовать. После указания указателя на объект TWinControl его имя (строка), расположенное в смете +8
. Вы можете попробовать ReadProcessMemory для его чтения. Основная проблема здесь заключается в том, чтобы создать версию FindControl(), которая соответствует вашим потребностям.
Изменить: (Наконец получил: D) Вызвать функцию GetWinControlName
// Get Pointer to TWinControl in another process
function GetWinControl(Wnd: HWND; out ProcessId: THandle): Pointer;
var
WindowAtomString: String;
WindowAtom: ATOM;
begin
if GetWindowThreadProcessId(Wnd, ProcessId) = 0 then RaiseLastOSError;
// This is atom for remote process (See controls.pas for details on this)
WindowAtomString := Format('Delphi%.8X',[ProcessID]);
WindowAtom := GlobalFindAtom(PChar(WindowAtomString));
if WindowAtom = 0 then RaiseLastOSError;
Result := Pointer(GetProp(Wnd, MakeIntAtom(WindowAtom)));
end;
function GetWinControlName(Wnd: HWND): string;
var
ProcessId: THandle;
ObjSelf: Pointer;
Buf: Pointer;
bytes: Cardinal;
destProcess: THandle;
begin
ObjSelf := GetWinControl(Wnd, ProcessId);
destProcess := OpenProcess(PROCESS_VM_READ, TRUE, ProcessId);
if destProcess = 0 then RaiseLastOSError;
try
GetMem(Buf, 256);
try
if not ReadProcessMemory(destProcess, Pointer(Cardinal(ObjSelf) + 8), Buf, 4, bytes) then RaiseLastOSError;
if not ReadProcessMemory(destProcess, Pointer(Cardinal(Buf^)), Buf, 256, bytes) then RaiseLastOSError;
Result := PChar(Buf);
finally
FreeMem(Buf);
end;
finally
CloseHandle(destProcess);
end;
end;