求大牛给我写个DLL (急求!!!)

a461467123 2013-04-14 11:27:18
急求一个DLL,DLL里面连表 delphi自带的DB。
读取一张表里的数据。然后返回结果集, 给别的程序调用。

大牛,大虾救我,急求代码!!

或者有别的返回表的内容的方法,也求大牛教我。


...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
OO_is_just_P 2013-04-16
  • 打赏
  • 举报
回复
楼上漂亮
simonhehe 2013-04-15
  • 打赏
  • 举报
回复
以下代码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;
a461467123 2013-04-14
  • 打赏
  • 举报
回复
晕死,这个问题就有这么难......无语了
sololie 2013-04-14
  • 打赏
  • 举报
回复
消灭0回复,撸过围观

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧