以下代码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;
其中的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;