bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Steve Lhomme <robux4@ycbcr.xyz>
To: bug-gnulib@gnu.org
Subject: Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps
Date: Fri, 29 May 2020 12:49:45 +0200	[thread overview]
Message-ID: <5e34e3e3-2c06-d3c3-80b9-de3b95bd8395@ycbcr.xyz> (raw)
In-Reply-To: <8510036.Z2lXjDNy1T@omega>

On 2020-05-29 2:04, Bruno Haible wrote:
> Hi,
> 
> Steve Lhomme wrote:
>> LoadLibrary is forbidden in such apps (can only load DLLs from within the app
>> package).
>> The API entries are available to all apps linking with the Windows API as found
>> here:
>> https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis
> 
> Thanks for these infos.
> 
>> +#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && _WIN32_WINNT >= 0x0A00 /* _WIN32_WINNT_WIN10 */
> 
> The GetSystemTimePreciseAsFileTime function is available starting with Windows 8,
> therefore
>    - the condition with WINAPI_FAMILY_PARTITION is not necessary,
>    - the condition _WIN32_WINNT >= 0x0A00 is overly restrictive;
>      _WIN32_WINNT >= _WIN32_WINNT_WIN8 will work just as well.

OK. I thought GetSystemTimePreciseAsFileTime was not available in Win8 
UWP apps. But it seems it was: 
https://docs.microsoft.com/en-us/windows/win32/apiindex/windows-81-api-sets

> I have added a page about the native Windows APIs at
> https://gitlab.com/ghwiki/gnow-how/-/wikis/Platforms/Native_Windows
> 
> Then here is a patch to avoid LoadLibrary when possible.
> 
> 
> 2020-05-28  Bruno Haible  <bruno@clisp.org>
> 
> 	Avoid dynamic loading of Windows API functions when possible.
> 	Reported by Steve Lhomme <robux4@ycbcr.xyz> in
> 	<https://lists.gnu.org/archive/html/bug-gnulib/2020-05/msg00182.html>.
> 	* lib/gettimeofday.c (GetProcAddress,
> 	GetSystemTimePreciseAsFileTimeFuncType,
> 	GetSystemTimePreciseAsFileTimeFunc, initialized, initialize): Don't
> 	define in a build for Windows 8 or higher.
> 	* lib/isatty.c (GetProcAddress, GetNamedPipeClientProcessIdFuncType,
> 	GetNamedPipeClientProcessIdFunc, QueryFullProcessImageNameFuncType,
> 	QueryFullProcessImageNameFunc, initialized, initialize): Don't define
> 	in a build for Windows Vista or higher.
> 	* lib/stat-w32.c (GetProcAddress, GetFileInformationByHandleExFuncType,
> 	GetFileInformationByHandleExFunc, GetFinalPathNameByHandleFuncType,
> 	GetFinalPathNameByHandleFunc, initialized, initialize): Likewise.
> 
> diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
> index 1980479..3d53115 100644
> --- a/lib/gettimeofday.c
> +++ b/lib/gettimeofday.c
> @@ -33,9 +33,11 @@
>   
>   #ifdef WINDOWS_NATIVE
>   
> +# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
> +
>   /* Avoid warnings from gcc -Wcast-function-type.  */
> -# define GetProcAddress \
> -   (void *) GetProcAddress
> +#  define GetProcAddress \
> +    (void *) GetProcAddress
>   
>   /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8.  */
>   typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime);
> @@ -54,6 +56,8 @@ initialize (void)
>     initialized = TRUE;
>   }
>   
> +# endif
> +
>   #endif
>   
>   /* This is a wrapper for gettimeofday.  It is used only on systems
> @@ -84,8 +88,10 @@ gettimeofday (struct timeval *restrict tv, void *restrict tz)
>        <http://www.windowstimestamp.com/description>.  */
>     FILETIME current_time;
>   
> +# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
>     if (!initialized)
>       initialize ();
> +# endif
>     if (GetSystemTimePreciseAsFileTimeFunc != NULL)
>       GetSystemTimePreciseAsFileTimeFunc (&current_time);
>     else
> diff --git a/lib/isatty.c b/lib/isatty.c
> index 6cdc0fb..fc771d1 100644
> --- a/lib/isatty.c
> +++ b/lib/isatty.c
> @@ -39,9 +39,11 @@
>   # include <io.h>
>   #endif
>   
> +#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
> +
>   /* Avoid warnings from gcc -Wcast-function-type.  */
> -#define GetProcAddress \
> -  (void *) GetProcAddress
> +# define GetProcAddress \
> +   (void *) GetProcAddress
>   
>   /* GetNamedPipeClientProcessId was introduced only in Windows Vista.  */
>   typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
> @@ -69,6 +71,8 @@ initialize (void)
>     initialized = TRUE;
>   }
>   
> +#endif
> +
>   static BOOL IsConsoleHandle (HANDLE h)
>   {
>     DWORD mode;
> @@ -84,8 +88,10 @@ static BOOL IsCygwinConsoleHandle (HANDLE h)
>     BOOL result = FALSE;
>     ULONG processId;
>   
> +#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
>     if (!initialized)
>       initialize ();
> +#endif
>   
>     /* GetNamedPipeClientProcessId
>        <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getnamedpipeclientprocessid>
> diff --git a/lib/stat-w32.c b/lib/stat-w32.c
> index b9163f5..02ad9ab 100644
> --- a/lib/stat-w32.c
> +++ b/lib/stat-w32.c
> @@ -40,18 +40,20 @@
>   #include "pathmax.h"
>   #include "verify.h"
>   
> +#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
> +
>   /* Avoid warnings from gcc -Wcast-function-type.  */
> -#define GetProcAddress \
> -  (void *) GetProcAddress
> +# define GetProcAddress \
> +   (void *) GetProcAddress
>   
> -#if _GL_WINDOWS_STAT_INODES == 2
> +# if _GL_WINDOWS_STAT_INODES == 2
>   /* GetFileInformationByHandleEx was introduced only in Windows Vista.  */
>   typedef DWORD (WINAPI * GetFileInformationByHandleExFuncType) (HANDLE hFile,
>                                                                  FILE_INFO_BY_HANDLE_CLASS fiClass,
>                                                                  LPVOID lpBuffer,
>                                                                  DWORD dwBufferSize);
>   static GetFileInformationByHandleExFuncType GetFileInformationByHandleExFunc = NULL;
> -#endif
> +# endif
>   /* GetFinalPathNameByHandle was introduced only in Windows Vista.  */
>   typedef DWORD (WINAPI * GetFinalPathNameByHandleFuncType) (HANDLE hFile,
>                                                              LPSTR lpFilePath,
> @@ -66,16 +68,18 @@ initialize (void)
>     HMODULE kernel32 = LoadLibrary ("kernel32.dll");
>     if (kernel32 != NULL)
>       {
> -#if _GL_WINDOWS_STAT_INODES == 2
> +# if _GL_WINDOWS_STAT_INODES == 2
>         GetFileInformationByHandleExFunc =
>           (GetFileInformationByHandleExFuncType) GetProcAddress (kernel32, "GetFileInformationByHandleEx");
> -#endif
> +# endif
>         GetFinalPathNameByHandleFunc =
>           (GetFinalPathNameByHandleFuncType) GetProcAddress (kernel32, "GetFinalPathNameByHandleA");
>       }
>     initialized = TRUE;
>   }
>   
> +#endif
> +
>   /* Converts a FILETIME to GMT time since 1970-01-01 00:00:00.  */
>   #if _GL_WINDOWS_STAT_TIMESPEC
>   struct timespec
> @@ -134,8 +138,10 @@ _gl_fstat_by_handle (HANDLE h, const char *path, struct stat *buf)
>     DWORD type = GetFileType (h);
>     if (type == FILE_TYPE_DISK)
>       {
> +#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
>         if (!initialized)
>           initialize ();
> +#endif
>   
>         /* st_mode can be determined through
>            GetFileAttributesEx
> 


  parent reply	other threads:[~2020-05-29 10:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-19  6:24 [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps Steve Lhomme
2020-05-28  6:24 ` Steve Lhomme
2020-05-29  0:04 ` Bruno Haible
2020-05-29  5:12   ` Daiki Ueno
2020-05-29 10:34     ` Bruno Haible
2020-05-29 10:49   ` Steve Lhomme [this message]
2020-05-29 20:29   ` do not use GetModuleHandle " Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5e34e3e3-2c06-d3c3-80b9-de3b95bd8395@ycbcr.xyz \
    --to=robux4@ycbcr.xyz \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).