【发布时间】:2013-06-21 17:06:42
【问题描述】:
我正在使用 Delphi 加载一个 dll(我在 Delphi XE-3 中创建),以便与一些 C 代码进行交互。我的问题是弄清楚为什么我的数组没有被传递给 c 函数——它们是唯一没有传递的。 delphi 文件(简化)如下所示:
program CallcCode
uses
SysUtils, Windows,
DLLUnit in 'DLLUnit.pas'; // Header conversion
var
DLLHandle: cardinal;
n: Integer;
A: TArray<Integer>;
result1: Integer;
begin
// Initialize each Array
SetLength(A,n);
A[0] = ...;
// Load the DLL (and confirm its loaded)
DLLhandle := LoadLibrary('dllname.dll');
if DLLhandle <> 0 then
begin
result1 := dll_func1(n,A); // A and B are not passed correctly
end
FreeLibrary(DLLHandle);
end.
我第一次成功“Trace into” dll_func1,进入DLLUnit,里面有:
const
nameofDLL = 'dllname';
function dll_func1(n: Integer; A: TArray<Integer>): Integer; cdecl; external nameofDLL;
再次“Tracing-into”,我到达 c 文件,它仍然具有正确的 n 和 DLLdefs 值,但 A(在“局部变量”标题下)变成了:
[-] A :(Aplha-Numeric)
..[0] 0 (0x00000000)
我知道我至少可以正确访问 DLL(希望如此),因为其他函数调用可以正常工作,并且我能够毫无问题地跟踪到 dll_func1.c 文件。我尝试将功能更改为
function dll_func1(n: Integer; A: PInteger): Integer; cdecl; external nameofDLL;
...
result1 := dll_func1(n,PInteger(A))
或
function dll_func1(n: Integer; A: PInteger): Integer; cdecl; external nameofDLL;
...
result1 := dll_func1(n,@A[0])
(同时使用 TArray 和整数或 A 数组)但没有变化,这让我相信这与我没有看到的问题有关。整个事情编译并运行,但由于 TArray 失败,结果 1 不正确。关于出了什么问题的任何想法?
EDIT C 中的函数为:
int dll_func1(int n, int A [])
【问题讨论】:
-
您只需要按照我在回答您上一个问题时告诉您的操作即可。密切关注我写的内容。你在这里所拥有的和它完全不同。好吧,问题的后半部分是。我怀疑该结构传递错误。为什么不编写一个接受单个数组并尝试传递它的 C 函数。然后变得更加雄心勃勃。
-
我在函数调用(单元文件)中写了
PInteger,在程序文件(我初始化它们的地方)中将它们定义为TArray<Integer>,并使用solve(n, PInteger(Ap), PInteger(Ax));只有数组没有传递给函数。结构传递不正确是什么意思? -
没有。 PInteger 是预定义的类型。不要将其重新定义为其他内容。删除所有声明。它被声明为 PInteger = ^Integer
-
哦,我说的“添加 Pinteger”不是这个意思,我的意思是函数声明将 Ap 和 Ai 设置为 PInteger 类型。
-
无论如何,您需要学习如何解决问题。目前,您正在尝试解决如何将数组从 Delphi 传递到 C。需要一个具有单个参数的函数。在您完全理解之前,不要考虑任何更复杂的事情。将问题分解成小块。
标签: c delphi dll delphi-xe3