Search code examples
windowsdelphidelphi-xe2

Delphi: How to get Application.MainForm if the Application is nil (in dll)


I need the main form of the application to perform ClientToParent() and unsuccessful because I'm in a dll and the Application variable is nil. I appreciate anybody's help.


Solution

  • You can't do anything with a form reference across a module boundary. The host application, even assuming it is a Delphi application, has a different instance of the VCL. So you cannot use a TForm instance from the app in the DLL. You could do so if you were using packages.

    You could perhaps enumerate top level windows and find the one in your process. That could work but is messy.

    Far better would be to expose functionality to allow the host to provide such services. When the host loaded your DLL it would supply an interface which provided the necessary services. It could convert the coordinates as you need, and perhaps offer other services.

    One thing I would query though is that you intend to call ClientToParent. This suggests that you have client/parent relationships between controls in different modules. That is only viable if you are using packages. I do wonder whether you are fully aware of the restrictions relating to VCL usage across modules.