德尔福的C联盟

时间:2015-01-01 23:27:00

标签: delphi delphi-xe7

我想翻译来自winnt.h的某些类型定义,其中包含一些带位域的联合。但是,这是正确的做法吗?我对_TP_CALLBACK_ENVIRON_V3中的联盟特别不确定。

  typedef struct _UMS_SYSTEM_THREAD_INFORMATION {
      ULONG UmsVersion;
      union {
          struct {
              ULONG IsUmsSchedulerThread : 1;
              ULONG IsUmsWorkerThread : 1;
          } DUMMYSTRUCTNAME;
          ULONG ThreadUmsFlags;
      } DUMMYUNIONNAME;
  } UMS_SYSTEM_THREAD_INFORMATION, *PUMS_SYSTEM_THREAD_INFORMATION;

  typedef struct _TP_CALLBACK_ENVIRON_V3 {
    TP_VERSION                         Version;
    PTP_POOL                           Pool;
    PTP_CLEANUP_GROUP                  CleanupGroup;
    PTP_CLEANUP_GROUP_CANCEL_CALLBACK  CleanupGroupCancelCallback;
    PVOID                              RaceDll;
    struct _ACTIVATION_CONTEXT        *ActivationContext;
    PTP_SIMPLE_CALLBACK                FinalizationCallback;
    union {
        DWORD                          Flags;
        struct {
            DWORD                      LongFunction :  1;
            DWORD                      Persistent   :  1;
            DWORD                      Private      : 30;
        } s;
    } u;
    TP_CALLBACK_PRIORITY               CallbackPriority;
    DWORD                              Size;
} TP_CALLBACK_ENVIRON_V3;

type
  UMS_SYSTEM_THREAD_INFORMATION = record
    UmsVersion: ULONG;
    ThreadUmsFlags: ULONG;
    case Integer of
      0:(IsUmsSchedulerThread : ULONG);
      1:(IsUmsWorkerThread : ULONG);
  end;
  PUMS_SYSTEM_THREAD_INFORMATION = ^UMS_SYSTEM_THREAD_INFORMATION;

  TP_CALLBACK_ENVIRON_V3 = record
    Version: TP_VERSION;
    Pool: PTP_POOL;
    CleanupGroup: PTP_CLEANUP_GROUP;
    CleanupGroupCancelCallback: PTP_CLEANUP_GROUP_CANCEL_CALLBACK;
    RaceDll: PVOID;
    ActivationContext: PACTIVATION_CONTEXT; // Pointer
    FinalizationCallback: PTP_SIMPLE_CALLBACK;
    case Flags: DWORD of
      1: (LongFunction: DWORD)
      1: (Persistent: DWORD)
      30: (Private: DWORD)
    end;
    CallbackPriority: TP_CALLBACK_PRIORITY;
    Size: DWORD;
  end;
  PTP_CALLBACK_ENVIRON = ^TP_CALLBACK_ENVIRON_V3;

2 个答案:

答案 0 :(得分:4)

那些: something符号是位字段。没有直接的Pascal等价物。

然而,由于三个位域的组合是一个完整的双字,因此等效的大致轮廓如下:

type
  _TP_CALLBACK_ENVIRON_V3 = record
  ...
    FinalizationCallback: PTP_SIMPLE_CALLBACK;
    case Integer of
    1: (Flags: DWord);
    2: (LongFunctionPersistentPrivate: DWord)
  end;

答案 1 :(得分:1)

如上所述,':'符号是位字段。没有直接的解决方案,但有一种简单的方法来翻译它们,使它们再次有用。请参阅我描述此内容的文章。它使用一组简单的函数来获取或设置一些位并将它们移动到位,并使用相当未知的属性索引以一种聪明的方式(我没有发明它,BTW)来声明它们。

看看这里:Pitfalls of converting

这个想法来自Stack Overflow answer

FWIW,文章还在此类翻译中描述了how to handle unions