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函数出错,请指教!

解决方案 »

  1.   

    直接传String不行啊。要带上ShareMem单元,否则要么内存泄漏,要么访问无效地址;
    用PChar传吧。把你的导出函数改了看看function FastSearch(ADOQuery1: TADOQuery; var ResultBufferLength: integer):PChar;stdcall;
    DLL里分配内存,主程序里用完了,根据ResultBufferLength的值释放掉。这是一个问题,另外看不出是不是还有别的什么。。
      

  2.   

    不要用String做DLL导出函数的参数。给你写了个简单例子:Delphi单元:library Project1;uses
      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);
    }
      

  3.   

    一般既然安装了BCB环境,那应该不会缺少BORLANDMM.DLL.所以估计不是这儿问题.
    可以在BCB里面跟踪调试一下吧.
      

  4.   

    不好意思,说反了..是在DELPHI中调试...把BCB编译的EXE设为HOST APPLICATION.