Here I'm trying to rewrite the VCL method FindControl()(Borland C++ Builder),because the original FindControl function does not support cross process access. According to someone's reference,I wrote the following code
TWinControl* FindControlEh(HWND hwnd);
extern "C" __declspec(dllexport) HWND __stdcall FindControls(HWND hwnd , char* ctrl_name);
TAtom ControlAtom;
char ControlAtomString[512];
DWORD RM_GetObjectInstance;
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
printf("Load FindControl.dll.");
switch(fwdreason) {
case DLL_PROCESS_ATTACH:
{
ControlAtom = GlobalAddAtom(ControlAtomString);
sprintf(ControlAtomString,"ControlOfs%08X%08X",GetModuleHandle(NULL),
GetCurrentThreadId());
RM_GetObjectInstance = RegisterWindowMessage(ControlAtomString);
}
break;
case DLL_PROCESS_DETACH:
GlobalDeleteAtom(ControlAtom);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}
HWND __stdcall FindControls(HWND hwnd , char* ctrl_name)
{
int index = 0;
TWinControl* main_controls = FindControlEh(hwnd);
int ctrls_count = main_controls->ControlCount;
UnicodeString ctrl_name_unicode(ctrl_name);
ShowMessage(IntToStr(ctrls_count));
TWinControl* tmp_control;
for(index = 0; index (main_controls->Controls[index]);
if(ctrl_name_unicode == tmp_control->GetNamePath()) {
return tmp_control->Handle;
}
}
return NULL;
}
TWinControl* FindControlEh(HWND hwnd)
{
DWORD OwningProcess;
DWORD hwnd_threadid;
TWinControl* result = NULL;
hwnd_threadid = GetWindowThreadProcessId(hwnd , &OwningProcess);
if( hwnd != NULL && 0 != hwnd_threadid && OwningProcess == GetCurrentProcessId()) {
if(GlobalFindAtom(ControlAtomString) == ControlAtom) {
result = (TWinControl*)Pointer(GetProp(hwnd , MakeIntAtomA(PChar(ControlAtom))));
}
else result = (TWinControl*)Pointer(SendMessage(hwnd , RM_GetObjectInstance , 0 , 0));
}
return result;
}
What I have tried:
I debug the
FindControlEh
method , the result always return null.
here is what I'm doing:
1.I'm not knowing well about the vcl. A lot vcl controls are not standard win32 controls. VCL control's property including some thing like control name which is not accessible.So I'm using the Borland C++ Builder IDE write some methods as interface(DLL), to expose some property of the vcl controls. I'm not known well about the Borland C++ Builder Env. the exposed interfaces in dll will be called in VSC++ env;
2.I'm trying to access all the controls of some other program developed by Borland C++ Builder IDE.cross process. The VCL original function not support cross process access.
FindControl
So I wrote another function,hope to access vcl controls cross process
FindControlEh
It don't work.I don't know what's wrong. give me a hand please.