移植C联盟

时间:2015-05-06 15:54:34

标签: delphi delphi-xe8

我正在尝试移植一个内部有联合的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;

1 个答案:

答案 0 :(得分:1)

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

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