1

I have C header with defines like

#define tds_accept      accept
#define tds_getpeername getpeername
#define tds_getsockopt  getsockopt
#define tds_getsockname getsockname
#define tds_recvfrom    recvfrom

where I should convert defines to Delphi and assign functions (from Winsock2). I tried both const and type

type
  tds_accept      = accept;
  tds_getpeername = getpeername;
  tds_getsockopt  = getsockopt;
  tds_getsockname = getsockname;
  tds_recvfrom    = recvfrom;

and have errors E2007 Constant or type identifier expected or E2035 Not enough actual parameters
Is it possible to do it with Delphi?

1

2 Answers 2

4

Use typed variables instead, eg:

var
  tds_accept      : function(s: TSocket; addr: PSockAddr; addrlen: PInteger): TSocket; stdcall;
  tds_getpeername : function(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer; stdcall;
  tds_getsockopt  : function(s: TSocket; level, optname: Integer; optval: PAnsiChar; var optlen: Integer): Integer; stdcall;
  tds_getsockname : function(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer; stdcall;
  tds_recvfrom    : function(s: TSocket; var Buf; len, flags: Integer; var from: TSockAddr; var fromlen: Integer): Integer; stdcall;

initialization
  tds_accept      := accept;
  tds_getpeername := getpeername;
  tds_getsockopt  := getsockopt;
  tds_getsockname := getsockname;
  tds_recvfrom    := recvfrom;

Or:

type
  lpfn_accept      = function(s: TSocket; addr: PSockAddr; addrlen: PInteger): TSocket; stdcall;
  lpfn_getpeername = function(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer; stdcall;
  lpfn_getsockopt  = function(s: TSocket; level, optname: Integer; optval: PAnsiChar; var optlen: Integer): Integer; stdcall;
  lpfn_getsockname = function(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer; stdcall;
  lpfn_recvfrom    = function(s: TSocket; var Buf; len, flags: Integer; var from: TSockAddr; var fromlen: Integer): Integer; stdcall;

var
  tds_accept      : lpfn_accept       = accept;
  tds_getpeername : lpfn_getpeername  = getpeername;
  tds_getsockopt  : lpfn_getsockopt   = getsockopt;
  tds_getsockname : lpfn_getsockname  = getsockname;
  tds_recvfrom    : lpfn_recvfrom     = recvfrom;
Sign up to request clarification or add additional context in comments.

Comments

0

Delphi does not have a pre-processor. So you typically find different ways to solve problems that are handled in C/C++ with the pre-processor. Personally, I think I'd use load time linking like this:

function tds_accept(...): TSocket; external dllname name 'accept';

and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.