例如,在下面的示例中,应用的逻辑是什么(CASE INTEGER OF)?
这是转换此结构的正确方法吗?
在C.
typedef union _FLT_PARAMETERS { struct { PIO_SECURITY_CONTEXT SecurityContext; ULONG Options; USHORT POINTER_ALIGNMENT FileAttributes; USHORT ShareAccess; ULONG POINTER_ALIGNMENT EaLength; PVOID EaBuffer; LARGE_INTEGER AllocationSize; } Create; struct { PIO_SECURITY_CONTEXT SecurityContext; ULONG Options; USHORT POINTER_ALIGNMENT Reserved; USHORT ShareAccess; PVOID Parameters; // PNAMED_PIPE_CREATE_PARAMETERS } CreatePipe; ...
在德尔福
TCreate = record SecurityContext: PIO_SECURITY_CONTEXT; Options: ULONG; FileAttributes: USHORT; ShareAccess: USHORT; EaLength: ULONG; EaBuffer: PVOID; AllocationSize: LARGE_INTEGER; end; TCreatePipe = Record SecurityContext: PIO_SECURITY_CONTEXT; Options: ULONG; Reserved: USHORT; ShareAccess: USHORT; Parameters: PVOID; end; _FLT_PARAMETERS = Record case integer of 0: (Create: TCreate); 1: (CreatePipe: TCreatePipe): ...
解决方法
Is this the correct way to convert this structure?
联盟被正确翻译.您的Pascal变体记录是处理联合的正确方法.记录的变体部分与C联合处理相同.从documentation:
Records with variant parts are complicated syntactically but deceptively simple semantically. The variant part of a record contains several variants which share the same space in memory. You can read or write to any field of any variant at any time; but if you write to a field in one variant and then to a field in another variant,you may be overwriting your own data.
我能用你的代码看到的唯一问题是宏POINTER_ALIGNMENT.那个宏扩展到了什么?我的期望是,对于32位代码,它将扩展为__declspec(align(4)),对于64位代码,它将扩展为__declspec(align(8)).
假设猜测是正确的,那么在编译32位时,您的Delphi代码已经具有正确的布局.那是因为标有POINTER_ALIGNMENT的每个字段都已经放在4字节边界上.
但是对于64位,记录将无法正确布局.如果您的目标是64位,则必须添加一些额外的填充,因为标记为POINTER_ALIGNMENT的每个成员都将被错误地布局.遗憾的是,Delphi中没有等效的__declspec(align(#)),所以你需要手动添加填充.
如果你确实需要添加这个填充,你应该仔细检查C和Delphi版本是否具有相同的布局.检查每个字段的偏移是否匹配.