gpt4 book ai didi

python - 在delphi XE2中访问DLL,从Python转换

转载 作者:行者123 更新时间:2023-11-30 23:03:01 24 4
gpt4 key购买 nike

我正在尝试访问从 python 转换的 delphiXE2 中的 DLL。

以下是 python 程序的摘录:

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from ctypes import *
from os.path import dirname, join

_dir = dirname(__file__)

try:
mylib = cdll.LoadLibrary(join(_dir, "myAPI.dll"))
except:
print "myAPI.dll not loaded"

const0 = 0
const1 = 1


def libCalculation(data):
""" generic calculation fonction
"""
cr = mylib.libCalculation(c_char_p(data))
return cr

def function1(p1, p2, p3, p4, value=const1):
cr = mylib.function1(
c_double(p1), c_double(p2),
c_double(p3), c_double(p4),
c_int(value)
)
return cr

我尝试在delphi中将调用转换为function1,如下所示:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;

var
Form1: TForm1;

const
const0 = 0;
const1 = 1;

function function1(p1,p2,p3,p4:double; v:integer):double;stdcall; external 'myAPI.dll';


implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var p1,p2,p3,p4,temp:double;
v:integer;
begin
p1:=43.1;
p2:=5.3;
p3:=43.5;
p4:=6.1;
v:=const1;
temp:=function1(p1,p2,p3,p4,v);
edit1.Text:=floattostrf(temp,fffixed,8,3);
end;

end.

在 dll 中正确找到该函数,但出现执行错误:“..浮点堆栈检查”。

我的转换正确吗?我能错过什么?与所使用的类型( double 型、整数型)有关的任何内容吗?我尝试了不同的类型来检查但没有成功......

libCalculation(data) 函数对我来说也是一个谜。如何在Delphi中转换它?

欢迎任何帮助。谢谢

最佳答案

Python 代码使用 cdll,因此调用约定为 cdecl。另外,ctypes 中默认返回类型为 c_int,但您使用了 double。这种不匹配解释了运行时错误。

所以 Delphi 应该是:

function function1(p1,p2,p3,p4: double; v: integer): integer; cdecl; external 'myAPI.dll';

至于另一个函数,它接受一个指向以 null 结尾的 8 位字符数组的指针,并返回整数:

function libCalculation(p: PAnsiChar): integer; cdecl; external 'myAPI.dll';

关于python - 在delphi XE2中访问DLL,从Python转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com