Recent

Author Topic: external static C library  (Read 3378 times)

Duvel

  • Newbie
  • Posts: 6
external static C library
« on: January 16, 2015, 10:09:29 am »
Hi,

I use an external C-library; but apperently there's something wrong in the type-conversion. Could anyone help?

This is the C-function:

LONG WINAPI MBTConnect(
 IN LPCTSTR szHostAddress, // TCP/IP address of device
 IN WORD port, // TCP port in device for communication
 IN BOOL useTCPorUDP, // TRUE - TCP; FALSE - UDP
 IN DWORD requestTimeout, // maximal time for managing an I/O request (ms)
 OUT HANDLE *hSocket // handle of the connected socket
 );



This is the code that I use:

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, ShellApi;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    {private declarations}
    Chandle_: Thandle;
    Ret:longword;
  public
    { public declarations }
  const
       ipadr_: pchar = '192.168.1.222';
       port_:integer = 502;
       UseTCP_:boolean= FALSE;
       ReqTime_:longword = 1000;

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

function MBTInit():integer;cdecl;external 'MBT.dll';
procedure MBTConnect(adr:pchar;port:boolean;UseTCP:longword;ReqTime:longword;Chandle:Thandle);cdecl;external 'MBT.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
     if MBTInit() = 0 then
        begin
          MBTConnect(ipadr_,port_,UseTCP_,ReqTime_,Chandle_);
        end;
end;


The compiler links it alle together but I get always an 'External SIGSEGV' error when running the program

thx,
T
« Last Edit: January 16, 2015, 01:04:35 pm by Duvel »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: external static C library
« Reply #1 on: January 16, 2015, 10:30:36 am »
WINAPI is a macro that's usually is expanded to __stdcall not cdecl.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Duvel

  • Newbie
  • Posts: 6
Re: external static C library
« Reply #2 on: January 16, 2015, 11:40:53 am »
Hi, thx, but this didn't help. :(

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12853
  • FPC developer.
Re: external static C library
« Reply #3 on: January 16, 2015, 11:42:52 am »
The last argument is a pointer, make it PTHandle or ;var chandle:THandle)

Duvel

  • Newbie
  • Posts: 6
Re: external static C library
« Reply #4 on: January 16, 2015, 02:26:02 pm »
Works!! Thx! I'm rather very new to Delphi and Free Pascal, so apparently I have to read a lot about pointer-arithmetic and type definitions in FPC.
But I ran in another problem: http://forum.lazarus.freepascal.org/index.php?topic=24607.0

grt,
T
« Last Edit: January 16, 2015, 02:27:57 pm by Duvel »

 

TinyPortal © 2005-2018