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 plaincopy

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

Delphi中例子:

[delphi]view plaincopy

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

2. 声明中加上__stdcall

C++中例子:

[cpp]view plaincopy

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 plaincopy

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

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

[plain]view plaincopy

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

delphi的定义如下

[delphi]view plaincopy

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

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