Properly call external dll in Delphi?

ELCouz picture ELCouz · Jan 1, 2015 · Viewed 8.1k times · Source

I know few basics of Delphi (In fact I've been using it for few years)...

I'm hitting a wall with DLL's (never really play with this).

Consider this example:

   unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

Type FT_Result = Integer;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;
  FT_HANDLE : DWord = 0;



implementation

{$R *.dfm}

function I2C_GetNumChannels(numChannels: dword):FT_Result; stdcall; external 'libmpsse.dll' name 'I2C_GetNumChannels';
function I2C_OpenChannel(index:dword;handle:pointer):FT_Result; stdcall; external 'libmpsse.dll' name 'I2C_OpenChannel';

procedure TForm1.Button1Click(Sender: TObject);
var
numofchannels:dword;
begin
i2c_getnumchannels(numofchannels);
showmessage(inttostr(numofchannels));
end;

end.

I need to interface the libmpsse.dll from FTDI to access a I2C device on the USB port.
When i'm calling the function I2C_GetNumChannels I get tons of AccessViolation...

I just want to know what's wrong with the dll function?

Also I2C_GetNumChannels is supposed to returns 2 values...

enter image description here

From the official API guide here -->http://www.ftdichip.com/Support/Documents/AppNotes/AN_177_User_Guide_For_LibMPSSE-I2C.pdf

Thank you very much!

Regards

Answer

David Heffernan picture David Heffernan · Jan 1, 2015

Your translation is incorrect. It should be:

function I2C_GetNumChannels(out numChannels: Longword): FT_Result;
  stdcall; external 'libmpsse.dll';

The function you are calling accepts the address of a 32 bit unsigned integer. Your translation passed a 32 bit unsigned integer by value.

You could translate the import using pointers but it's easier for the caller to do it using a var or out parameter, as I have done.

I'm assuming that you have correctly determined that the calling convention is stdcall. You'd need to check the header file to know for sure.

You should check the value returned from the function call for errors. This is the single most common mistake that people make when calling external libraries. Don't ignore return values. Check for errors.