diff --git a/WeChatFerry/com/util.cpp b/WeChatFerry/com/util.cpp index 9bc0753..2c45632 100644 --- a/WeChatFerry/com/util.cpp +++ b/WeChatFerry/com/util.cpp @@ -1,6 +1,7 @@ #include "util.h" #include +#include #include #include #include @@ -15,6 +16,8 @@ #pragma comment(lib, "shlwapi") #pragma comment(lib, "Version.lib") +namespace fs = std::filesystem; + namespace util { @@ -101,47 +104,57 @@ static std::optional get_wechat_win_dll_path() return std::nullopt; } - std::string dll_path = *wechat_path; - PathRemoveFileSpecA(dll_path.data()); - PathAppendA(dll_path.data(), WECHATWINDLL); + fs::path dll_path = *wechat_path; + dll_path = dll_path.parent_path(); - if (PathFileExistsA(dll_path.c_str())) { - return dll_path; + fs::path wechat_dll_path = dll_path / WECHATWINDLL; + if (fs::exists(wechat_dll_path)) { // 尝试直接查找 WeChatWin.dll + return wechat_dll_path.string(); } // 微信从(大约)3.7开始,增加了一层版本目录: [3.7.0.29] - PathRemoveFileSpecA(dll_path.data()); - WIN32_FIND_DATAA find_data; - HANDLE hFind = FindFirstFileA((dll_path + "\\*.*").c_str(), &find_data); - if (hFind == INVALID_HANDLE_VALUE) { - return std::nullopt; + std::optional found_path; + for (const auto &entry : fs::directory_iterator(dll_path)) { + if (entry.is_directory()) { + fs::path possible_dll = entry.path() / WECHATWINDLL; + if (fs::exists(possible_dll)) { + found_path = possible_dll.string(); + break; // 取第一个找到的版本号文件夹 + } + } } - FindClose(hFind); - std::string versioned_path = dll_path + "\\" + find_data.cFileName + WECHATWINDLL; - return PathFileExistsA(versioned_path.c_str()) ? std::optional(versioned_path) : std::nullopt; + if (!found_path) { + LOG_ERROR("未找到 WeChatWin.dll"); + } + + return found_path; } -static std::optional get_file_version(const std::string &file_path) +static std::optional get_file_version(const std::string &path) { - if (!PathFileExistsA(file_path.c_str())) { + if (!PathFileExistsA(path.c_str())) { + LOG_ERROR("文件不存在: {}", path); return std::nullopt; } DWORD dummy = 0; - DWORD size = GetFileVersionInfoSizeA(file_path.c_str(), &dummy); + DWORD size = GetFileVersionInfoSizeA(path.c_str(), &dummy); if (size == 0) { + LOG_ERROR("无法获取文件版本信息大小: {}", path); return std::nullopt; } std::vector buffer(size); - if (!GetFileVersionInfoA(file_path.c_str(), 0, size, buffer.data())) { + if (!GetFileVersionInfoA(path.c_str(), 0, size, buffer.data())) { + LOG_ERROR("无法获取文件版本信息: {}", path); return std::nullopt; } VS_FIXEDFILEINFO *ver_info = nullptr; UINT ver_size = 0; if (!VerQueryValueA(buffer.data(), "\\", reinterpret_cast(&ver_info), &ver_size)) { + LOG_ERROR("无法获取文件版本信息: {}", path); return std::nullopt; } diff --git a/WeChatFerry/sdk/sdk.cpp b/WeChatFerry/sdk/sdk.cpp index 6f9b4c3..0635c70 100644 --- a/WeChatFerry/sdk/sdk.cpp +++ b/WeChatFerry/sdk/sdk.cpp @@ -113,8 +113,10 @@ int WxInitSDK(bool debug, int port) pp.port = port; snprintf(pp.path, MAX_PATH, "%s", std::filesystem::current_path().string().c_str()); - if (!call_dll_func_ex(wcProcess, spyDllPath, spyBase, "InitSpy", (LPVOID)&pp, sizeof(util::PortPath), NULL)) { + bool ok = call_dll_func_ex(wcProcess, spyDllPath, spyBase, "InitSpy", (LPVOID)&pp, sizeof(util::PortPath), &status); + if (!ok || status != 0) { MessageBoxA(NULL, "初始化失败", "WxInitSDK", 0); + WxDestroySDK(); return -1; } diff --git a/WeChatFerry/spy/spy.cpp b/WeChatFerry/spy/spy.cpp index 26fc287..785f116 100644 --- a/WeChatFerry/spy/spy.cpp +++ b/WeChatFerry/spy/spy.cpp @@ -11,7 +11,7 @@ constexpr std::string_view SUPPORT_VERSION = "3.9.11.25"; UINT64 g_WeChatWinDllAddr = 0; -void InitSpy(LPVOID args) +int InitSpy(LPVOID args) { auto *pp = static_cast(args); @@ -28,11 +28,12 @@ void InitSpy(LPVOID args) if (version != SUPPORT_VERSION) { LOG_ERROR(msg); MessageBoxA(NULL, msg.c_str(), "错误", MB_ICONERROR); - return; + return -1; } LOG_INFO(msg); RpcStartServer(pp->port); + return 0; } void CleanupSpy() diff --git a/WeChatFerry/spy/spy.h b/WeChatFerry/spy/spy.h index 3aa41dd..07825e6 100644 --- a/WeChatFerry/spy/spy.h +++ b/WeChatFerry/spy/spy.h @@ -2,5 +2,5 @@ #include "framework.h" -void InitSpy(int port); +int InitSpy(int port); void CleanupSpy();