我有位于另一个进程窗口上的 VCL 控件的 Hwnd。有没有办法通过 Windows API 获取该控件的 VCL 名称(TControl.Name
属性)?
我需要该名称,因为该窗口上有多个 TEdit,并且我需要识别我想要的 TEdit,以便向其发送 WM_SETTEXT
消息。
这两个应用程序都是使用 Delphi 2010 构建的。
最佳答案
Delphi 有内置函数 FindControl()
,它返回指定 hWnd 的 TWinControl
。但它适用于 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;
关于delphi - 有没有办法通过Windows API获取VCL控件的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15534081/