Delphi编写的dll源码如下:
library FSearch;uses
SysUtils, Classes, FastSearch, DB, ADODB;var
FastSearch1: TFastSearch;{$R *.res}
function FastSearch(ADOQuery1: TADOQuery):String;stdcall;
var strSQL:String;
begin
strSQL:='';
FastSearch1.DataSet:=ADOQuery1;
if FastSearch1.Search() then
begin
strSQL:=FastSearch1.QueryCondition;
end;
Result:=strSQL;
end;exports
FastSearch;begin
end.C++Builder中调用如下:
typedef AnsiString (GetValue)(TADOQuery*);
__declspec(dllimport) AnsiString __stdcall FastSearch(TADOQuery *adoQuery);
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE nDLL;
GetValue *theValue;
nDLL = LoadLibrary("FSearch.dll");
if(nDLL != NULL)
{
theValue = (GetValue*)GetProcAddress(nDLL, "FastSearch");
if(theValue != NULL)
Edit1->Text = (*theValue)(ADOQuery1);
}
FreeLibrary(nDLL);
}
//---------------------------------------------------------------------------
跟踪运行到Edit1->Text = (*theValue)(ADOQuery1);报错,也就是说调用Dll函数出错,请指教!
library FSearch;uses
SysUtils, Classes, FastSearch, DB, ADODB;var
FastSearch1: TFastSearch;{$R *.res}
function FastSearch(ADOQuery1: TADOQuery):String;stdcall;
var strSQL:String;
begin
strSQL:='';
FastSearch1.DataSet:=ADOQuery1;
if FastSearch1.Search() then
begin
strSQL:=FastSearch1.QueryCondition;
end;
Result:=strSQL;
end;exports
FastSearch;begin
end.C++Builder中调用如下:
typedef AnsiString (GetValue)(TADOQuery*);
__declspec(dllimport) AnsiString __stdcall FastSearch(TADOQuery *adoQuery);
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE nDLL;
GetValue *theValue;
nDLL = LoadLibrary("FSearch.dll");
if(nDLL != NULL)
{
theValue = (GetValue*)GetProcAddress(nDLL, "FastSearch");
if(theValue != NULL)
Edit1->Text = (*theValue)(ADOQuery1);
}
FreeLibrary(nDLL);
}
//---------------------------------------------------------------------------
跟踪运行到Edit1->Text = (*theValue)(ADOQuery1);报错,也就是说调用Dll函数出错,请指教!
用PChar传吧。把你的导出函数改了看看function FastSearch(ADOQuery1: TADOQuery; var ResultBufferLength: integer):PChar;stdcall;
DLL里分配内存,主程序里用完了,根据ResultBufferLength的值释放掉。这是一个问题,另外看不出是不是还有别的什么。。
SysUtils,
Classes,
DB,
ADODB;{$R *.res}procedure FastSearch(ADOQuery1: TADOQuery; szSQL: PChar; nLen: Integer); stdcall;
var strSQL: String;
begin
// 我这里没有TFastSearch,只好简单一下了。
strSQL := 'test, test.';
StrLCopy(szSQL, PChar(strSQL), nLen);
end;exports
FastSearch;begin
end.
C++ Builer 单元:
typedef void (__stdcall *FASTSEARCH)(TADOQuery *aq, char *lpSQL, int nLen);
FASTSEARCH FastSearch;void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE hDll = LoadLibrary("Project1.dll");
if(hDll)
{
FastSearch = (FASTSEARCH)GetProcAddress(hDll, "FastSearch");
if(FastSearch)
{
char szSQL[255];
FastSearch(ADOQuery1, szSQL, 255);
Edit1->Text = szSQL;
}
}
FreeLibrary(hDll);
}
可以在BCB里面跟踪调试一下吧.