dll 中传递 adoConnection 的例子

前几天看以别人用 dll 来传 DataBase 的连接 , 我想试试能不能用 dll 来传 Adoconnection 呢?发现

Adoconnection 没有 handle 这个属性,这怎么传呀?

上网问了问也没有问出什么来,也就这样放着,今天上网看了一个网友问同样一个问题,嗯,刚好看到一篇有

关 dll  中传对象的文章,又找出来 看了看,呵呵,正好是关于怎么传 adoconnection 的(我以前怎么忘了

看这篇文章呢),复制、粘贴,给网友回贴了。

不能就这样回贴吧,我再试试吧,一试,出现问题了,不行

------------------------
我的代码
-------------------------------
dll

library Project1;


uses
  SysUtils,
  Classes,
  DllUnit2 in 'DllUnit2.pas' {Form2};

{$R *.res}

exports showF;

begin
end.

-----
unit DllUnit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls;

type
  TForm2 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
procedure showF(H:Thandle;adoC:tAdoconnection);cdecl;
var
  Form2: TForm2;
  a:^TAdoConnection;
implementation

 

{$R *.dfm}

procedure showF(H:Thandle;adoC:TAdoconnection);
begin
  application.Handle := h;

  form2 := tform2.Create(application);
 
  form2.ADOConnection1:= adoc;
 
 if form2.ADOConnection1.Connected then showmessage('ooo')
  else
    showmessage('not connection'); 
  try
   
    form2.ShowModal;
  finally
    form2.Free;
    application.Handle := 0;
  end;

end;

procedure TForm2.Button1Click(Sender: TObject);
begin
     adoquery1.Open;
end;

end.

===========
主程序

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Grids, DBGridEh;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    Button1: TButton;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    DBGridEh1: TDBGridEh;
    procedure Button1Click(Sender: TObject);
  private
   
  public
    { Public declarations }
  end;
  procedure showF(h:THandle;adoC:TAdoconnection);cdecl;external 'project1.dll';
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   showF(application.Handle,self.ADOConnection1);
end;

end.

------------------------------------------------


点击 button1 提示错误,意思是没有找到数据源,怎么回事呢?

后来这样试了试

------------------
procedure showF(H:Thandle;adoC:TAdoconnection);
begin
  application.Handle := h;

  form2 := tform2.Create(application);
 
  form2.adoconnection1 := adoC;

  if form2.ADOConnection1.Connected then showmessage('ooo')
  else
    showmessage('not connection'); // 这里没有问题,提示是 'ooo'
  try
   
    form2.ADOQuery1.Open;          // 这里问题依旧
    form2.ShowModal;
  finally
    form2.Free;
    application.Handle := 0;
  end;

end;
------------------

 再试试

-----------------
procedure showF(H:Thandle;adoC:TAdoconnection);
begin
  application.Handle := h;

  form2 := tform2.Create(application);
 
  form2.ADOConnection1.ConnectionString := adoc.ConnectionString;

  form2.ADOConnection1.Connected := true;

 { if form2.ADOConnection1.Connected then showmessage('ooo')
  else
    showmessage('not connection');  }
  try
   
    //form2.ADOQuery1.Open;  这里打开没有错误,用 button1 的打开也没有错误
    form2.ShowModal;
  finally
    form2.Free;
    application.Handle := 0;
  end;

end;
-----------------

上面这样没有问题了,表里的内容给显示出来了


注意,上面的 dll 用的是 cdecl


总结一下

form2.ADOConnection1:= adoc; 这样赋值应该不正确,如果要用 adoconnection 也只能在该过程里用,而不能

出了这个过程再用

突然想到了用 stdcall 再试试

发现用 stdcall 也可以,呵,http://blog.csdn.net/qi_jianzhou/archive/2006/03/08/618653.aspx 我的这

篇 blog 里说不能用 stdcall,但我成功了,我用了一个指针 ^TAdoconnection

附代码

-----------------------
dll
--------


library Project1;


uses
  SysUtils,
  Classes,
  DllUnit2 in 'DllUnit2.pas' {Form2};

{$R *.res}

exports showF;

begin
end.
-----------
form2
-----------
unit DllUnit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls;

type
  TForm2 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
procedure showF(H:Thandle;adoC:tAdoconnection);stdcall;
var
  Form2: TForm2;
  a:^TAdoConnection;
implementation

 

{$R *.dfm}

procedure showF(H:Thandle;adoC:TAdoconnection);
begin
  application.Handle := h;

  form2 := tform2.Create(application);
  a := @adoc;
  //form2.ADOConnection1.ConnectionString := adoc.ConnectionString;
  //form2.ADOConnection1.Connected := true;
 { if form2.ADOConnection1.Connected then showmessage('ooo')
  else
    showmessage('not connection');  }
  try
    form2.ADOQuery1.Connection := a^;
    form2.ADOQuery1.Open;
    form2.ShowModal;
  finally
    form2.Free;
    application.Handle := 0;
  end;

end;

procedure TForm2.Button1Click(Sender: TObject);
begin
     adoquery1.Open;
end;

end.
=========
主程序
=========
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Grids, DBGridEh;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    Button1: TButton;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    DBGridEh1: TDBGridEh;
    procedure Button1Click(Sender: TObject);
  private
   
  public
    { Public declarations }
  end;
  procedure showF(h:THandle;adoC:TAdoconnection);stdcall;external 'project1.dll';
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   showF(application.Handle,self.ADOConnection1);
end;

end.

-----------------------

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值