Reputation: 147
I am trying to port a C structure which has a union inside. The default structure in Winapi.Winsock2.pas
is missing some fields in the record.
But is this the correct way? Thanks.
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;
The compiler complains with Error: E2004 Identifier redeclared: 'lpOverlapped'
UPDATE 2:
After some thinking I've come up with this.
_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;
Upvotes: 2
Views: 186
Reputation: 613352
Your translation is fine, modulo the fact that you have to use a separate name for each of the members of the record.
Personally though, I would define a record for each branch of the union. That would allow you to give each branch of the union a name, as is the case in the C code.
Upvotes: 1