Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将C联合转换为delphi?_C_Delphi - Fatal编程技术网

如何将C联合转换为delphi?

如何将C联合转换为delphi?,c,delphi,C,Delphi,我正在将C库转换为Delphi。 我在转换下面的代码时遇到问题。 这是用于通信的结构,因此顺序必须正确 德尔菲 Tparam_union_params_t = packed record case Integer of 0: (param_float:single); 1: (param_int32:Int32); 2: (param_uint32:UInt32); ... ... end; Tparam_union_t = packed recor

我正在将C库转换为Delphi。 我在转换下面的代码时遇到问题。 这是用于通信的结构,因此顺序必须正确

德尔菲

Tparam_union_params_t = packed record
  case Integer of
    0: (param_float:single);
    1: (param_int32:Int32);
    2: (param_uint32:UInt32);
    ...
    ...
end;

Tparam_union_t = packed record
  param:Tparam_union_params_t // This method requires var name.
  type:UInt8;
end;
C郎

#ifdef __GNUC__
  #define PACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
#else
  #define PACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
#endif

PACKED(
typedef struct param_union {
    union {
        float param_float;
        int32_t param_int32;
        uint32_t param_uint32;
        int16_t param_int16;
        uint16_t param_uint16;
        int8_t param_int8;
        uint8_t param_uint8;
        uint8_t bytes[4];
    }; // This no-named union. no-named is important.
    uint8_t type;
}) param_union_t;
我的方法需要变量名 但是原始的c代码没有命名。
如何将C中的匿名联合或结构转换为Delphi?

您拥有的还不错,但在我的文章中,我介绍了一种更好的技术来处理这种没有名称的联合:

param\u union\u p=^param\u union\t;
param_union_t=压缩记录
大小写整数
0:(参数浮点数:单个);
1:(参数int32:int32);
2:(param_uint32:uint32;//将联合后的成员添加到最大的分支。
&类型:UInt8);
3:(参数int16:int16);
...
...
结束;
PParamUnion=^TParamUnion;
TParamUnion=参数union\t;
除了在
UInt32
分支中,还可以将其添加到相同大小的
单个
Int32
分支中。这仍然会导致与C中的结构相同的内存布局,偏移量为4时使用
&type
,记录的大小为5,这就是最重要的。只需查看文章中的图表进行澄清:

这样,就不需要为联合部件指定自己的类型和名称。如果您不相信“技巧”,请使用检查C和Delphi中的偏移量


Borland和Embarcadero,还有Delphi绝地,使用同样的技巧来翻译匿名联合,Delphi
TVarRec
(用于常量数组)和
TVarType
(用于变体)记录也是这样建立的。

谢谢!我对你的密码有一个问题。为什么使用指针参数union和PParamUnion?我想教它,因为我缺乏经验。:)我已经转换了许多API头,声明指针类型和漂亮的(类似Delphi的)名称总是有意义的,也就是说,没有un_der_分数和限制。C和C++中的T后缀一般与Delphi中的T前缀具有相同的含义,所以对于指针,我使用了一个p后缀,其中一个在delphi中使用p前缀。像
PSomething=^TSomething这样的行在此类翻译中非常常见。