bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH v2] win32: don't force _WIN32_WINNT to a lower version
@ 2020-05-26 11:43 Steve Lhomme
  2020-05-26 20:38 ` Bruno Haible
  0 siblings, 1 reply; 5+ messages in thread
From: Steve Lhomme @ 2020-05-26 11:43 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Steve Lhomme

---
 lib/ftruncate.c   | 2 ++
 lib/sethostname.c | 6 ++++--
 lib/stat-w32.c    | 6 ++++--
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/lib/ftruncate.c b/lib/ftruncate.c
index a1856374e..990b71dbb 100644
--- a/lib/ftruncate.c
+++ b/lib/ftruncate.c
@@ -30,8 +30,10 @@
    argument.  So, define a 64-bit safe SetFileSize function ourselves.  */
 
 /* Ensure that <windows.h> declares GetFileSizeEx.  */
+#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0500
 #  undef _WIN32_WINNT
 #  define _WIN32_WINNT _WIN32_WINNT_WIN2K
+#endif
 
 /* Get declarations of the native Windows API functions.  */
 #  define WIN32_LEAN_AND_MEAN
diff --git a/lib/sethostname.c b/lib/sethostname.c
index 87b3af958..379e6ca13 100644
--- a/lib/sethostname.c
+++ b/lib/sethostname.c
@@ -88,8 +88,10 @@ sethostname (const char *name, size_t len)
 /* Native Windows API.  Also used on Cygwin.  */
 
 /* Ensure that <windows.h> declares SetComputerNameEx.  */
-#undef _WIN32_WINNT
-#define _WIN32_WINNT _WIN32_WINNT_WIN2K
+#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0500
+#  undef _WIN32_WINNT
+#  define _WIN32_WINNT _WIN32_WINNT_WIN2K
+#endif
 
 #define WIN32_LEAN_AND_MEAN
 
diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index 5048093e6..69b06740f 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -21,8 +21,10 @@
 #if defined _WIN32 && ! defined __CYGWIN__
 
 /* Ensure that <windows.h> defines FILE_ID_INFO.  */
-#undef _WIN32_WINNT
-#define _WIN32_WINNT _WIN32_WINNT_WIN8
+#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602
+#  undef _WIN32_WINNT
+#  define _WIN32_WINNT _WIN32_WINNT_WIN8
+#endif
 
 #include <sys/types.h>
 #include <sys/stat.h>
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] win32: don't force _WIN32_WINNT to a lower version
  2020-05-26 11:43 [PATCH v2] win32: don't force _WIN32_WINNT to a lower version Steve Lhomme
@ 2020-05-26 20:38 ` Bruno Haible
  2020-05-27  6:16   ` Steve Lhomme
  0 siblings, 1 reply; 5+ messages in thread
From: Bruno Haible @ 2020-05-26 20:38 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Steve Lhomme

Hi Steve,

>  
>  /* Ensure that <windows.h> declares GetFileSizeEx.  */
> +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0500
>  #  undef _WIN32_WINNT
>  #  define _WIN32_WINNT _WIN32_WINNT_WIN2K
> +#endif
>  

What do you gain by this? What does it bring to compile this code
with a higher _WIN32_WINNT value?

Bruno



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] win32: don't force _WIN32_WINNT to a lower version
  2020-05-26 20:38 ` Bruno Haible
@ 2020-05-27  6:16   ` Steve Lhomme
  2020-05-28  0:12     ` Bruno Haible
  0 siblings, 1 reply; 5+ messages in thread
From: Steve Lhomme @ 2020-05-27  6:16 UTC (permalink / raw)
  To: bug-gnulib

Hi,

On 2020-05-26 22:38, Bruno Haible wrote:
> Hi Steve,
> 
>>   
>>   /* Ensure that <windows.h> declares GetFileSizeEx.  */
>> +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0500
>>   #  undef _WIN32_WINNT
>>   #  define _WIN32_WINNT _WIN32_WINNT_WIN2K
>> +#endif
>>   
> 
> What do you gain by this? What does it bring to compile this code
> with a higher _WIN32_WINNT value?

It's general good practice. In another file I modified I did not 
understand why I was still getting the old APIs when I was selecting 
Win10. So I modified the places where the value is enforced. It's future 
proof when editing files.

When building for UWP it's particularly important as a lot of APIs may 
be hidden if you use the proper values and they are hidden because you 
shouldn't use them. Forcing to use them when they shouldn't will lead to 
apps that don't run.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] win32: don't force _WIN32_WINNT to a lower version
  2020-05-27  6:16   ` Steve Lhomme
@ 2020-05-28  0:12     ` Bruno Haible
  2020-05-28  5:33       ` Steve Lhomme
  0 siblings, 1 reply; 5+ messages in thread
From: Bruno Haible @ 2020-05-28  0:12 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Steve Lhomme

Hi Steve,

> When building for UWP it's particularly important as a lot of APIs may 
> be hidden if you use the proper values and they are hidden because you 
> shouldn't use them. Forcing to use them when they shouldn't will lead to 
> apps that don't run.

Oh, so Microsoft is not only adding new APIs from one version to the next,
but sometimes also removing APIs. Indeed, the Microsoft doc says this:

  "Over the years, Windows APIs and data types have been added, and
   sometimes changed or removed."

<https://docs.microsoft.com/en-us/cpp/porting/overview-of-potential-upgrade-issues-visual-cpp>

Applied like this. (I couldn't take your patch as-is, because in Gnulib
we indent preprocessor lines in a particular way.)


2020-05-27  Bruno Haible  <bruno@clisp.org>

	Improve pattern for defining _WIN32_WINNT.
	Newer versions of the Windows API may not only add, but also remove API
	functions. Therefore, when the user is e.g. building for Windows 10, we
	should not set _WIN32_WINNT to e.g. Windows 8, as this may enable the
	use of APIs that were present in Windows 8 but removed in Windows 10.
	Suggested by Steve Lhomme <robux4@ycbcr.xyz> in
	<https://lists.gnu.org/archive/html/bug-gnulib/2020-05/msg00318.html>.
	* lib/ftruncate.c (_WIN32_WINNT): Don't set to a smaller value.
	* lib/sethostname.c (_WIN32_WINNT): Likewise.
	* lib/stat-w32.c (_WIN32_WINNT): Likewise.

diff --git a/lib/ftruncate.c b/lib/ftruncate.c
index a185637..2514075 100644
--- a/lib/ftruncate.c
+++ b/lib/ftruncate.c
@@ -30,8 +30,10 @@
    argument.  So, define a 64-bit safe SetFileSize function ourselves.  */
 
 /* Ensure that <windows.h> declares GetFileSizeEx.  */
-#  undef _WIN32_WINNT
-#  define _WIN32_WINNT _WIN32_WINNT_WIN2K
+#  if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
+#   undef _WIN32_WINNT
+#   define _WIN32_WINNT _WIN32_WINNT_WIN2K
+#  endif
 
 /* Get declarations of the native Windows API functions.  */
 #  define WIN32_LEAN_AND_MEAN
diff --git a/lib/sethostname.c b/lib/sethostname.c
index 1be69be..96318fb 100644
--- a/lib/sethostname.c
+++ b/lib/sethostname.c
@@ -23,11 +23,11 @@
 /* Unix API.  */
 
 /* Specification.  */
-#include <unistd.h>
+# include <unistd.h>
 
-#include <errno.h>
-#include <stdio.h>
-#include <limits.h>
+# include <errno.h>
+# include <stdio.h>
+# include <limits.h>
 
 /* Set up to LEN chars of NAME as system hostname.
    Return 0 if ok, set errno and return -1 on error. */
@@ -43,7 +43,7 @@ sethostname (const char *name, size_t len)
       return -1;
     }
 
-#ifdef __minix /* Minix */
+# ifdef __minix /* Minix */
   {
     FILE *hostf;
     int r = 0;
@@ -76,38 +76,40 @@ sethostname (const char *name, size_t len)
 
     return r;
   }
-#else
+# else
   /* For platforms that we don't have a better option for, simply bail
      out.  */
   errno = ENOSYS;
   return -1;
-#endif
+# endif
 }
 
 #else
 /* Native Windows API.  Also used on Cygwin.  */
 
 /* Ensure that <windows.h> declares SetComputerNameEx.  */
-#undef _WIN32_WINNT
-#define _WIN32_WINNT _WIN32_WINNT_WIN2K
+# if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
+#  undef _WIN32_WINNT
+#  define _WIN32_WINNT _WIN32_WINNT_WIN2K
+# endif
 
-#define WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
 
 /* Specification.  */
-#include <unistd.h>
+# include <unistd.h>
 
-#include <errno.h>
-#include <limits.h>
-#include <string.h>
+# include <errno.h>
+# include <limits.h>
+# include <string.h>
 
-#include <windows.h>
+# include <windows.h>
 /* The mingw header files don't define GetComputerNameEx, SetComputerNameEx.  */
-#ifndef GetComputerNameEx
-# define GetComputerNameEx GetComputerNameExA
-#endif
-#ifndef SetComputerNameEx
-# define SetComputerNameEx SetComputerNameExA
-#endif
+# ifndef GetComputerNameEx
+#  define GetComputerNameEx GetComputerNameExA
+# endif
+# ifndef SetComputerNameEx
+#  define SetComputerNameEx SetComputerNameExA
+# endif
 
 /* Set up to LEN chars of NAME as system hostname.
    Return 0 if ok, set errno and return -1 on error. */
diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index 296ccf1..c4b5de9 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -21,8 +21,10 @@
 #if defined _WIN32 && ! defined __CYGWIN__
 
 /* Ensure that <windows.h> defines FILE_ID_INFO.  */
-#undef _WIN32_WINNT
-#define _WIN32_WINNT _WIN32_WINNT_WIN8
+#if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN8)
+# undef _WIN32_WINNT
+# define _WIN32_WINNT _WIN32_WINNT_WIN8
+#endif
 
 #include <sys/types.h>
 #include <sys/stat.h>



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] win32: don't force _WIN32_WINNT to a lower version
  2020-05-28  0:12     ` Bruno Haible
@ 2020-05-28  5:33       ` Steve Lhomme
  0 siblings, 0 replies; 5+ messages in thread
From: Steve Lhomme @ 2020-05-28  5:33 UTC (permalink / raw)
  To: bug-gnulib

On 2020-05-28 2:12, Bruno Haible wrote:
> Hi Steve,

Hi Bruno,

>> When building for UWP it's particularly important as a lot of APIs may
>> be hidden if you use the proper values and they are hidden because you
>> shouldn't use them. Forcing to use them when they shouldn't will lead to
>> apps that don't run.
> 
> Oh, so Microsoft is not only adding new APIs from one version to the next,
> but sometimes also removing APIs. Indeed, the Microsoft doc says this:
> 
>    "Over the years, Windows APIs and data types have been added, and
>     sometimes changed or removed."
> 
> <https://docs.microsoft.com/en-us/cpp/porting/overview-of-potential-upgrade-issues-visual-cpp>

I've never been hit by an API removal. But with UWP and win10 variants 
they made a set of API's available on all devices but not all of them 
(the full API set is left on Desktop, for now). So apps targeting *all* 
win10 devices should restrict to these API's.

https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis

> Applied like this. (I couldn't take your patch as-is, because in Gnulib
> we indent preprocessor lines in a particular way.)

Thanks a lot.

> 2020-05-27  Bruno Haible  <bruno@clisp.org>
> 
> 	Improve pattern for defining _WIN32_WINNT.
> 	Newer versions of the Windows API may not only add, but also remove API
> 	functions. Therefore, when the user is e.g. building for Windows 10, we
> 	should not set _WIN32_WINNT to e.g. Windows 8, as this may enable the
> 	use of APIs that were present in Windows 8 but removed in Windows 10.
> 	Suggested by Steve Lhomme <robux4@ycbcr.xyz> in
> 	<https://lists.gnu.org/archive/html/bug-gnulib/2020-05/msg00318.html>.
> 	* lib/ftruncate.c (_WIN32_WINNT): Don't set to a smaller value.
> 	* lib/sethostname.c (_WIN32_WINNT): Likewise.
> 	* lib/stat-w32.c (_WIN32_WINNT): Likewise.
> 
> diff --git a/lib/ftruncate.c b/lib/ftruncate.c
> index a185637..2514075 100644
> --- a/lib/ftruncate.c
> +++ b/lib/ftruncate.c
> @@ -30,8 +30,10 @@
>      argument.  So, define a 64-bit safe SetFileSize function ourselves.  */
>   
>   /* Ensure that <windows.h> declares GetFileSizeEx.  */
> -#  undef _WIN32_WINNT
> -#  define _WIN32_WINNT _WIN32_WINNT_WIN2K
> +#  if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
> +#   undef _WIN32_WINNT
> +#   define _WIN32_WINNT _WIN32_WINNT_WIN2K
> +#  endif
>   
>   /* Get declarations of the native Windows API functions.  */
>   #  define WIN32_LEAN_AND_MEAN
> diff --git a/lib/sethostname.c b/lib/sethostname.c
> index 1be69be..96318fb 100644
> --- a/lib/sethostname.c
> +++ b/lib/sethostname.c
> @@ -23,11 +23,11 @@
>   /* Unix API.  */
>   
>   /* Specification.  */
> -#include <unistd.h>
> +# include <unistd.h>
>   
> -#include <errno.h>
> -#include <stdio.h>
> -#include <limits.h>
> +# include <errno.h>
> +# include <stdio.h>
> +# include <limits.h>
>   
>   /* Set up to LEN chars of NAME as system hostname.
>      Return 0 if ok, set errno and return -1 on error. */
> @@ -43,7 +43,7 @@ sethostname (const char *name, size_t len)
>         return -1;
>       }
>   
> -#ifdef __minix /* Minix */
> +# ifdef __minix /* Minix */
>     {
>       FILE *hostf;
>       int r = 0;
> @@ -76,38 +76,40 @@ sethostname (const char *name, size_t len)
>   
>       return r;
>     }
> -#else
> +# else
>     /* For platforms that we don't have a better option for, simply bail
>        out.  */
>     errno = ENOSYS;
>     return -1;
> -#endif
> +# endif
>   }
>   
>   #else
>   /* Native Windows API.  Also used on Cygwin.  */
>   
>   /* Ensure that <windows.h> declares SetComputerNameEx.  */
> -#undef _WIN32_WINNT
> -#define _WIN32_WINNT _WIN32_WINNT_WIN2K
> +# if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
> +#  undef _WIN32_WINNT
> +#  define _WIN32_WINNT _WIN32_WINNT_WIN2K
> +# endif
>   
> -#define WIN32_LEAN_AND_MEAN
> +# define WIN32_LEAN_AND_MEAN
>   
>   /* Specification.  */
> -#include <unistd.h>
> +# include <unistd.h>
>   
> -#include <errno.h>
> -#include <limits.h>
> -#include <string.h>
> +# include <errno.h>
> +# include <limits.h>
> +# include <string.h>
>   
> -#include <windows.h>
> +# include <windows.h>
>   /* The mingw header files don't define GetComputerNameEx, SetComputerNameEx.  */
> -#ifndef GetComputerNameEx
> -# define GetComputerNameEx GetComputerNameExA
> -#endif
> -#ifndef SetComputerNameEx
> -# define SetComputerNameEx SetComputerNameExA
> -#endif
> +# ifndef GetComputerNameEx
> +#  define GetComputerNameEx GetComputerNameExA
> +# endif
> +# ifndef SetComputerNameEx
> +#  define SetComputerNameEx SetComputerNameExA
> +# endif
>   
>   /* Set up to LEN chars of NAME as system hostname.
>      Return 0 if ok, set errno and return -1 on error. */
> diff --git a/lib/stat-w32.c b/lib/stat-w32.c
> index 296ccf1..c4b5de9 100644
> --- a/lib/stat-w32.c
> +++ b/lib/stat-w32.c
> @@ -21,8 +21,10 @@
>   #if defined _WIN32 && ! defined __CYGWIN__
>   
>   /* Ensure that <windows.h> defines FILE_ID_INFO.  */
> -#undef _WIN32_WINNT
> -#define _WIN32_WINNT _WIN32_WINNT_WIN8
> +#if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN8)
> +# undef _WIN32_WINNT
> +# define _WIN32_WINNT _WIN32_WINNT_WIN8
> +#endif
>   
>   #include <sys/types.h>
>   #include <sys/stat.h>
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-05-28  5:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-26 11:43 [PATCH v2] win32: don't force _WIN32_WINNT to a lower version Steve Lhomme
2020-05-26 20:38 ` Bruno Haible
2020-05-27  6:16   ` Steve Lhomme
2020-05-28  0:12     ` Bruno Haible
2020-05-28  5:33       ` Steve Lhomme

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).