Michael
Michael

Reputation: 13

Access to DLL in delphi XE2, converting from Python

I'm trying to access a DLL in delphiXE2 converting from python.

Here is an extract of the python program:

#!/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

I try to convert the call to function1 in delphi like this:

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.

The function is correctly found in the dll but I get an execution error: ".. floating point stack check".

Is my conversion correct? What could I miss? anything related to the types used (double, integer)? I have tried different types to check but no success...

The libCalculation(data) function is also a mystery from me. How to convert that in Delphi?

Any help welcome. Thank you

Upvotes: 1

Views: 290

Answers (1)

David Heffernan
David Heffernan

Reputation: 613352

The Python code uses cdll so the calling convention is cdecl. Also, the return type is c_int by default in ctypes, but you used double. That mismatch explains the runtime error.

So the Delphi should be:

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

As for the other function, it takes a pointer to null-terminated 8 bit character array, and returns integer:

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

Upvotes: 3

Related Questions