Delphi 調用C/C++的Dll(stdcall關鍵字, 會導致函數名分裂. 此時函數名變成_stdadd@8)


delphi調用C++寫的Dll, 當然這個Dll要求是非MFC的Dll, 這樣子才能被delphi調用. 根據C++定義函數的情況, Delphi有不同的相對應的處理方法.
1. 聲明中不加__stdcall,采用VC默認格式__cdecl,但在Delphi中要注明調用格式為cdecl。
C++中例子:

[cpp]  view plain  copy
 
 print?
  1. extern "C" int __declspec(dllexport) add(int x, int y);  

 Delphi中例子:

[delphi]  view plain  copy
 
 print?
  1. function add(i:Integer; j:Integer):Integer; cdecl; External 'NonMfcDll.dll';  

2. 聲明中加上__stdcall
C++中例子:

[cpp]  view plain  copy
 
 print?
  1. extern "C" int __declspec(dllexport) __stdcall stdadd(int x, int y);  

因為加上__stdcall關鍵字, 會導致函數名分裂. 此時函數名變成_stdadd@8. 其中, 8是參數的總字節數
Delphi引用的方法1: 在delphi定義中加上"name'_stdadd@8'".

[delphi]  view plain  copy
 
 print?
  1. function stdadd(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll' name'_stdadd@8';  

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

[plain]  view plain  copy
 
 print?
  1. ; NonMfcDll.def : 聲明 DLL 的模塊參數。  
  2.   
  3. LIBRARY      "NonMfcDll"  
  4.   
  5. EXPORTS  
  6.     ; 此處可以是顯式導出  
  7.  stdadd @1  

delphi的定義如下

[delphi]  view plain  copy
 
 print?
  1. function add(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll';  

http://blog.csdn.net/huang_xw/article/details/7524359


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM