delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__stdcall,采用VC默认格式__cdecl,但在Delphi中要注明调用格式为cdecl。C++中例子:




  1. extern "C" int __declspec(dllexport) add(int x, int y);  


 Delphi中例子:




  1. function add(i:Integer; j:Integer):Integer; cdecl; External 'NonMfcDll.dll';  


2. 声明中加上__stdcallC++中例子:




  1. extern "C" int __declspec(dllexport) __stdcall stdadd(int x, int y);  


因为加上__stdcall关键字, 会导致函数名分裂. 此时函数名变成​​​​. 其中, 8是参数的总字节数

Delphi引用的方法1: 在delphi定义中加上"name​


  1. function stdadd(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll' name'_stdadd@8';  


Delphi引用的方法2: 增加def文件, 内容如下



  1. ; NonMfcDll.def : 声明 DLL 的模块参数。  
  2.   
  3. LIBRARY      "NonMfcDll"  
  4.   
  5. EXPORTS  
  6.     ; 此处可以是显式导出  
  7.  stdadd @1  


delphi的定义如下




  1. function add(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll';