diff --git a/src/Common.Post.props b/src/Common.Post.props index 5abb0089..113e0a9c 100644 --- a/src/Common.Post.props +++ b/src/Common.Post.props @@ -29,7 +29,7 @@ Use pch.h $(IntDir)pch.pch - _WINDOWS;WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;WINRT_NO_MODULE_LOCK;NOGDICAPMASKS;NOICONS;NOATOM;NOCLIPBOARD;NODRAWTEXT;NOMEMMGR;NOMETAFILE;NOMINMAX;NOOPENFILE;NOSCROLL;NOSERVICE;NOSOUND;NOTEXTMETRIC;NOCOMM;NOKANJI;NOHELP;NOPROFILER;NODEFERWINDOWPOS;NOMCX;%(PreprocessorDefinitions) + _WINDOWS;WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;WINRT_NO_MODULE_LOCK;WIL_SUPPRESS_EXCEPTIONS;NOGDICAPMASKS;NOICONS;NOATOM;NOCLIPBOARD;NODRAWTEXT;NOMEMMGR;NOMETAFILE;NOMINMAX;NOOPENFILE;NOSCROLL;NOSERVICE;NOSOUND;NOTEXTMETRIC;NOCOMM;NOKANJI;NOHELP;NOPROFILER;NODEFERWINDOWPOS;NOMCX;%(PreprocessorDefinitions) MAGPIE_COMMIT_ID=$(CommitId);%(PreprocessorDefinitions) MAGPIE_VERSION_MAJOR=$(MajorVersion);MAGPIE_VERSION_MINOR=$(MinorVersion);MAGPIE_VERSION_PATCH=$(PatchVersion);MAGPIE_VERSION_TAG=$(VersionTag);%(PreprocessorDefinitions) /bigobj %(AdditionalOptions) diff --git a/src/Magpie/main.cpp b/src/Magpie/main.cpp index 756ca7df..ec508e08 100644 --- a/src/Magpie/main.cpp +++ b/src/Magpie/main.cpp @@ -20,14 +20,14 @@ // 将当前目录设为程序所在目录 static void SetWorkingDir() noexcept { - std::wstring exePath = Win32Utils::GetExePath(); + std::wstring path = Win32Utils::GetExePath(); FAIL_FAST_IF_FAILED(PathCchRemoveFileSpec( - exePath.data(), - exePath.size() + 1 + path.data(), + path.size() + 1 )); - FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(exePath.c_str())); + FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(path.c_str())); } static void IncreaseTimerResolution() noexcept { diff --git a/src/Shared/CommonPch.h b/src/Shared/CommonPch.h index c5d66bf1..b661443c 100644 --- a/src/Shared/CommonPch.h +++ b/src/Shared/CommonPch.h @@ -23,10 +23,12 @@ #include // WIL +// string_maker 需要启用异常 +#define WIL_ENABLE_EXCEPTIONS +#include +#undef WIL_ENABLE_EXCEPTIONS #include -#include // 应在 C++/WinRT 前包含 #include -#include #include // C++/WinRT diff --git a/src/Updater/Updater.vcxproj b/src/Updater/Updater.vcxproj index ce82ec70..829dab64 100644 --- a/src/Updater/Updater.vcxproj +++ b/src/Updater/Updater.vcxproj @@ -33,6 +33,7 @@ Windows + Pathcch.lib;%(AdditionalDependencies) @@ -59,7 +60,17 @@ + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + \ No newline at end of file diff --git a/src/Updater/Updater.vcxproj.filters b/src/Updater/Updater.vcxproj.filters index c21e79e2..47760446 100644 --- a/src/Updater/Updater.vcxproj.filters +++ b/src/Updater/Updater.vcxproj.filters @@ -58,4 +58,10 @@ + + + + + + \ No newline at end of file diff --git a/src/Updater/main.cpp b/src/Updater/main.cpp index f7b4fbf4..c8841bda 100644 --- a/src/Updater/main.cpp +++ b/src/Updater/main.cpp @@ -14,35 +14,32 @@ // along with this program. If not, see . #include "pch.h" +#include #include "Version.h" #include "PackageFiles.h" -#include -#include #include "Utils.h" // 将当前目录设为程序所在目录 -static void SetCurDir() noexcept { - wchar_t curDir[MAX_PATH] = { 0 }; - GetModuleFileName(NULL, curDir, MAX_PATH); +static void SetWorkingDir() noexcept { + std::wstring path; + FAIL_FAST_IF_FAILED(wil::GetModuleFileNameW(NULL, path)); - for (int i = (int)Utils::StrLen(curDir) - 1; i >= 0; --i) { - if (curDir[i] == L'\\' || curDir[i] == L'/') { - break; - } else { - curDir[i] = L'\0'; - } - } + FAIL_FAST_IF_FAILED(PathCchRemoveFileSpec( + path.data(), + path.size() + 1 + )); - SetCurrentDirectory(curDir); + FAIL_FAST_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(path.c_str())); } static bool WaitForMagpieToExit() noexcept { static constexpr const wchar_t* SINGLE_INSTANCE_MUTEX_NAME = L"{4C416227-4A30-4A2F-8F23-8701544DD7D6}"; - HANDLE hSingleInstanceMutex = CreateMutex(nullptr, FALSE, SINGLE_INSTANCE_MUTEX_NAME); - if (hSingleInstanceMutex) { - WaitForSingleObject(hSingleInstanceMutex, 10000); - CloseHandle(hSingleInstanceMutex); + { + wil::unique_mutex_nothrow hSingleInstanceMutex; + if (hSingleInstanceMutex.try_create(SINGLE_INSTANCE_MUTEX_NAME)) { + wil::handle_wait(hSingleInstanceMutex.get(), 10000); + } } // 即使 mutex 已被释放,Magpie.exe 仍有可能正在后台执行清理工作 @@ -61,15 +58,14 @@ static bool WaitForMagpieToExit() noexcept { static void MoveFolder(const std::wstring& src, const std::wstring& dest) noexcept { WIN32_FIND_DATA findData{}; - HANDLE hFind = FindFirstFileEx((std::wstring(src) + L"\\*").c_str(), - FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH); - if (!hFind || hFind == INVALID_HANDLE_VALUE) { + wil::unique_hfind hFind(FindFirstFileEx((std::wstring(src) + L"\\*").c_str(), + FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH)); + if (!hFind) { return; } do { - std::wstring_view fileName(findData.cFileName); - if (fileName == L"." || fileName == L"..") { + if (wil::path_is_dot_or_dotdot(findData.cFileName)) { continue; } @@ -81,9 +77,7 @@ static void MoveFolder(const std::wstring& src, const std::wstring& dest) noexce CreateDirectory(destPath.c_str(), nullptr); MoveFolder(curPath, destPath); } - } while (FindNextFile(hFind, &findData)); - - FindClose(hFind); + } while (FindNextFile(hFind.get(), &findData)); } int APIENTRY wWinMain( @@ -97,7 +91,7 @@ int APIENTRY wWinMain( return 0; } - SetCurDir(); + SetWorkingDir(); Version oldVersion; if (!oldVersion.Parse(Utils::UTF16ToUTF8(lpCmdLine))) { @@ -132,7 +126,7 @@ int APIENTRY wWinMain( MoveFolder(L"update", L"."); // 删除 update 文件夹 - std::filesystem::remove_all(L"update"); + wil::RemoveDirectoryRecursiveNoThrow(L"update"); // 启动 Magpie SHELLEXECUTEINFO execInfo{ diff --git a/src/Updater/packages.config b/src/Updater/packages.config new file mode 100644 index 00000000..09be25d9 --- /dev/null +++ b/src/Updater/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/Updater/pch.h b/src/Updater/pch.h index e2bbe914..56c1c72d 100644 --- a/src/Updater/pch.h +++ b/src/Updater/pch.h @@ -19,3 +19,11 @@ #include #include #include + +// string_maker 需要启用异常 +#define WIL_ENABLE_EXCEPTIONS +#include +#undef WIL_ENABLE_EXCEPTIONS +#include +#include +#include