From 3bdd79a3fd6d915c1bddfa46c6b0313cb4ce1b2a Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 22 Sep 2009 23:03:16 +0000 Subject: [PATCH] Correct Windows unicode build issues. --- .../chaiscript/language/chaiscript_engine.hpp | 84 +++++++++++++------ msvc/chai-example/chai-example.vcproj | 3 +- msvc/chaiscript/chaiscript.vcproj | 4 +- msvc/test_module/test_module.vcproj | 3 +- src/example.cpp | 2 +- 5 files changed, 64 insertions(+), 32 deletions(-) diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 453a51a..f4ae4c7 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -89,39 +89,69 @@ namespace chaiscript #ifdef WIN32 - std::string GetErrorMessage(DWORD err) - { - LPSTR lpMsgBuf = 0; - - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - err, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&lpMsgBuf, - 0, NULL ); - - std::string retval; - - if (lpMsgBuf) - { - retval = lpMsgBuf; - } else { - retval = "Unknown error occured"; - } - - LocalFree(lpMsgBuf); - return retval; - } struct Loadable_Module { + template + static std::wstring towstring(const T &str) + { + return std::wstring(str.begin(), str.end()); + } + + template + static std::string tostring(const T &str) + { + return std::string(str.begin(), str.end()); + } + +#ifdef _UNICODE + template + static std::wstring toproperstring(const T &str) + { + return towstring(str); + } +#else + template + static std::string toproperstring(const T &str) + { + return tostring(str); + } +#endif + + static std::string GetErrorMessage(DWORD err) + { +#ifdef _UNICODE + typedef LPWSTR StringType; + std::wstring retval = L"Unknown Error"; +#else + typedef LPSTR StringType; + std::string retval = "Unknown Error"; +#endif + StringType lpMsgBuf = 0; + + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (StringType)&lpMsgBuf, + 0, NULL ); + + if (lpMsgBuf) + { + retval = lpMsgBuf; + } + + LocalFree(lpMsgBuf); + return tostring(retval); + } + struct DLModule { DLModule(const std::string &t_filename) - : m_data(LoadLibrary(t_filename.c_str())) + : m_data(LoadLibrary(toproperstring(t_filename).c_str())) { if (!m_data) { diff --git a/msvc/chai-example/chai-example.vcproj b/msvc/chai-example/chai-example.vcproj index df85bf7..3e3e947 100644 --- a/msvc/chai-example/chai-example.vcproj +++ b/msvc/chai-example/chai-example.vcproj @@ -42,7 +42,7 @@ (boost::bind(&dynamic_object_attribute, "TestType", "attr", _1))), "attr"); + chai.add(fun(boost::function(boost::bind(&Dynamic_Object_Attribute::func, "TestType", "attr", _1))), "attr"); chai.eval("var x = TestType()"); chai.eval("x.attr = \"hi\"");