假设我在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:参数2:错误的类型
所以我的问题是:Delphi中的AllocMem与Python等效吗?我必须指出,当然,所有代码都是示例,在“现实生活”中,我无法访问DLL中的Delphi代码。
这里是一个简单而完整的示例,演示了如何执行此操作:
Delphi库
library SO_60391682;
uses
SysUtils;
function testStringOut(Input, Output: PChar; OutputLen: Int64): Integer; stdcall;
var
tmp: string;
begin
tmp := '+++ ' + Input + ' +++';
StrPLCopy(Output, PChar(tmp), OutputLen - 1);
// -1 because of StrPLCopy's handling of null terminator
Result := 0;
end;
exports
testStringOut;
begin
end.
用于调用Delphi库的Python程序
import ctypes
lib = ctypes.WinDLL(r'SO_60391682.dll')
testStringOut = lib.testStringOut
testStringOut.restype = ctypes.c_int
testStringOut.argtypes = ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_int64
output = ctypes.create_unicode_buffer(256)
res = testStringOut('foo', output, len(output))
print('res={}, output={}'.format(res, output.value))