访问delphi XE2中的DLL,从Python转换
我正在尝试访问从python转换而来的delphiXE2中的DLL 以下是python程序的摘录:访问delphi XE2中的DLL,从Python转换,python,delphi,dll,Python,Delphi,Dll,我正在尝试访问从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:
#!/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中正确找到,但我得到一个执行错误:
“.浮点堆栈检查”
我的转换正确吗?我会错过什么?与所用类型(双精度、整数)相关的任何内容?我尝试了不同类型的检查,但没有成功
libCalculation(data)函数对我来说也是个谜。如何在Delphi中转换
欢迎任何帮助。
谢谢Python代码使用了
cdll
,因此调用约定是cdecl
。另外,在ctypes
中,返回类型默认为c_int
,但您使用了double
。这种不匹配可以解释运行时错误
因此,德尔菲应该是:
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';