1

我有 C 标头,其定义如下

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

我应该将定义转换为 Delphi 并分配函数(来自 Winsock2)。我都试过consttype

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

并且有错误E2007 Constant or type identifier expected或者E2035 Not enough actual parameters
是否可以使用Delphi来做到这一点?

4

2 回答 2

4

改用类型变量,例如:

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;

或者:

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;
于 2013-11-01T17:38:37.293 回答
0

Delphi 没有预处理器。因此,您通常会找到不同的方法来解决使用预处理器在 C/C++ 中处理的问题。就个人而言,我认为我会像这样使用加载时间链接:

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

等等。

于 2013-11-01T18:49:28.520 回答