=============================test.dll的工程文件===================
library test;uses
  SysUtils,
  Classes,
  t in 't.pas' {Form1};{$R *.res}
procedure iniConnection(ip,dbname,usr,pwd:String);stdcall;
begin
  iniconn(ip,dbname,usr,pwd);
end;function getname(id:integer):String;stdcall;
begin
  result:=getData(id);
end;exports
  iniConnection,getname;begin
end.
===============t.pas=========
unit t;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,activeX, DB, ADODB;type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  procedure iniconn(ip,dbname,usr,pwd:String);  //初始化连接
  function getData(id:integer):String;
implementation
{$R *.dfm}
function getData(id:integer):String;
var
  re:String;
begin
  Form1.ADOQuery1.SQL.Clear;
  Form1.ADOQuery1.SQL.Text:='SELECT * FROM TY01 WHERE ID='''+IntToStr(id)+'''';
  Form1.ADOQuery1.Open;
  re:=Form1.ADOQuery1.Recordset.Fields.Item['dmmc'].Value;
  Form1.ADOQuery1.Close;
end;procedure iniconn(ip,dbname,usr,pwd:String);
begin
  Form1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+pwd+';Persist Security Info=True;User ID='+usr+';Initial Catalog='+dbname+';Data Source='+ip;
  Form1.ADOConnection1.Connected:=true;
  Form1.ADOQuery1.Connection:=form1.ADOConnection1;
end;initialization
  CoInitialize(nil);
finalization
  CoUnInitialize;end.===============这是另外一程序调用此DLL=======
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  procedure iniConnection(ip,dbname,usr,pwd:String); stdcall; external 'test.dll';
  function getname(id:integer):String; stdcall; external 'test.dll';implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  iniConnection('w','T','s','l');
  Edit1.Text:=getname(1);
end;end.
请问谁知道到底错误在哪里吗?

解决方案 »

  1.   

    我另外补充一下,上面的代码全部在Delphi6下编译通过的,就是在运行调用时不成功,不知道为何?
      

  2.   

    form1,ADOConnection1, ADOQuery1好象没有创建就直接用
      

  3.   

    DLL的过程函数的string 改为pchar
      

  4.   

    ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
    对象要初始化,并且要释放,参数和归回值是字符型最好用Pchar.
      

  5.   

    ADOConnection1和ADOQuery1属于DLL中TForm1类的单元,调用是必须创建Form1对象,用完后要释放,否则会出错。
      

  6.   

    我知道这段代码写得不好,不过我只是为了试一试,我按照上面的说法,我把
    procedure iniConnection(ip,dbname,usr,pwd:String);stdcall;
    begin
      iniconn(ip,dbname,usr,pwd);
    end;
    修改为
    procedure iniConnection(ip,dbname,usr,pwd:pchar);stdcall;
    begin
      iniconn(ip,dbname,usr,pwd);
    end;
    可是还是出错
    错误代码如下:
    Access violation at address 003c630E in module 'test.dll'. Read of address 000002F0.
      

  7.   

    有哪位知道问题出在哪里吗?
    ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
    对象要初始化,并且要释放,参数和归回值是字符型最好用Pchar.那么如何初始化和释放?????????
      

  8.   

    你还没有exports呀
    DLL最后
    exports
       iniconn;
       getdata;
      

  9.   

    iniconn;
       getdata;
    这两个是我写在DLL内部的函数,没打算做为接口,这也需要Exports吗???
      

  10.   

    函数返回值用String不行!应该换为PChar
      

  11.   


    楼主没有断点追踪吗?
    我调试了一下发现iniconn里的内容不执行,弹出错误信息:
    Access violation at address 003c630E in module 'test.dll'. Read of address 000002F0.
    查找原因是: 在主程序里无法得到DLL里 FORM1 的权限,所以无法执行 Form1.ADOConnection1,Form1.ADOQuery1
    调用链接库里的FORM 要从主程序传递句柄,所以你的代码无法运行。
    采用其他的方法吧!procedure iniconn(ip,dbname,usr,pwd:String);
    begin
      Form1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+pwd+';Persist Security Info=True;User ID='+usr+';Initial Catalog='+dbname+';Data Source='+ip;
      Form1.ADOConnection1.Connected:=true;
      Form1.ADOQuery1.Connection:=form1.ADOConnection1;
    end;
      

  12.   

    function getData(id:integer):String;
    var
      re:String;
    begin
      Form1.ADOQuery1.SQL.Clear;
      Form1.ADOQuery1.SQL.Text:='SELECT * FROM TY01 WHERE ID='''+IntToStr(id)+'''';
      Form1.ADOQuery1.Open;
      re:=Form1.ADOQuery1.Recordset.Fields.Item['dmmc'].Value;
      Form1.ADOQuery1.Close;
    end;
    _______________
    怎么没有发现Result???
      

  13.   

    将test.dll中的两个实现函数拉到t.pas中看看
      

  14.   

    通常遇到与session及dbtables有关的东西时, 在dll中需要初始,让dll中的Application与应用程序中的Application为同一实例, 并初始dll中的session,调用结束后还原。我以前做的时候遇到过, 不过现在解决了。现在你碰到的问题只是这些,以后如果你在dll中的form中引用数据库对象时, 而form又是MDIchild窗口时会碰到更多的问题。