summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author Giuseppe Gorgoglione <gorgogsp@gmail.com>2016-07-24 13:22:17 +0200
committer Giuseppe Gorgoglione <gorgogsp@gmail.com>2016-07-24 13:27:08 +0200
commit1344df74969df64fe90555d51675921882249c93 (patch)
treed85dff5dfee5286664b63fcc238e81d74f5f35a7
parent142d03fdb960d9b62c91c3b6676165844b378541 (diff)
Fix for netlist library when built for WINDOWS with UNICODE defined
When UNICODE is defined LoadLibrary is redefined to LoadLibraryW which expects a widechar string as library name, while libname is always UTF8. Fixed copying TCHAR conversion code from strconv.cpp since netlist is meant to be also a stand-alone library.
-rw-r--r--src/lib/netlist/plib/pdynlib.cpp55
1 files changed, 52 insertions, 3 deletions
diff --git a/src/lib/netlist/plib/pdynlib.cpp b/src/lib/netlist/plib/pdynlib.cpp
index 6aa1f180aa3..2088ab47c1b 100644
--- a/src/lib/netlist/plib/pdynlib.cpp
+++ b/src/lib/netlist/plib/pdynlib.cpp
@@ -5,9 +5,54 @@
*
*/
-#include <plib/pdynlib.h>
+#include "pdynlib.h"
+
#ifdef WIN32
#include "windows.h"
+#include "palloc.h"
+
+namespace plib {
+CHAR *astring_from_utf8(const char *utf8string)
+{
+ WCHAR *wstring;
+ int char_count;
+ CHAR *result;
+
+ // convert MAME string (UTF-8) to UTF-16
+ char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0);
+ wstring = (WCHAR *)alloca(char_count * sizeof(*wstring));
+ MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count);
+
+ // convert UTF-16 to "ANSI code page" string
+ char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, nullptr, 0, nullptr, nullptr);
+ result = palloc_array<CHAR>(char_count);
+ if (result != nullptr)
+ WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, nullptr, nullptr);
+
+ return result;
+}
+
+WCHAR *wstring_from_utf8(const char *utf8string)
+{
+ int char_count;
+ WCHAR *result;
+
+ // convert MAME string (UTF-8) to UTF-16
+ char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0);
+ result = palloc_array<WCHAR>(char_count);
+ if (result != nullptr)
+ MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);
+
+ return result;
+}
+}
+
+#ifdef UNICODE
+#define tstring_from_utf8 plib::wstring_from_utf8
+#else // !UNICODE
+#define tstring_from_utf8 plib::astring_from_utf8
+#endif // UNICODE
+
#else
#include <dlfcn.h>
#endif
@@ -18,14 +63,16 @@ dynlib::dynlib(const pstring libname)
{
#ifdef WIN32
//fprintf(stderr, "win: loading <%s>\n", libname.cstr());
+ TCHAR *buffer = tstring_from_utf8(libname.cstr());
if (libname != "")
- m_lib = LoadLibrary(libname.cstr());
+ m_lib = LoadLibrary(buffer);
else
m_lib = GetModuleHandle(nullptr);
if (m_lib != nullptr)
m_isLoaded = true;
//else
// fprintf(stderr, "win: library <%s> not found!\n", libname.cstr());
+ pfree_array(buffer);
#else
//printf("loading <%s>\n", libname.cstr());
if (libname != "")
@@ -44,8 +91,9 @@ dynlib::dynlib(const pstring path, const pstring libname)
{
// printf("win: loading <%s>\n", libname.cstr());
#ifdef WIN32
+ TCHAR *buffer = tstring_from_utf8(libname.cstr());
if (libname != "")
- m_lib = LoadLibrary(libname.cstr());
+ m_lib = LoadLibrary(buffer);
else
m_lib = GetModuleHandle(nullptr);
if (m_lib != nullptr)
@@ -54,6 +102,7 @@ dynlib::dynlib(const pstring path, const pstring libname)
{
//printf("win: library <%s> not found!\n", libname.cstr());
}
+ pfree_array(buffer);
#else
//printf("loading <%s>\n", libname.cstr());
if (libname != "")