0

I have two EXEs, exe1 and exe2. They act like client and server linked through TCP. There is a "Merge" button in exe 1. On clicking it, the handle of the Form in exe 2 is received, which is logged as an int in a file.

// resp is object holding the handle value as wstring

long whnd = _wtoi(resp->GetValue("handle")->Value().c_str());  // converted from string to long

// Getting form object of exe2 from handle
TCommonCustomForm  *wd = Fmx::Platform::Win::FindWindow((HWND)whnd);

// wd  = NULL

But this is returning nullptr? Any idea why.

How to get the Form from exe2 through an HWND and merge it into the Form of exe1?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
viji
  • 11
  • 1
  • if you are linked with TCP or any other IPC and have access to the source code you could make an API that will send the Handle you want ... If not you can search for the Handle by winapi see: [getting HWND of running apps](https://stackoverflow.com/a/21330590/2521214) – Spektre Jun 19 '20 at 07:38
  • Since the hwnd belongs to another process, it is not possible to return a TCommonCustomForm pointer to it, because pointers refer only to objects in your own process. – Raymond Chen Jun 22 '20 at 04:20
  • @viji As I told you in your [same question on the Embarcadero forums](https://community.idera.com/developer-tools/general-development/f/tools-22/72624/merging-of-two-exes) (I have included that reply below), what you are attempting to do will not work. – Remy Lebeau Jun 22 '20 at 23:47

1 Answers1

0

The Fmx::Platform::Win::FindWindow() function takes a Win32 HWND as input and returns the FMX Form object that owns the HWND. Although an HWND is a global resource and can be accessed across process boundaries, each process has its own memory address space and objects in each process cannot be directly accessed across process boundaries. Which means the HWND you pass to FindWindow() must belong to a Form that exists in the same process that is calling FindWindow(), which is not the case in your situation, which is why FindWindow() returns nullptr.

So, you will not be able to merge your UIs together at the FMX level, you would only be able to do so at the Win32 API level, using the Win32 SetParent() function. Which you should not do across process boundaries at all, since FMX is not designed for cross-process sharing of its windows.

In fact, it is not even possible anyway, because an FMX Panel does not have its own HWND, like a VCL Panel does. All child controls in FMX are window-less! Only Forms have HWNDs, since they are the only UI element that directly interacts with the OS. All child controls in FMX are custom-drawn by FMX. Parent/child relationships are managed only by FMX, not by the OS.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770