[Mingw-users] Including winsock2.h considered harmful?

Back to archive index

Keith Marshall keith****@users*****
Sun Jul 29 03:40:45 JST 2018


On 28/07/18 14:12, Eli Zaretskii wrote:
> I've upgraded to MinGW runtime 5.0.2, and several programs now fail to
> compile because they include winsock2.h and then expect error codes
> from winerror.h to be defined.  Here's a typical example, from Gawk:
> 
>   gcc -c -D__USE_MINGW_ANSI_STDIO -DHAVE_LIBREADLINE -DHAVE_MPFR -O2 -gdwarf-2 -g3 -Wno-deprecated-declarations -DGAWK -I. -I./support -DHAVE_CONFIG_H -DDEFLIBPATH="\"d:/usr/lib/gawk\"" -DSHLIBEXT="\"dll\"" -DLOCALEDIR="\"d:/usr/share/locale\"" gawkmisc.c
>   In file included from gawkmisc.c:36:0:
>   pc/gawkmisc.pc: In function 'dlopen':
>   pc/gawkmisc.pc:756:18: error: 'ERROR_INVALID_PARAMETER' undeclared (first use in this function); did you mean 'WSA_INVALID_PARAMETER'?
> 	 last_err = ERROR_INVALID_PARAMETER;
> 		    ^~~~~~~~~~~~~~~~~~~~~~~
> 		    WSA_INVALID_PARAMETER
>   pc/gawkmisc.pc:756:18: note: each undeclared identifier is reported only once for each function it appears in
>   pc/gawkmisc.pc: In function 'dlsym':
>   pc/gawkmisc.pc:814:18: error: 'ERROR_INVALID_PARAMETER' undeclared (first use in this function); did you mean 'WSA_INVALID_PARAMETER'?
> 	 last_err = ERROR_INVALID_PARAMETER;
> 		    ^~~~~~~~~~~~~~~~~~~~~~~
> 		    WSA_INVALID_PARAMETER
>   pc/gawkmisc.pc: In function 'w32_maybe_set_errno':
>   pc/gawkmisc.pc:1013:12: error: 'ERROR_BAD_PIPE' undeclared (first use in this function); did you mean 'LZERROR_BADVALUE'?
> 	 case ERROR_BAD_PIPE:
> 	      ^~~~~~~~~~~~~~
> 	      LZERROR_BADVALUE
>   pc/gawkmisc.pc:1014:12: error: 'ERROR_PIPE_BUSY' undeclared (first use in this function)
> 	 case ERROR_PIPE_BUSY:
> 	      ^~~~~~~~~~~~~~~
>   pc/gawkmisc.pc:1015:12: error: 'ERROR_NO_DATA' undeclared (first use in this function); did you mean 'IMM_ERROR_NODATA'?
> 	 case ERROR_NO_DATA:
> 	      ^~~~~~~~~~~~~
> 	      IMM_ERROR_NODATA
>   pc/gawkmisc.pc:1016:12: error: 'ERROR_PIPE_NOT_CONNECTED' undeclared (first use in this function); did you mean 'WN_NOT_CONNECTED'?
> 	 case ERROR_PIPE_NOT_CONNECTED:
> 	      ^~~~~~~~~~~~~~~~~~~~~~~~
> 	      WN_NOT_CONNECTED
> 
> This happens because some header included by the source file does
> this:
> 
>   #if !defined _WIN32_WINNT || _WIN32_WINNT < 0x501
>   # undef _WIN32_WINNT
>   # define _WIN32_WINNT 0x501
>   #endif
>   #include <winsock2.h>
>   #include <ws2tcpip.h>
> 
> This used to work with MinGW runtime 3.x, but now fails, evidently
> because winsock2.h no longer includes windows.h,

Actually, it does, but indirectly via <winsock.h>, with which it now
shares code which is common to WinSock v1.1 and v2 protocols.

> and this doesn't define the ERROR_* macros.  Instead, it does this:
> 
>   #define WSA_IO_PENDING                  (ERROR_IO_PENDING)
>   #define WSA_IO_INCOMPLETE               (ERROR_IO_INCOMPLETE)
>   #define WSA_INVALID_HANDLE              (ERROR_INVALID_HANDLE)
> 
> etc, i.e. it redirects winsock errors to "common" errors.

It has always done this; I guess it's now more selective about what it
actually includes from <winerror.h>.

> But it doesn't seem to take care of defining those common errors.

That it gratuitously depends on the above three (and three more), which
it filters out, is a bug.

> So I need to include winerror.h before winsock2.h, in order to have
> those error codes defined.

It shouldn't matter which order you include them; <winsock2.h> first,
followed by <winerror.h> should work just as well.

> Do these programs do something that isn't "kosher" nowadays?  What
> should a program do if it wants to include winsock2.h, but doesn't
> want to include windows.h before that?  Is that allowed?

Yes.  In fact, IIRC, it is required for strict MSVC compatibility.

What is less clear cut is: should <winsock2.h>, (and indeed
<winsock.h>), unconditionally expose all of <winerror.h>, or just those
definitions which are relevant for the WinSock protocols?  Certainly,
the six error codes, on which <winsock2.h> gratuitously depends, must be
exposed, but that won't be sufficient to define other error codes, which
appear to be undefined in your compilation, unless you also explicitly
include <winerror.h>, or its WinSock selectivity filters are removed.

On balance, it may be best to just expose all of <winerror.h>, whenever
it is included; I think that's what happened, (via <windows.h>), in the
earlier version, where the selectivity filters were different: they were
in place to filter out blocks of conflicting WinSock specific error
codes, which were "duplicated" (inconsistently) in <winsock.h>,
<winsock2.h>, and <winerror.h>

-- 
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
Url : https://lists.osdn.me/mailman/archives/mingw-users/attachments/20180728/46f6a366/attachment-0001.pgp 



More information about the MinGW-Users mailing list
Back to archive index