Refactoring
This commit is contained in:
parent
9e306e2452
commit
0d228745ac
@ -1,6 +1,7 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
|
#include <filesystem>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <strsafe.h>
|
#include <strsafe.h>
|
||||||
@ -15,6 +16,8 @@
|
|||||||
#pragma comment(lib, "shlwapi")
|
#pragma comment(lib, "shlwapi")
|
||||||
#pragma comment(lib, "Version.lib")
|
#pragma comment(lib, "Version.lib")
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace util
|
namespace util
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -101,47 +104,57 @@ static std::optional<std::string> get_wechat_win_dll_path()
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string dll_path = *wechat_path;
|
fs::path dll_path = *wechat_path;
|
||||||
PathRemoveFileSpecA(dll_path.data());
|
dll_path = dll_path.parent_path();
|
||||||
PathAppendA(dll_path.data(), WECHATWINDLL);
|
|
||||||
|
|
||||||
if (PathFileExistsA(dll_path.c_str())) {
|
fs::path wechat_dll_path = dll_path / WECHATWINDLL;
|
||||||
return dll_path;
|
if (fs::exists(wechat_dll_path)) { // 尝试直接查找 WeChatWin.dll
|
||||||
|
return wechat_dll_path.string();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 微信从(大约)3.7开始,增加了一层版本目录: [3.7.0.29]
|
// 微信从(大约)3.7开始,增加了一层版本目录: [3.7.0.29]
|
||||||
PathRemoveFileSpecA(dll_path.data());
|
std::optional<std::string> found_path;
|
||||||
WIN32_FIND_DATAA find_data;
|
for (const auto &entry : fs::directory_iterator(dll_path)) {
|
||||||
HANDLE hFind = FindFirstFileA((dll_path + "\\*.*").c_str(), &find_data);
|
if (entry.is_directory()) {
|
||||||
if (hFind == INVALID_HANDLE_VALUE) {
|
fs::path possible_dll = entry.path() / WECHATWINDLL;
|
||||||
return std::nullopt;
|
if (fs::exists(possible_dll)) {
|
||||||
|
found_path = possible_dll.string();
|
||||||
|
break; // 取第一个找到的版本号文件夹
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
FindClose(hFind);
|
|
||||||
|
|
||||||
std::string versioned_path = dll_path + "\\" + find_data.cFileName + WECHATWINDLL;
|
if (!found_path) {
|
||||||
return PathFileExistsA(versioned_path.c_str()) ? std::optional<std::string>(versioned_path) : std::nullopt;
|
LOG_ERROR("未找到 WeChatWin.dll");
|
||||||
|
}
|
||||||
|
|
||||||
|
return found_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::optional<std::string> get_file_version(const std::string &file_path)
|
static std::optional<std::string> get_file_version(const std::string &path)
|
||||||
{
|
{
|
||||||
if (!PathFileExistsA(file_path.c_str())) {
|
if (!PathFileExistsA(path.c_str())) {
|
||||||
|
LOG_ERROR("文件不存在: {}", path);
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD dummy = 0;
|
DWORD dummy = 0;
|
||||||
DWORD size = GetFileVersionInfoSizeA(file_path.c_str(), &dummy);
|
DWORD size = GetFileVersionInfoSizeA(path.c_str(), &dummy);
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
|
LOG_ERROR("无法获取文件版本信息大小: {}", path);
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<BYTE> buffer(size);
|
std::vector<BYTE> 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;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
VS_FIXEDFILEINFO *ver_info = nullptr;
|
VS_FIXEDFILEINFO *ver_info = nullptr;
|
||||||
UINT ver_size = 0;
|
UINT ver_size = 0;
|
||||||
if (!VerQueryValueA(buffer.data(), "\\", reinterpret_cast<LPVOID *>(&ver_info), &ver_size)) {
|
if (!VerQueryValueA(buffer.data(), "\\", reinterpret_cast<LPVOID *>(&ver_info), &ver_size)) {
|
||||||
|
LOG_ERROR("无法获取文件版本信息: {}", path);
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,8 +113,10 @@ int WxInitSDK(bool debug, int port)
|
|||||||
pp.port = port;
|
pp.port = port;
|
||||||
snprintf(pp.path, MAX_PATH, "%s", std::filesystem::current_path().string().c_str());
|
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);
|
MessageBoxA(NULL, "初始化失败", "WxInitSDK", 0);
|
||||||
|
WxDestroySDK();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ constexpr std::string_view SUPPORT_VERSION = "3.9.11.25";
|
|||||||
|
|
||||||
UINT64 g_WeChatWinDllAddr = 0;
|
UINT64 g_WeChatWinDllAddr = 0;
|
||||||
|
|
||||||
void InitSpy(LPVOID args)
|
int InitSpy(LPVOID args)
|
||||||
{
|
{
|
||||||
auto *pp = static_cast<util::PortPath *>(args);
|
auto *pp = static_cast<util::PortPath *>(args);
|
||||||
|
|
||||||
@ -28,11 +28,12 @@ void InitSpy(LPVOID args)
|
|||||||
if (version != SUPPORT_VERSION) {
|
if (version != SUPPORT_VERSION) {
|
||||||
LOG_ERROR(msg);
|
LOG_ERROR(msg);
|
||||||
MessageBoxA(NULL, msg.c_str(), "错误", MB_ICONERROR);
|
MessageBoxA(NULL, msg.c_str(), "错误", MB_ICONERROR);
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO(msg);
|
LOG_INFO(msg);
|
||||||
RpcStartServer(pp->port);
|
RpcStartServer(pp->port);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CleanupSpy()
|
void CleanupSpy()
|
||||||
|
@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
#include "framework.h"
|
#include "framework.h"
|
||||||
|
|
||||||
void InitSpy(int port);
|
int InitSpy(int port);
|
||||||
void CleanupSpy();
|
void CleanupSpy();
|
||||||
|
Loading…
Reference in New Issue
Block a user