delphi調用C++寫的Dll, 當然這個Dll要求是非MFC的Dll, 這樣子才能被delphi調用. 根據C++定義函數的情況, Delphi有不同的相對應的處理方法.
1. 聲明中不加__stdcall,采用VC默認格式__cdecl,但在Delphi中要注明調用格式為cdecl。
C++中例子:
- extern "C" int __declspec(dllexport) add(int x, int y);
Delphi中例子:
- function add(i:Integer; j:Integer):Integer; cdecl; External 'NonMfcDll.dll';
2. 聲明中加上__stdcall
C++中例子:
- extern "C" int __declspec(dllexport) __stdcall stdadd(int x, int y);
因為加上__stdcall關鍵字, 會導致函數名分裂. 此時函數名變成_stdadd@8. 其中, 8是參數的總字節數
Delphi引用的方法1: 在delphi定義中加上"name'_stdadd@8'".
- function stdadd(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll' name'_stdadd@8';
Delphi引用的方法2: 增加def文件, 內容如下
- ; NonMfcDll.def : 聲明 DLL 的模塊參數。
- LIBRARY "NonMfcDll"
- EXPORTS
- ; 此處可以是顯式導出
- stdadd @1
delphi的定義如下
- function add(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll';
http://blog.csdn.net/huang_xw/article/details/7524359