急求一个DLL,DLL里面连表 delphi自带的DB。
读取一张表里的数据。然后返回结果集, 给别的程序调用。
 
    大牛,大虾救我,急求代码!!  或者有别的返回表的内容的方法,也求大牛教我。

解决方案 »

  1.   

    以下代码Delphi XE 下测试通过
    其中的test.db表, 含有id, name字段
    test.dll
    library test;{ 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,
      DB,
      DBTables;{$R *.res}
    function GetTable(ADBPath, ATBName : PChar) : TTable;
    begin
      Result := nil;
      try
        Result := TTable.Create(nil);
        Result.DatabaseName := StrPas(ADBPath);
        result.TableName := StrPas(ATBName);
        result.open;
      except on E: Exception do
        raise Exception.Create('数据提取错误:' + e.message);
      end;
    end;exports
      GetTable;begin
    end.
    调用方法(下边的代码中, table在dll销毁前也做了销毁, 如果你要在程序的其它部分使用该table, 最好使用dll的静态调用, 或者对dll的释放时机做出调整)
    uses DBTables;
    {$R *.dfm}procedure TForm15.btn1Click(Sender: TObject);
    type
      TFun = function (ADBPath, ATBName : PChar) : TTable;
    var
      aFun : TFun;
      aHandle : thandle;
      aTable : ttable;
    begin
      aHandle := LoadLibrary(PChar('test.dll'));
      try
        if aHandle <> 0 then
        begin
          aFun := GetProcAddress(aHandle, 'GetTable');
          try
            aTable := aFun(PChar(ExtractFilePath(Application.ExeName)), PChar('test.db'));
            if aTable <> nil then
            begin
              aTable.First;
              ShowMessage(aTable.FieldByName('id').AsString);        end;
          except on E: Exception do
            ShowMessage(e.Message);
          end;
        end;
      finally
        if aTable <> nil then
          aTable.free;
        FreeLibrary(aHandle);
      end;
    end;