跨进程访问VCL的一个用例(Delphi6、TurboDelphi测试通过)

Controls.pas单元中有一个FindControl函数,通过句柄获得对应的TWinControl对象。

function FindControl(Handle: HWnd): TWinControl;
begin
Result := nil;
if (Handle <> 0) then
begin
if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
Result := Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)))
else
Result := ObjectFromHWnd(Handle);
end;
end;

由于,进程间内存地址是相对的,所以直接访问这个对象会出现内存异常。

procedure InitControls;
var
UserHandle: HMODULE;
begin
WindowAtomString := Format('Delphi%.8X',[GetCurrentProcessID]);
WindowAtom := GlobalAddAtom(PChar(WindowAtomString));
ControlAtomString := Format('ControlOfs%.8X%.8X', [HInstance, GetCurrentThreadID]);
ControlAtom := GlobalAddAtom(PChar(ControlAtomString));
RM_GetObjectInstance := RegisterWindowMessage(PChar(ControlAtomString));
//...
end;

另外FindControl函数用到的变量(如:WindowAtomString)和具体线程ID有关。

大概的步骤是:
1、创建一个hook,以便跨进程访问内存空间;
2、自定义FindControl方法,按目标窗体所在线程组装参数;
3、获得TWinControl对象;
4、通过WM_COPYDATA消息返回查询内容。

http://blog.csdn.net/zswang

http://download.csdn.net/detail/zswang/1107748
http://download.csdn.net/download/liangpei2008/1985753

Delphi跨进程访问DBGRID

http://www.cnblogs.com/key-ok/p/3358898.html

原文地址:https://www.cnblogs.com/findumars/p/6348018.html