gpt4 book ai didi

delphi - 跨 DLL 边界共享 ADO 连接

转载 作者:行者123 更新时间:2023-12-03 15:14:31 25 4
gpt4 key购买 nike

我们希望跨 DLL 边界共享 ADOConnection(目前是 Delphi 到 Delphi,但在不久的将来也可能是 C# 到 Delphi)。

由于我们希望将来能够灵活地从 C# 调用 DLL,因此我们希望能够使用 _Connection 作为参数来定义 DLL 调用。像这样的东西:

procedure DoStuff (ADOConnection: _Connection)
var
InnerConnection: TADOConnection;
begin
InnerConnection := TADOConnection.create(nil);
try
InnerConnection.ConnectionObject := ADOConnection;
DoMoreStuff(InnerConnection);
finally
InnerConnection.free;
end;
end;

不幸的是,TADOConnection 析构函数代码关闭了传递给它的连接,这是一个不需要的副作用。添加

InnerConnection.ConnectionObject := nil

在 free 之前不执行任何操作,因为它被捕获了

if Assigned(Value) = nil

在 TADOConnection.SetConnectionObject 中,这会导致调用不执行任何操作。

有没有更好的方法来实现这一目标?传递连接字符串是一种替代方法,但这意味着我们必须处理用户名/密码问题和跨边界加密。传递 TADOConnection 是另一种选择,但这会阻止从其他语言进行调用。

编辑:为了清楚起见,原始 TADOConnection 对象的用户名/密码是使用 .Open 例程设置的,因此这些详细信息不在连接字符串中(事实上,通常会存储错误的用户名,因为它是名称用于在 MS UDL 编辑器中“测试连接”)

最佳答案

您可以尝试以下方法:

  type TInit_StFattDLL = procedure( var DataBase:TAdoConnection);
var Init_StFattDLL:TInit_StFattDll;

调用者是:

Function ConnectDll():Boolean;
var
handleDll:THandle;

begin
handleDll := LoadLibrary('mydll.DLL');
@Init_StFattDLL := GetProcAddress(handleDll , 'myConnectFunction');

if @Init_StFattDLL <> nil then
begin
Init_StFattDLL(ADOConnection1);
result:=true;
end
else
result:=false;
end;

将以下内容放入 dll 中:

在项目文件中放置导出:

Exports myConnectFunction;

全局部分:

var Database:TAdoConnection;

导出的过程如下:

procedure myConnectFunction( var MyDataBase:TAdoConnection);export;
begin
Database:=MyDataBase;
end

关于delphi - 跨 DLL 边界共享 ADO 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com