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

2024-10-01 07:50:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 函数testinputoutputstringdllctdelphi
1条回答
网友
1楼 · 发布于 2024-10-01 07:50:29

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

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

相关问题 更多 >