From cf6b5f69d449c78d81c43b5e8274b9682cd3f1d5 Mon Sep 17 00:00:00 2001
From: Ben Boeckel <ben.boeckel@kitware.com>
Date: Mon, 25 Mar 2019 11:09:27 -0400
Subject: [PATCH] DynamicLoader: use Encoding::ToWindowsExtendedPath for the
 libname

This converts the path to a format suitable for passing to Windows APIs.
---
 CMakeLists.txt    |  3 +++
 DynamicLoader.cxx | 10 ++--------
 2 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8577506c..5fa904fe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -172,6 +172,9 @@ ENDIF()
 IF(KWSYS_USE_Directory)
   SET(KWSYS_USE_Encoding 1)
 ENDIF()
+IF(KWSYS_USE_DynamicLoader)
+  SET(KWSYS_USE_Encoding 1)
+ENDIF()
 IF(KWSYS_USE_FStream)
   SET(KWSYS_USE_Encoding 1)
 ENDIF()
diff --git a/DynamicLoader.cxx b/DynamicLoader.cxx
index 17432f3c..2dc22c24 100644
--- a/DynamicLoader.cxx
+++ b/DynamicLoader.cxx
@@ -4,6 +4,7 @@
 #include KWSYS_HEADER(DynamicLoader.hxx)
 
 #include KWSYS_HEADER(Configure.hxx)
+#include KWSYS_HEADER(Encoding.hxx)
 
 // Work-around CMake dependency scanning limitation.  This must
 // duplicate the above list of headers.
@@ -190,14 +191,7 @@ namespace KWSYS_NAMESPACE {
 DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
   const std::string& libname)
 {
-  DynamicLoader::LibraryHandle lh;
-  int length = MultiByteToWideChar(CP_UTF8, 0, libname.c_str(), -1, NULL, 0);
-  wchar_t* wchars = new wchar_t[length + 1];
-  wchars[0] = '\0';
-  MultiByteToWideChar(CP_UTF8, 0, libname.c_str(), -1, wchars, length);
-  lh = LoadLibraryW(wchars);
-  delete[] wchars;
-  return lh;
+  return LoadLibraryW(Encoding::ToWindowsExtendedPath(libname).c_str());
 }
 
 int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
-- 
GitLab