Creating Forms that are stored in DLLs

本文介绍了一种解决DLL形式的表单集成到主应用程序时遇到的问题的方法,特别是如何通过传递正确的Application、Screen对象和Control Atom来确保DLL内的表单能正确地与主应用程序交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 assign a TFont to a TFont" message.

 

So how do we Coax the form in the DLL to think it is part of the EXE, we replace the Application and Screen Object in the DLL with the reference to the EXE's Application and Screen.

 

This is a standard approach that you will find on the net. However there is one additional element that needs to be passed from EXE to DLL and this is 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 when 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 but when 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 the same name (atom) 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 GetControlAtom : Pointer that returns @ControlAtom from the controls units.

Then in your DLL's you initialize by passing the ControlAtom from the application Controls units and set this value in the Controls 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);
end;

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 projects

 

Take 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 function declaration for the function that will return the address of the current ControlAtom variable.

 

  function GetControlAtom : pointer; 

 

In the implementation section add the GetControlAtom function.

 

  function GetControlAtom : pointer; begin result := @ControlAtom; end;

 

We now have an exposed function that will return the address of the Control Atom Variable.

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 then use this source in preference to the standard controls.dcu.

We now need to provide a DLLInitialization procedure and a DLLFinalization Procedure. The DLLInitialization must be called before trying to use any forms from the DLL, usually this is

called just after loading the dll into memory. The DLLFinalization procedure should be called before the DLL is unloaded from memory.

Below is an example 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;
OldControlAtom : 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 then use this source in preference to the standard controls.dcu.

Declare some procedure / function types to be used to reference the exported procs in the dll.

 

type 
TDLLInitialize = procedure (App : TApplication; Scr : TScreen; RealControlAtom : Integer);
TDLLFinalize = procedure;
TGetInstance = function (AOwner : TComponent) : TForm;

Add a variable to store the DLL HInstance in

 

var
  FInst : HINST;

Add a procedure to Load 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 procedure CreateAForm;

 

 

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,'GetInstance');
if assigned(APRoc) then
begin
AForm := TMyPlugin(AProc(self));
AForm.Name := 'Form' + Formatdatetime('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 EXE application.

<think>嗯,用户的问题是关于在MySQL存储函数中执行DML操作的解决方案和错误原因。首先,我需要回忆一下MySQL中存储函数和存储过程的区别。存储函数通常被设计为确定性的,并且不允许执行修改数据的操作,比如INSERT、UPDATE、DELETE这些DML语句。而存储过程则允许进行这些操作。这可能就是用户遇到问题的原因,因为存储函数默认不允许DML操作。 接下来,我需要确认一下MySQL的版本和文档中的相关说明。比如,从MySQL 5.7开始,存储函数确实不允许执行DML操作,而存储过程可以。这可能是由于存储函数需要在查询中被调用,需要保证不产生副作用,而存储过程更偏向于执行操作而非返回值。 然后,用户提到的错误原因可能是因为尝试在存储函数中执行了DML语句,导致系统报错。解决方案可能包括改用存储过程,或者调整数据库的配置,不过调整配置可能涉及到修改全局变量,这需要管理员权限,并且可能影响其他函数,存在风险。 另外,还要考虑用户是否有其他需求,比如是否必须在函数中执行DML操作,或者是否有其他变通方法。比如,使用存储过程来封装DML操作,然后通过事件调度器或者触发器来调用,但这可能增加系统的复杂性。 还需要检查是否有用户权限的问题,或者是否有其他限制导致DML操作无法执行。不过根据常见情况,存储函数本身的限制是最可能的原因。 最后,总结解决方案时,要分点列出,可能包括改用存储过程、检查权限、调整配置(虽然不推荐)以及替代方法。同时,引用相关的文档或资料来支持这些建议,比如MySQL官方文档中的说明,确保信息的准确性。</think>在MySQL中,存储函数(Stored Function)默认不允许执行DML操作(INSERT/UPDATE/DELETE),这是由其设计特性决定的。以下是具体分析和解决方案: ### 错误原因 1. **函数特性限制** MySQL要求存储函数必须是确定性的(不修改数据库状态),因此禁止直接执行DML操作[^1]。 $$ \text{错误示例:ERROR 1422 (HY000): Explicit or implicit commit is not allowed in stored function or trigger} $$ 2. **事务控制冲突** 存储函数无法显式开启或提交事务(`START TRANSACTION`/`COMMIT`),而DML操作通常需要事务支持。 --- ### 解决方案 #### 方法1:改用存储过程(Stored Procedure) 存储过程支持DML操作且无事务限制: ```sql DELIMITER $$ CREATE PROCEDURE update_data() BEGIN UPDATE users SET status = 1 WHERE id = 100; DELETE FROM logs WHERE created_at < NOW() - INTERVAL 30 DAY; END $$ DELIMITER ; ``` #### 方法2:检查权限与配置 1. 确保用户拥有`EXECUTE`权限: ```sql GRANT EXECUTE ON database_name.* TO 'user'@'host'; ``` 2. (不推荐)通过全局变量临时绕过限制: ```sql SET GLOBAL log_bin_trust_function_creators = 1; -- 需SUPER权限 ``` #### 方法3:替代方案 - **触发器(Trigger)**:响应特定表事件时执行DML - **事件调度器(Event Scheduler)**:定时执行SQL语句 - **应用程序层处理**:将DML逻辑移至代码(如Python/Java) --- ### 引用说明 MySQL官方文档明确指出存储函数中禁止修改数据表状态[^1]。若需强制修改数据,存储过程是标准做法[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值