[glib/nirbheek/preliminary-uwp-support] gmodule: Add support for loading UWP packaged DLLs



commit be56d90fe93e1e9cefe3251e2d038b826341dcd7
Author: Nirbheek Chauhan <nirbheek centricular com>
Date:   Mon May 6 19:13:35 2019 +0530

    gmodule: Add support for loading UWP packaged DLLs
    
    LoadLibrary() is not available when building for the Universal Windows
    Platform, which is used for shipping apps to the Windows Store on all
    devices (Windows Desktop, Windows Phone, Surface, XBox, etc).
    
    Apps are not allowed to load arbitrary DLLs from the system. The only
    DLLs they can load are those that are bundled with the app as assets.
    LoadPackagedLibrary() can be used to access those assets by filename.
    
    The function is meant to be a drop-in replacement for LoadLibrary(),
    and the HANDLE returned can be treated the same as before.

 gmodule/gmodule-win32.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
---
diff --git a/gmodule/gmodule-win32.c b/gmodule/gmodule-win32.c
index 20459f372..1c7226a68 100644
--- a/gmodule/gmodule-win32.c
+++ b/gmodule/gmodule-win32.c
@@ -39,6 +39,12 @@
 #include <sys/cygwin.h>
 #endif
 
+/* Default family is DESKTOP_APP which is DESKTOP | APP
+ * We want to know when we're only building for apps */
+#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
+#define G_WINAPI_ONLY_APP
+#endif
+
 static void
 set_error (const gchar *format,
           ...)
@@ -84,7 +90,15 @@ _g_module_open (const gchar *file_name,
   success = SetThreadErrorMode (SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS, &old_mode);
   if (!success)
     set_error ("");
+
+  /* When building for UWP, load app asset DLLs instead of filesystem DLLs.
+   * Needs MSVC, Windows 8 and newer, and is only usable from apps. */
+#if _WIN32_WINNT >= 0x0602 && defined(G_WINAPI_ONLY_APP)
+  handle = LoadPackagedLibrary (wfilename, 0);
+#else
   handle = LoadLibraryW (wfilename);
+#endif
+
   if (success)
     SetThreadErrorMode (old_mode, NULL);
   g_free (wfilename);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]