2

我正在尝试移植一个内部有一个联合的 C 结构。中的默认结构Winapi.Winsock2.pas缺少记录中的某些字段。但这是正确的方法吗?谢谢。

typedef struct _WSACOMPLETION {
  WSACOMPLETIONTYPE Type;
  union {
    struct {
      HWND   hWnd;
      UINT   uMsg;
      WPARAM context;
    } WindowMessage;
    struct {
      LPWSAOVERLAPPED lpOverlapped;
    } Event;
    struct {
      LPWSAOVERLAPPED                    lpOverlapped;
      LPWSAOVERLAPPED_COMPLETION_ROUTINE lpfnCompletionProc;
    } Apc;
    struct {
      LPWSAOVERLAPPED lpOverlapped;
      HANDLE          hPort;
      ULONG_PTR       Key;
    } Port;
  } Parameters;
} WSACOMPLETION;

_WSACOMPLETION = record
  &Type: WSACOMPLETIONTYPE;
  case Parameters: Integer of
    0: (hWnd: HWND; uMsg: UINT; context: WPARAM); // WindowMessage
    1: (lpOverlapped: LPWSAOVERLAPPED); // Event
    2: (lpOverlapped: LPWSAOVERLAPPED; lpfnCompletionProc: LPWSAOVERLAPPED_COMPLETION_ROUTINE); // Apc
    3: (lpOverlapped: LPWSAOVERLAPPED; hPort: HANDLE; Key: ULONG_PTR); //Port
end; 

编译器抱怨Error: E2004 Identifier redeclared: 'lpOverlapped'

更新 2:

经过一番思考,我想出了这个。

  _WSACOMPLETION = record
    &Type: WSACOMPLETIONTYPE;
    case Parameters: Integer of
    0: (WindowMessage: record
        hWnd: HWND;
        uMsg: UINT;
        context: WPARAM;
          end);
    1: (Event: record
        lpOverlapped: LPWSAOVERLAPPED;
         end);
    2: (Apc: record
                lpOverlapped: LPWSAOVERLAPPED;
                lpfnCompletionProc: LPWSAOVERLAPPED_COMPLETION_ROUTINE
       end);
    3: (Port: record
        lpOverlapped: LPWSAOVERLAPPED;
        hPort: HANDLE;
        Key: ULONG_PTR;
         end)
  end;
4

1 回答 1

1

您的翻译很好,因为您必须为记录的每个成员使用单独的名称。

不过,就我个人而言,我会为工会的每个分支定义一个记录。这将允许您为联合的每个分支命名,就像 C 代码中的情况一样。

于 2015-05-06T16:20:33.950 回答