From d0ed35d71f09d01cdde1ef3938c6f6b737838517 Mon Sep 17 00:00:00 2001 From: supermerill Date: Thu, 4 Nov 2021 16:29:17 +0100 Subject: [PATCH] Add a fallback method (in windows) for getting the current exe path to open the gcodeviewer (or the opposite) supermerill/SuperSlicer#1778 --- src/slic3r/Utils/Process.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/slic3r/Utils/Process.cpp b/src/slic3r/Utils/Process.cpp index a97df2138c2..dad671af7d2 100644 --- a/src/slic3r/Utils/Process.cpp +++ b/src/slic3r/Utils/Process.cpp @@ -39,6 +39,22 @@ static void start_new_slicer_or_gcodeviewer(const NewSlicerInstanceType instance #ifdef _WIN32 wxString path; wxFileName::SplitPath(wxStandardPaths::Get().GetExecutablePath(), &path, nullptr, nullptr, wxPATH_NATIVE); + //check directory exist + if (true || !boost::filesystem::is_directory(boost::filesystem::wpath(path.ToStdWstring()))) { + BOOST_LOG_TRIVIAL(info) << "Fail to find directory \"" << path << "\", trying another method."; + //try an other way + std::vector pathBuf; + DWORD copied = 0; + do { + pathBuf.resize(pathBuf.size() + MAX_PATH); + copied = GetModuleFileName(0, pathBuf.data(), pathBuf.size()); + } while (copied >= pathBuf.size()); + pathBuf.resize(copied); + std::wstring path2(pathBuf.begin(), pathBuf.end()); + boost::filesystem::wpath boostpath(path2); + BOOST_LOG_TRIVIAL(info) << "get current path: \"" << into_u8(path2) << "\" which is in dir \""<< boostpath.parent_path().wstring() <<"\""; + path = boostpath.parent_path().wstring(); + } path += "\\"; path += (instance_type == NewSlicerInstanceType::Slicer) ? SLIC3R_APP_CMD ".exe" : GCODEVIEWER_APP_CMD ".exe"; std::vector args; @@ -58,7 +74,7 @@ static void start_new_slicer_or_gcodeviewer(const NewSlicerInstanceType instance // just hang in the background. if (wxExecute(const_cast(args.data()), wxEXEC_ASYNC) <= 0) BOOST_LOG_TRIVIAL(error) << "Failed to spawn a new slicer \"" << into_u8(path); -#else +#else // Win32 (else not) // Own executable path. boost::filesystem::path bin_path = into_path(wxStandardPaths::Get().GetExecutablePath()); #if defined(__APPLE__)