Converting c structure with union to Delphi record

给你一囗甜甜゛ 提交于 2019-12-11 15:57:34

问题


I'm wondering if this c structure I've translated to Delphi is incorrect, and if incorrect, how it can be translated properly? Since the union is mid structure it seems it's not as simple to convert this properly. Any help would be much appreciated

typedef struct FWPM_FILTER0_ {
    GUID                   filterKey;
    FWPM_DISPLAY_DATA0     displayData;
    UINT32                 flags;
    GUID                   *providerKey;
    FWP_BYTE_BLOB          providerData;
    GUID                   layerKey;
    GUID                   subLayerKey;
    FWP_VALUE0             weight;
    UINT32                 numFilterConditions;
    FWPM_FILTER_CONDITION0 *filterCondition;
    FWPM_ACTION0           action;
    union {
        UINT64 rawContext;
        GUID   providerContextKey;
    };
    GUID                   *reserved;
    UINT64                 filterId;
    FWP_VALUE0             effectiveWeight;
} FWPM_FILTER0;
type
  FWPM_FILTER0 = record
    filterKey: TGUID;
    displayData: FWPM_DISPLAY_DATA0;
    flags: UINT32;
    providerKey: PGUID;
    providerData: FWP_BYTE_BLOB;
    layerKey: TGUID;
    subLayerKey: TGUID;
    weight: FWP_VALUE0;
    numFilterConditions: UINT32;
    filterCondition: PFWPM_FILTER_CONDITION0;
    action: FWPM_ACTION0;
    case Integer of
      0: (rawContext: UINT64);
      1: (providerContextKey: TGUID;
    reserved: PGUID;
    filterId: UINT64;
    effectiveWeight: FWP_VALUE0);
  end;

回答1:


A variant part of a record has to appear at the end of a record, in Delphi. Because this union appears in the middle of the struct, you will need to declare the union as a separate type in Delphi and then use it in the containing record.




回答2:


Just fold the fields after the CASE block into one of the branches (preferably the largest one)

Declaring a separate record would require changing the way you access it.

P.s. not entirely mine, see Rudy Velthuis' site, http://rvelthuis.de/articles/articles-convert.html



来源:https://stackoverflow.com/questions/56906440/converting-c-structure-with-union-to-delphi-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!