在我的Python脚本中从Delphi DLL中获取PChar参数

时间:2020-02-25 09:48:36

标签: python delphi dll pchar

假设我在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代码。

1 个答案:

答案 0 :(得分:2)

这是一个简单而完整的示例,演示了如何执行此操作:

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.

Python程序调用Delphi库

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))