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.
This commit is contained in:
Giuseppe Gorgoglione 2016-07-24 13:22:17 +02:00
parent 142d03fdb9
commit 1344df7496

View File

@ -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 != "")