在Delphi中使用C ++ .DLL中的函数

Mik*_*der 3 c++ dll export freepascal static-linking

我正在尝试从Delphi中的“近似最近邻居”(ANN)库访问各种功能(实际上,它是Lazarus / FreePascal,但这无关紧要)。

这是C ++中的减速度:

#include <cstdlib>                      // C standard lib defs
#include <ANN/ANNx.h>                   // all ANN includes
#include <ANN/ANNperf.h>                // ANN performance 

using namespace std;                    // make std:: accessible
....
....
void annMaxPtsVisit(            // set limit on max. pts to visit in search
    int                 maxPts)         // the limit
{
    ANNmaxPtsVisited = maxPts;
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试annMaxPtsVisit从Lazarus内部访问该函数的方法(如果您不知道Lazarus是什么,请假装其delphi):

unit unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure annMaxPtsVisit(input:Integer); stdcall;
  private

    { private declarations }
  public
    { public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}
{ TForm1 }
procedure TForm1.annMaxPtsVisit(input: Integer); stdcall; external 'ANN.dll' name 'annMaxPtsVisit';

procedure TForm1.Button1Click(Sender: TObject);
begin
           annMaxPtsVisit(10);
end;

end.
Run Code Online (Sandbox Code Playgroud)

该程序可以编译,但是当我尝试执行该程序时,出现错误消息:

“过程入口点annMaxPtsVisit不能位于动态链接库ANN.dll中”

如果有人可以帮助我解决这个问题,我将非常感激。

这是DLL的导出:

class ANNsampStat ann_average_err   
class ANNsampStat ann_rank_err  
const ANNbd_tree::`vftable' 
const ANNbruteForce::`vftable'  
const ANNkd_tree::`vftable' 
const ANNpointSet::`vftable'    
double * * __cdecl annAllocPts(int,int) 
double * __cdecl annAllocPt(int,double) 
double * __cdecl annCopyPt(int,double *)    
double __cdecl annDist(int,double *,double *)   
protected: void __thiscall ANNkd_tree::SkeletonTree(int,int,int,double * *,int *)   
public: __thiscall ANNbd_tree::ANNbd_tree(class ANNbd_tree const &) 
public: __thiscall ANNbd_tree::ANNbd_tree(class std::basic_istream<char,struct std::char_traits<char> > &)  
public: __thiscall ANNbd_tree::ANNbd_tree(double * *,int,int,int,enum ANNsplitRule,enum ANNshrinkRule)  
public: __thiscall ANNbd_tree::ANNbd_tree(int,int,int)  
public: __thiscall ANNbruteForce::ANNbruteForce(class ANNbruteForce const &)    
public: __thiscall ANNbruteForce::ANNbruteForce(double * *,int,int) 
public: __thiscall ANNkd_tree::ANNkd_tree(class ANNkd_tree const &) 
public: __thiscall ANNkd_tree::ANNkd_tree(class std::basic_istream<char,struct std::char_traits<char> > &)  
public: __thiscall ANNkd_tree::ANNkd_tree(double * *,int,int,int,enum ANNsplitRule) 
public: __thiscall ANNkd_tree::ANNkd_tree(int,int,int)  
public: __thiscall ANNpointSet::ANNpointSet(class ANNpointSet const &)  
public: __thiscall ANNpointSet::ANNpointSet(void)   
public: __thiscall ANNsampStat::ANNsampStat(void)   
public: class ANNbd_tree & __thiscall ANNbd_tree::operator=(class ANNbd_tree const &)   
public: class ANNbruteForce & __thiscall ANNbruteForce::operator=(class ANNbruteForce const &)  
public: class ANNkd_tree & __thiscall ANNkd_tree::operator=(class ANNkd_tree const &)   
public: class ANNpointSet & __thiscall ANNpointSet::operator=(class ANNpointSet const &)    
public: class ANNsampStat & __thiscall ANNsampStat::operator=(class ANNsampStat const &)    
public: double __thiscall ANNsampStat::max(void)    
public: double __thiscall ANNsampStat::mean(void)   
public: double __thiscall ANNsampStat::min(void)    
public: double __thiscall ANNsampStat::stdDev(void) 
public: int __thiscall ANNsampStat::samples(void)   
public: virtual __thiscall ANNbd_tree::~ANNbd_tree(void)    
public: virtual __thiscall ANNbruteForce::~ANNbruteForce(void)  
public: virtual __thiscall ANNkd_tree::~ANNkd_tree(void)    
public: virtual __thiscall ANNpointSet::~ANNpointSet(void)  
public: virtual double * * __thiscall ANNbruteForce::thePoints(void)    
public: virtual double * * __thiscall ANNkd_tree::thePoints(void)   
public: virtual int __thiscall ANNbruteForce::annkFRSearch(double *,double,int,int *,double *,double)   
public: virtual int __thiscall ANNbruteForce::nPoints(void) 
public: virtual int __thiscall ANNbruteForce::theDim(void)  
public: virtual int __thiscall ANNkd_tree::annkFRSearch(double *,double,int,int *,double *,double)  
public: virtual int __thiscall ANNkd_tree::nPoints(void)    
public: virtual int __thiscall ANNkd_tree::theDim(void) 
public: virtual void __thiscall ANNbruteForce::annkSearch(double *,int,int *,double *,double)   
public: virtual void __thiscall ANNkd_tree::annkSearch(double *,int,int *,double *,double)  
public: virtual void __thiscall ANNkd_tree::Dump(enum ANNbool,class std::basic_ostream<char,struct std::char_traits<char> > &)  
public: virtual void __thiscall ANNkd_tree::getStats(class ANNkdStats &)    
public: virtual void __thiscall ANNkd_tree::Print(enum ANNbool,class std::basic_ostream<char,struct std::char_traits<char> > &) 
public: void __thiscall ANNkd_tree::`default constructor closure'(void) 
public: void __thiscall ANNkd_tree::annkPriSearch(double *,int,int *,double *,double)   
public: void __thiscall ANNsampStat::operator+=(double) 
public: void __thiscall ANNsampStat::reset(void)    
void __cdecl annClose(void) 
void __cdecl annDeallocPt(double * &)   
void __cdecl annDeallocPts(double * * &)    
void __cdecl annMaxPtsVisit(int)    
void __cdecl annPrintStats(enum ANNbool)    
void __cdecl annResetCounts(void)   
void __cdecl annResetStats(int) 
void __cdecl annUpdateStats(void)   
Run Code Online (Sandbox Code Playgroud)

Hei*_*cht 5

也许通过索引导入:

procedure annMaxPtsVisit(input: Integer); stdcall; external 'ANN.dll' index 39;
Run Code Online (Sandbox Code Playgroud)

(这是通过Dependecy Walker获得的索​​引)

并且您可能必须更改为cdecl调用约定,具体取决于DLL的构建方式:

procedure annMaxPtsVisit(input: Integer); cdecl; external 'ANN.dll' index 39; // could work
Run Code Online (Sandbox Code Playgroud)

编辑:看起来cdecl是正确的事情,查看您添加的导出。

Edit2:依赖行者还会显示修饰的名称,如@ user786653所怀疑。这似乎也可行:

procedure annMaxPtsVisit(input: Integer); cdecl; external 'ANN.dll' Name '?annMaxPtsVisit@@YAXH@Z';
Run Code Online (Sandbox Code Playgroud)

不过看起来很丑。

  • 请注意,每个索引的导入确实很脆弱(C ++名称混乱不堪)。如果您可以控制源代码的构建,建议您使用`extern“ C”`导出以获取名称`_annMaxPtsVisit`。通常,通过DLL导出C ++接口非常混乱。但这只是附带说明,很高兴它能解决:) (3认同)