title: Properly call external dll in Delphi?
body: <p>I know few basics of Delphi (In fact I've been using it for few years)...</p>
  
  <p>I'm hitting a wall with DLL's (never really play with this).</p>
  
  <p>Consider this example:</p>
  
  <pre><code>   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.
  </code></pre>
  
  <p>I need to interface the libmpsse.dll from FTDI to access a I2C device on the USB port.<br>
  When i'm calling the function I2C_GetNumChannels I get tons of AccessViolation...</p>
  
  <p>I just want to know what's wrong with the dll function?</p>
  
  <p>Also I2C_GetNumChannels is supposed to returns 2 values...</p>
  
  <p><img src="https://i.stack.imgur.com/gUId8.png" alt="enter image description here"></p>
  
  <p>From the official API guide here --><a href="http://www.ftdichip.com/Support/Documents/AppNotes/AN_177_User_Guide_For_LibMPSSE-I2C.pdf" rel="nofollow noreferrer">http://www.ftdichip.com/Support/Documents/AppNotes/AN_177_User_Guide_For_LibMPSSE-I2C.pdf</a></p>
  
  <p>Thank you very much!</p>
  
  <p>Regards</p>