Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
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()) {
			//ShowMessage("Find the control" + ctrl_name_unicode);
			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
C++
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.
C++
FindControl

So I wrote another function,hope to access vcl controls cross process
C++
FindControlEh

It don't work.I don't know what's wrong. give me a hand please.
Posted
Updated 27-Jun-18 17:38pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900