From 5e151f3bac051bf85c311e2fa88d0b24fd0b1aba Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Sat, 20 Oct 2012 18:59:48 -0400 Subject: [PATCH] Added similar python plugin improvements for Windows --- src/libcore/fresolver.cpp | 8 +++++--- src/libpython/core.cpp | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/libcore/fresolver.cpp b/src/libcore/fresolver.cpp index 80cf0ba4..8010f024 100644 --- a/src/libcore/fresolver.cpp +++ b/src/libcore/fresolver.cpp @@ -1,4 +1,5 @@ #include +#include #if defined(__WINDOWS__) # include @@ -47,9 +48,10 @@ FileResolver::FileResolver() { // There is an error if and only if the function returns 0 if (nSize != 0) { - prependPath(fs::path(lpFilename).parent_path()); - } - else { + fs::path path(lpFilename); + if (boost::to_lower_copy(path.filename().string()).find("python") == std::string::npos) + prependPath(path.parent_path()); + } else { const std::string msg(lastErrorText()); Log(EError, "Could not detect the executable path! (%s)", msg.c_str()); } diff --git a/src/libpython/core.cpp b/src/libpython/core.cpp index 3c6ca52b..fe404641 100644 --- a/src/libpython/core.cpp +++ b/src/libpython/core.cpp @@ -28,6 +28,8 @@ # include #elif defined(__OSX__) # include +#elif defined(__WINDOWS__) +# include #endif using namespace mitsuba; @@ -71,6 +73,25 @@ void initializeFramework() { if (boost::ends_with(imageName, "mitsuba.so")) basePath = fs::path(imageName).parent_path().parent_path().parent_path(); } + #elif defined(__WINDOWS__) + HMODULE hm; + std::vector lpFilename(MAX_PATH); + if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &initializeFramework, &hm)) { + std::vector lpFilename(MAX_PATH); + + // Try to get the path with the default MAX_PATH length (260 chars) + DWORD nSize = GetModuleFileNameW(hm, &lpFilename[0], MAX_PATH); + + // Adjust the buffer size in case if was too short + while (nSize == lpFilename.size()) { + lpFilename.resize(nSize * 2); + nSize = GetModuleFileNameW(hm, &lpFilename[0], nSize); + } + + if (nSize) + basePath = fs::path(lpFilename).parent_path().parent_path().parent_path(); + } #endif if (!basePath.empty())