DLL代码如下:library SYSDLL;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Classes,
  Forms,
  Dictionary in 'Dictionary.pas' {frmDictionary},
  Functions in '..\..\Include\Functions.pas',
  waitquery in '..\..\Include\waitquery.pas' {frmWaitQuery},
  ErrorInfo in '..\..\Include\ErrorInfo.pas' {frmErrorInfo},
  BaseFunctions in '..\..\Include\BaseFunctions.pas',
  ADOConnection in '..\..\Include\ADOConnection.pas';{$R *.res}
function CreateDllFrm(sFrmName: String): Boolean; StdCall;
begin
  Result := True;
  if sFrmName = '' then
    ErrorFrm('窗体参数不能为空...', 1);
  end;  try
    case sFrmName of
      'frmDictionary':
      begin
        Application.CreateForm(TfrmDictionary, frmDictionary);
        frmDictionary.ShowModal;
        frmDictionary.Free;
      end;
    end;
  except
  end;
end;exports
  CreateDllFrm;
end.DLL中的form要如何才能使用调用它的EXE里已经创建的ADO连接?
请赐教啊。

解决方案 »

  1.   

    试试这个,随手写的
    function connect(DLLconnect:TADOConnection):true;stdcall;
    begin
      try
       begin
       DLLquery.Connection:=DLLconnect;
       result:=true;
       end;
      except
        begin
        result:=false;
        end;
    end;
      

  2.   

    回楼上的,你的DLLconnect是传入的吧,但是我现在就是不知道哪里可以传入这个DLLconnect
    EXE和DLL是两个项目文件。
    EXE在创建的时候会联立一个ADOCon的数据库长连接。
    我想在DLL中也使用它,而不想再建连接。创建代码如下:
      try
        Adocon := TAdoConnection.Create(nil);
        Adocon.LoginPrompt := False;
        bDBConnected := False;
        if not AdoConnSqlDb(AdoCon,sPassword,server,'master',sUser) then
        begin
          ErrorFrm('连接数据库失败,请重试或重新设置数据连接参数和数据库用户密码!', 1);
          AdoCon.Free;
          bDBConnected := False;
          Application.CreateForm(TfrmParamForm, frmParamForm);
          Exit;
        end;
      except
        ShowMessage('连接数据库失败,请重试或重新设置数据连接参数!');
        AdoCon.Free;
        Application.Terminate;
      end;
      

  3.   

    把Exe里的连接作为参数传进去就行了
      

  4.   

    { 外部声明必须和 DLL中的参数列表一致,否则会运行时错误 }
    procedure DoTest(H: THandle; { 传递句柄 }
                     AConn: TADOConnection; { 传递数据库连接 }
                     S: string; { 传递文本信息 }
                     N: Integer); { 传递数值信息 }
    cdecl; { 指定调用协议 }
      

  5.   

    具体你可能参考下面这个帖子,里面说得很详细的。
    http://search.csdn.net/Expert/topic/1645/1645329.xml?temp=.5659906
    记得结贴给分噢:)
      

  6.   

    好长啊,看完了,受了些启发。
    只是示例中的情况和我的刚好相反。示例中是EXE通过DLL创建ADO连接,我这里的问题是DLL中的模块需要引用EXE中的ADO连接,还情各位DX明示下。
      

  7.   

    简单的说,就是在EXE里建立一个链接,然后在调用DLL的函数时,将ADO链接作为一个参数传进去,这样在DLL里,这个函数就可以直接用这个链接了。像我上面提到的DoTest函数,就是将ADO用参数的方法传进去,这样在DLL里就直接用AConn这个链接就行了。
      

  8.   

    我现在EXE中创建的ADO连接的代码如下,
    try
        Adocon := TAdoConnection.Create(nil);
        Adocon.LoginPrompt := False;
        bDBConnected := False;
        if not AdoConnSqlDb(AdoCon,sPassword,server,'master',sUser) then
        begin
          ErrorFrm('连接数据库失败,请重试或重新设置数据连接参数和数据库用户密码!', 1);
          AdoCon.Free;
          bDBConnected := False;
          Application.CreateForm(TfrmParamForm, frmParamForm);
          Exit;
        end;
      except
        ShowMessage('连接数据库失败,请重试或重新设置数据连接参数!');
        AdoCon.Free;
        Application.Terminate;
      end;DLL中的一个form需要的ADO连接如下,
    try
        begin
       【   Connection := Adocon;  】   
          ProcedureName := 'sql..sp_sys_dict_set';
          Parameters.CreateParameter('@staff_id', ftInteger, pdInput, 4, CommonInfo.StaffID);
          Parameters.CreateParameter('@st_station', ftstring, pdInput, 12, CommonInfo.MAC);
          Parameters.CreateParameter('@action', ftInteger, pdInput, 4, iAction);      
          Parameters.CreateParameter('@dict_prompt', ftstring, pdInput, 255, Trim(sedtdictprompt.Text));
          Parameters.CreateParameter('@error_info', ftString, pdOutput, 255, '');
          ExecProc;我该如何是好,这个form原本在exe里的,现在准备要放到DLL里公用。
      

  9.   

    >>>>>你怎麼這麼笨啊!  alinsoft(艾林) 說的很詳細了!!!如果你不願意傳tadoconnection對象指針, 那你就傳這個對象的連接字符串也行啊.你只要將a.exe內的ado.connectionstring 作為一個參數傳進dll, 在dll內保存這個連接字符串給FConStr.
    如果要用adoconnection,動態創建一個不就行了麼或者直接寫:
    ADOStoredProc1.ConnectionString:=FConStr;
    ADOQuery1.ConnectionString:=FConStr;
    ADODataSet1.ConnectionString:=FConStr;你怎樣用都行. 想問題要靈活些.
      

  10.   

    我落下delphi 有一段时间了,略微有些手生,给我1周时间恢复。
      

  11.   

    对于 JonnySun() 说的动态创建会消耗很大的系统资源,因为我们的软件会打开非常多的用户界面。
    而且每次重连数据库有时会很慢,所以这种做法不太考虑,
    如果动态传建的话我们的数据库连接配置都是在注册表里的,也不用当成参数传来传去。