假设我在DLL中有这个函数
function test_3(sInput, sOutput : PChar; sSize : int64): Integer; stdcall;
var
sTmp : string;
fText : TextFile;
begin
sTmp := '+++ ' + sInput + ' +++';
StrPLCopy(sOutput, PChar(sTmp), sSize);
Result := 69;
AssignFile(fText, 'test.txt');
Rewrite(fText);
Writeln(fText, 'in: ' + sInput);
Writeln(fText, 'out: ' + sOutput);
CloseFile(fText);
end;
在我的Delphi程序中,我这样称呼它
…
Input := EdtIn.Text;
OutputSize := Input.Length + 8;
Output := AllocMem(OutputSize);
RC := test_3(PChar(Input), Output, OutputSize);
EdtOut.Text := Output;
FreeMem(Output);
而且它工作得很好。现在我想从Python脚本中调用该函数
import ctypes as ct
...
myString = "test Delphi 10.3 DLL"
outputsize = len(myString) + 8
…
test_3 = lib.test_3
test_3.restype = ct.c_int
test_3.argtypes = [ct.c_wchar_p, ct.c_wchar_p]
sOutput = ct.create_string_buffer(outputsize)
print("sOutput = " + sOutput.value)
我得到一个错误
ctypes.ArgumentError: argument 2: : wrong type
所以我的问题是:Delphi中AllocMem的Python等价物是什么。 我必须明确指出,当然所有的代码都是示例,在“现实生活”中,我无法访问DLL中的Delphi代码
下面是一个简单而完整的示例,演示如何执行此操作:
Delphi库
调用Delphi库的Python程序
相关问题 更多 >
编程相关推荐