Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问delphi XE2中的DLL,从Python转换_Python_Delphi_Dll - Fatal编程技术网

访问delphi XE2中的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:

我正在尝试访问从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中正确找到,但我得到一个执行错误: “.浮点堆栈检查”

我的转换正确吗?我会错过什么?与所用类型(双精度、整数)相关的任何内容?我尝试了不同类型的检查,但没有成功

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';