Ala*_*ark 2 c++ delphi dll parameter-passing delphi-7
这是通过DLL公开的C++函数:
double my_exposed_cpp_function(int* my_array){
int i = my_array[2]; /* i should be 3 */
my_array[2]++;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的Delphi函数的声明
function dll_function(var my_array: array of integer): Real; stdcall; external myDLL name 'my_exposed_cpp_function';
Run Code Online (Sandbox Code Playgroud)
这是我想在Delphi函数中做的事情
procedure talk_to_dll();
var
return_value: Real;
my_array: array[0..2] of integer;
final_value: integer;
begin
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 3;
return_value := dll_function(my_array);
final_value = my_array[2]; /* my_array[2] should now be 4 */
end;
Run Code Online (Sandbox Code Playgroud)
希望这个例子能让我明白我的意图.我可以使用这个设置来处理更简单的数据类型,所以我知道Delphi和dll之间的通信很好.我需要改变什么才能使其正常工作?
Delphi open数组实际上是使用两个参数传递的:第一个元素的地址和元素数减一.
要调用C++函数,不能使用开放数组.只需将该函数声明为接收指向第一个元素的指针:
function dll_function(my_array: PInteger): Real; stdcall;
external myDLL name 'my_exposed_cpp_function';
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
return_value := dll_function(@my_array[0]);
Run Code Online (Sandbox Code Playgroud)
在某些时候,您可能希望让数组长度动态变化.代码假定该数组有三个元素.为了更加通用,您将传递一个额外的参数来指定数组长度.
我假设C++函数确实是stdcall,尽管问题中的任何内容都没有明确说明.
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |