Creating Forms That Are Stored in DLLS

xiaoxiao2021-03-06  37

There are lots of resources and solutions out there on the internet that are specific to this problem, however, in using the BusinessSkinForm components, that are tightly integrated with the VCL and messaging, I came across a few problems with the standard approaches.

The Final Solution Came with The Assistance of Steve Woods (Aka Reconics).

The main problem with storing Forms in dlls and being able to create instances of them from within a host exe is that when Delphi compiles up a dll, it has its own TApplication and TScreen instances (as well as other info as to be discovered).

This Means That The DLL AND THE EXE Message Loops Are Different, The RTTI Information IS Different, And Causes Lots of Problems Like The Well Know "Cannot" MESSAGE.

SOHE, WE Replace The Application and Screen Object In The Dll with Screen.

This is the the Tricky One.. '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

From Steve Woods -

"The problem is caused by the ControlAtom local variable in the Controls.pas units. When the controls unit initializes it creates a global atom based on the string 'ControlOfs' the HInstance and thread ID of the application and stores the atom in the ControlAtom Local variable.

All Well and Good. Then New WinControls Are Added to the Application A Pointer To The Control Is Stored Under The Window Handle Using The SetProp API Function. This Allows

Delphi

TO Get The TwinControl of a Handle.

The CM_MouseEnter etc. events are generated from the TApplication.DoMouseIdle method. In order to get the current wincontrol under the mouse, and thus pass the required messages, its searches for a property with a Atom equal to the local ControlAtom variable stored in the Controls Units.this All Works Fine for Standalone Apps Buthen You Start Putting Forms In Dll's The Following Happens:

The controls unit is initialized again for the DLL. This creates a new global atom and stores it in the Controls unit local variable, so you now have TWO control global atoms, one for the main app and one for the DLL.

And thus, when the Application.DoMouseIdle method tries to find a property stored with (atom) the same name as the application on a DLL form, it fails. To solve this I had to hack the controls.pas units a little. I added A Routine: Function GetControltom: PoinTr That Returns @ControlAtom from the controls units.

THEN YOUR DLL'S You Initialize by Passing The Controls Unit of The Application Controls Units Unit of The DLL:

EG

Library Adll; Uses ... Forms, Controls, // Add this here so it initializes unit before we try and change getcontrolatom; ...

procedure CreateForm (App: TApplication; Scr: TScreen; RealControlAtom: Integer); Var P: ^ Word; Begin If (OldApp = Nil) Then Begin OldApp: = Application; OldScr: = Screen; P: = GetControlAtom; OldAtom: = P ^; Application: = App; Screen: = SCR; P ^: = realcontrolatom; end; tform1.create (application.mainform);

AND Make Sure You Clean-Up Before You Unload The DLL. "

Stage 1) The ControlAtom variable is private to the controls.pas unit so we need to change Controls.pas for the EXE and DLL projectsTake a copy of controls.pas from the delphisourcevcl directory and save it into a directory that the EXE project and DLL Project Can Access.

In the interface section add the burr Return the address of the current controlom variable.

Function GetControltom: Pointer;

In The Implementation Section Add The getControlTom Function.

Function getControltom: Pointer; Begin Result: = @ControlTom; end;

WE now..............

Stage 2) Setting Up The DLL Exported Functions

Add The Modified Controls.Pas to The Project So It Appears in The Project Manager.

Delphi

Will the use this source in preference to the standard controls.dcu.

WE now NEED TO PROVIDE A DLLINITION Procedure and A DLLFINALIZATION Procedure. The Dllinitialization Must Be Called Before Trying To Use Any Forms from The DLL, Usually this IS

.

Below is an esample of the code needed to be exposed in the dll. NOTICE THE DLL EXPORTS 3 functions / procedures dllinitiailize, dllfinalize and getInstance

GetInstance is The call That Actually Returns an Instance of a the DLLS Form.

Library Plugin; Uses Sharemem, Sysutils, Classes, Windows, Forms, Controls IN 'Controls.Pas', Pluginform in 'PluginForm.PAS' {MyPlugin};

{$ R * .res}

Var Oldapp: TAPPLICATION; OldScreen: Tscreen; OldControltom: Tatom

procedure DLLInitialize (App: TApplication; Scr: TScreen; RealControlAtom: Integer); var x: pointer; p: ^ Word; begin If (OldApp = Nil) Then Begin // store away the current application, screen and control atom OldApp: = Application; OldScreen: = Screen; p: = GetControlAtom; OldControlAtom: = p ^; // Assign the EXE's application, screen and control atom Application: = App; Screen: = Scr; p ^: = RealControlAtom; end; ASkin: = Skin; end; function GetInstance (AOwner: TComponent): TForm; begin // create an instance of the form result: = TMyPlugin.create (Application.MainForm); end; procedure DLLFinalize; var p: ^ Word; begin // restore the DLL's application, screen and control atom p: = GetControlAtom; p ^: = OldControlAtom; Screen: = OldScreen; Application: = OldApp; end; exports GetInstance, DLLInitialize, DLLFinalize; begin OldApp: = nil; OldScreen: = nil; OldControlAtom : = 0; End.stage 3) Setting Up The EXE.

Add The Modified Controls.Pas to The Project So It Appears in The Project Manager.

Delphi

Will the use this source in preference to the standard controls.dcu.

.

Type TdllInitialize = procedure (app: tapplication; sc: tscreen; realcontrolatom: integer; tdllfinalize = process; tGetInstance = function; tform;

Add a variable to store the dll hinstance in

VAR FINST: HINST;

Add a procedure to loading the dll and initialize it

procedure LoadDLL var AInit: TMyInitialize; p: ^ WORD; begin // get the address of the Exe's control atom p: = GetControlAtom; FInst: = LoadLibrary ( 'plugin.dll'); AInit: = GetProcAddress (FInst, 'DLLInitialize' ); if assigned (AInit) then begin // pass the TApplication, TScreen and value of the Control Atom AInit (Application, Screen, p ^, bsCompressedStoredSkin1); end; end; Add a procedure to Finalize the DLL and Unload it

procedure UnloadDLL var AProc: TMyFinalize; begin // unload the dll after asking the dll to restore it's original application, screen and ControlAtom AProc: = GetProcAddress (FInst, 'DLLFinalize'); if assigned (AProc) then begin AProc; end; if FINST <> 0 THEN FREELIBRARY (FINST); END;

Add a procedure to create an instance process

Var Aproc: TgetInstance; AFORM: TMYPLUGIN; Begin // Get An Instance of The Form in The DLL - Not That The DLL Creates It ... NOT The EXE. APROC: = GetProcaddress (FINST, 'GETISTANCE'); if Assigned APROC) The begin AFORM: = Tmyplugin (Aproc (self)); AFORM.NAME: = 'Form' Formator ('hhnnss', now); // give it a unique name end; end;

So you now have an EXE and DLL that both use the modified controls.pas, that correctly passes the EXE's application object, screen object and ControlAtom to the DLL to trick the DLL into thinking it is part of the EXE application.

Procedure CreateForm (App: TApplication; Scr: TScreen; RealControlAtom: Integer); Var P: ^ Word; Begin If (OldApp = Nil) Then Begin OldApp: = Application; OldScr: = Screen; P: = GetControlAtom; OldAtom: = P ^; Application: = App; Screen: = Scr; P ^: = RealControlAtom; end; end; So you now have an EXE and DLL that both use the modified controls.pas, that correctly passes the EXE's application object, screen object and Controlatom to the dll to Trick The DLL INTO Thinking It is part of the estyle.

转载请注明原文地址:https://www.9cbs.com/read-72813.html

New Post(0)