我有一个用Delphi编写的DLL,但我需要在我的Python应用程序中使用它。
在Delphi中,它称之为简单:Calc:=CalcCode(@Data, 32, @Num);
@Data
和@Num
是字节数组的指针:
Data: array[0..31] of byte;
Num: array[0..7] of byte;
如何通过ctypes在我的Python程序中调用此函数?
Delphi dll函数声明:
function CalcCode(Data: pointer; DataSize: integer; Num: pointer):cardinal; stdcall; external 'CalcCode.dll';
答案 0 :(得分:1)
此Python代码演示了如何调用该函数:
import ctypes
lib = ctypes.WinDLL(dllname)
CalcCode = lib.CalcCode
CalcCode.restype = ctypes.c_uint
CalcCode.argtypes = ctypes.POINTER(ctypes.c_byte*32),
ctypes.c_int, ctypes.POINTER(ctypes.c_byte*8)
data = (ctypes.c_byte*32)(*range(32))
num = (ctypes.c_byte*8)()
retval = CalcCode(data, len(data), num)
print retval, list(num)
我使用了这个测试DLL:
library Project1;
function CalcCode(Data: pointer; DataSize: integer; Num: pointer): cardinal; stdcall;
var
i: Integer;
P: PByte;
begin
Result := 0;
P := PByte(Data);
for i := 0 to DataSize-1 do begin
inc(Result, P^);
inc(P);
end;
PInt64(Num)^ := Result*2;
end;
exports
CalcCode;
begin
end.
输出结果为:
496 [-32, 3, 0, 0, 0, 0, 0, 0]