winpthreads: prefer calling Unicode functions when possible

Signed-off-by: Kirill Makurin <maiddaisuki@outlook.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-libraries/winpthreads/src/affinity.c b/mingw-w64-libraries/winpthreads/src/affinity.c
index 4564121..7beca8a 100644
--- a/mingw-w64-libraries/winpthreads/src/affinity.c
+++ b/mingw-w64-libraries/winpthreads/src/affinity.c
@@ -215,7 +215,7 @@
 
 void winpthreads_affinity_init (void) {
 #ifdef RUNTIME_CHECKS
-  HMODULE kernel32 = GetModuleHandleA ("kernel32.dll");
+  HMODULE kernel32 = WINPTHREADS_AW (GetModuleHandle) (WINPTHREADS_TEXT ("kernel32.dll"));
 
 #if WINPTHREADS_WINNT < WINPTHREADS_WINNT_NT_3_5
   FuncGetProcessAffinityMask ptrGetProcessAffinityMask = NULL;
diff --git a/mingw-w64-libraries/winpthreads/src/misc.c b/mingw-w64-libraries/winpthreads/src/misc.c
index 72dd787..26f3aaf 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -171,8 +171,8 @@
 static void winpthreads_init (void)
 {
 #ifdef RUNTIME_CHECKS
-  HMODULE kernel32   = GetModuleHandleA ("kernel32.dll");
-  HMODULE kernelbase = GetModuleHandleA ("kernelbase.dll");
+  HMODULE kernel32   = WINPTHREADS_AW (GetModuleHandle) (WINPTHREADS_TEXT ("kernel32.dll"));
+  HMODULE kernelbase = WINPTHREADS_AW (GetModuleHandle) (WINPTHREADS_TEXT ("kernelbase.dll"));
 
 #if WINPTHREADS_WINNT < WINPTHREADS_WINNT_NT_3_5
   FuncGetHandleInformation ptrGetHandleInformation = NULL;
diff --git a/mingw-w64-libraries/winpthreads/src/misc.h b/mingw-w64-libraries/winpthreads/src/misc.h
index a82d0f9..57966e7 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.h
+++ b/mingw-w64-libraries/winpthreads/src/misc.h
@@ -239,6 +239,36 @@
 #endif
 
 /**
+ * Many Windows functions have two versions: ANSI and Unicode.
+ *
+ * Unless you are targeting Windows 10 or later and using UTF-8 manifest,
+ * the Unicode versions should always be preferred; otherwise, those
+ * functions will only be able to operate on ANSI and OEM code pages.
+ *
+ * However, Unicode functions are not supported on Win9x systems,
+ * and so ANSI versions have to be used when Win9x support is desired.
+ *
+ * The best approach to handle this is to use Unicode versions through some
+ * kind of wrapper; when Win9x support is enabled, make those wrappers choose
+ * whether to call Unicode or ANSI version at runtime, and make those wrapper
+ * handle conversion from Unicode to appropriate code page.
+ *
+ * In some cases, however, we may call such functions in context where we
+ * pass constant string literals or using runtime checks is not feasible.
+ *
+ * The macros `WINPTHREADS_AW` and `WINPTHREADS_TEXT` allow to call Unicode
+ * or ANSI version depending on whether Win9x support is enabled.
+ */
+#if WINPTHREADS_WIN9X
+#define WINPTHREADS_AW(name)          name ## A
+#define WINPTHREADS_TEXT(text)        text
+#else
+#define WINPTHREADS_AW(name)          name ## W
+#define WINPTHREADS_TEXT_CONCAT(text) L ## text
+#define WINPTHREADS_TEXT(text)        WINPTHREADS_TEXT_CONCAT (text)
+#endif
+
+/**
  * Portability wrapper for `GetHandleInformation`.
  */
 int winpthreads_get_handle_information (void *, unsigned *);
diff --git a/mingw-w64-libraries/winpthreads/src/time.c b/mingw-w64-libraries/winpthreads/src/time.c
index d788c1e..ee991b3 100644
--- a/mingw-w64-libraries/winpthreads/src/time.c
+++ b/mingw-w64-libraries/winpthreads/src/time.c
@@ -389,7 +389,7 @@
 
 void winpthreads_time_init (void) {
 #ifdef RUNTIME_CHECKS
-  HMODULE kernel32   = GetModuleHandleA ("kernel32.dll");
+  HMODULE kernel32 = WINPTHREADS_AW (GetModuleHandle) (WINPTHREADS_TEXT ("kernel32.dll"));
 
 #if WINPTHREADS_WINNT < WINPTHREADS_WINNT_NT_3_51
   FuncGetSystemTimeAsFileTime ptrGetSystemTimeAsFileTime = NULL;