c++ - Delphi 和 C++,dll 导入

标签 c++ delphi dll

我有一个 DLL,具有导出函数:

int HCalculator::CalculateMC(const char* h, const char* b, const char* d, __int64 numberOfTrials, double* results) 

如何在 Delphi 项目中导入该函数?

好像是这样的:

function CalculateMC(h, b, d: PChar; numberOfTrials: int64; results: ???): integer; stdcall; external 'MYDLL.DLL'; 

但是 double 组呢?如何写下来?我应该如何调用该函数?

最佳答案

results 参数是一个 double 组。假设,为了论证,该数组具有 numberOfTrials 元素。然后你会像这样调用 DLL:

function CalculateMC(h, b, d: PChar; numberOfTrials: int64; 
    results: PDouble): integer; stdcall; external 'MYDLL.DLL';
.....
var
  returnVal: integer;
  results: array of Double;
.....
SetLength(results, numberOfTrials);
returnVal := CalculateMC(h, b, d, numberOfTrials, @results[0]);

我看到的一个主要问题是您的 C++ 函数似乎是一个成员函数。您可以通过在所有其他参数之前向 CalculateMC 添加一个额外参数来从 Delphi 调用它,该参数包含指向 C++ 对象实例的指针。但是您将需要一种获取该实例的方法。显然,您必须在 Delphi 代码中修改 CalculateMC 的声明。假设您的 DLL 将导出一个返回新创建实例的函数。

另一个可能的问题是您在 Delphi 导入中指定了 stdcall,但在 C++ 代码中没有这样做。如果您编译 C++ 并选择为您的函数使用 stdcall,那么接口(interface)将匹配。否则,您根本无法从 Delphi 调用此代码,因为 MS thiscall 在 Delphi 中没有直接等效项。我会在您的 C++ 代码中添加显式 __stdcall

关于c++ - Delphi 和 C++,dll 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9948620/

相关文章:

c++ - D3D11/C++ 像素着色器中的 uv 插值不准确。如何避免?

c++ - WINAPI SQLite C 无法正确地将数据插入数据库

json - 如何使用 SuperObject lib 在 Delphi 中创建 JSON 文件?

c++ - Excel VBA,无法从 DLL 文件中找到 DLL 入口点

c++ - 在 metatrader4 中创建一个基本的 dll

c++ - 在没有蛮力的情况下反转 SHA-1 或 MD5 给定的(小)输入长度

delphi - 如何将文件从 Explorer Shell 拖放到 Delphi 应用程序中的 VirtualTreeView 控件中?

delphi 到 C++ 构建器的转换

.net - 如何在没有 Visual Studios(也可能使用 py2exe)的情况下将 IronPython 应用程序编译为 EXE?

c++ - boost 中 is_same 和 mpl::same_as 的区别