GetHandleInformation() function is not available for UWP builds and on
old Windows 3.1 systems. On Windows 9x systems it is available but always
returns error ERROR_CALL_NOT_IMPLEMENTED. See documentation:
https://web.archive.org/web/20021105020332/http://msdn.microsoft.com/library/en-us/sysinfo/base/gethandleinformation.asp
https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation

Use GetModuleHandleA() and GetProcAddress() to resolve GetHandleInformation
function symbol at runtime and do not fail testing passed HANDLE if the
GetHandleInformation() is not available or returns ERROR_CALL_NOT_IMPLEMENTED.
---
 mingw-w64-libraries/winpthreads/src/misc.c | 4 ++++
 mingw-w64-libraries/winpthreads/src/misc.h | 9 ++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/misc.c 
b/mingw-w64-libraries/winpthreads/src/misc.c
index 2fbd87553670..8930550b862f 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -35,6 +35,7 @@
 void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME) = NULL;
 static ULONGLONG (WINAPI *_pthread_get_tick_count_64) (VOID);
 HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR) = NULL;
+BOOL (WINAPI *_pthread_get_handle_information) (HANDLE, LPDWORD) = NULL;
 
 #if defined(__GNUC__) || defined(__clang__)
 #if !defined(__clang__)
@@ -48,6 +49,9 @@ static void winpthreads_init(void)
     HMODULE mod = GetModuleHandleA("kernel32.dll");
     if (mod)
     {
+        _pthread_get_handle_information =
+            (BOOL (WINAPI *)(HANDLE, LPDWORD))(void*) GetProcAddress(mod, 
"GetHandleInformation");
+
         _pthread_get_tick_count_64 =
             (ULONGLONG (WINAPI *)(VOID))(void*) GetProcAddress(mod, 
"GetTickCount64");
 
diff --git a/mingw-w64-libraries/winpthreads/src/misc.h 
b/mingw-w64-libraries/winpthreads/src/misc.h
index 109cdf1c5801..dc11eaff500f 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.h
+++ b/mingw-w64-libraries/winpthreads/src/misc.h
@@ -35,10 +35,7 @@ typedef long long LONGBAG;
 typedef long LONGBAG;
 #endif
 
-#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
-#undef GetHandleInformation
-#define GetHandleInformation(h,f)  (1)
-#endif
+extern BOOL (WINAPI *_pthread_get_handle_information) (HANDLE, LPDWORD);
 
 /* For gcc and clang define DUMMY_WRITABLE_DWORD as C99 compound literal.
  * For other pre-C99 compilers declare DUMMY_WRITABLE_DWORD as static variable.
@@ -51,7 +48,9 @@ static DWORD DUMMY_WRITABLE_DWORD;
 
 #define TEST_HANDLE(h)                                                  \
   (((h) != NULL && (h) != INVALID_HANDLE_VALUE) && (                    \
-     GetHandleInformation((h), &DUMMY_WRITABLE_DWORD)                   \
+     _pthread_get_handle_information == NULL ||                         \
+     _pthread_get_handle_information((h), &DUMMY_WRITABLE_DWORD) ||     \
+     GetLastError() == ERROR_CALL_NOT_IMPLEMENTED                       \
   ))
 
 #define CHECK_HANDLE2(h, e)                                             \
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to