#include "Utilities.h" #include "Constant.h" #include #include #include #include #include // 静态成员初始化 const std::string Utilities::WeChatDefaultPath = "C:\\Program Files\\Tencent\\WeChat"; const std::string Utilities::MmMojoDll = "mmmojo.dll"; const std::string Utilities::MmMojo64Dll = "mmmojo_64.dll"; std::string Utilities::GetWeChatDir(const std::string& path) { std::string wechatPath = path.empty() ? WeChatDefaultPath : path; // 移除路径末尾的反斜杠 if (!wechatPath.empty() && wechatPath.back() == '\\') wechatPath.pop_back(); // 根据给定路径下 mmmojo.dll 的存在与否,判断是否为微信完整路径 std::string dllPath = wechatPath + "\\" + MmMojoDll; if (std::filesystem::exists(dllPath)) return wechatPath; // 从注册表中获取微信安装版本并根据参数path拼接微信安装目录 HKEY hKey; if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\WeChat", 0, KEY_READ, &hKey) == ERROR_SUCCESS) { char displayVersion[256] = { 0 }; DWORD bufferSize = sizeof(displayVersion); DWORD type = REG_SZ; if (RegQueryValueExA(hKey, "DisplayVersion", NULL, &type, (LPBYTE)displayVersion, &bufferSize) == ERROR_SUCCESS) { std::string versionDir = wechatPath + "\\[" + displayVersion + "]"; RegCloseKey(hKey); if (std::filesystem::exists(versionDir)) return versionDir; } RegCloseKey(hKey); } return ""; } std::string Utilities::GetWeChatOcrExePath() { char appDataPath[MAX_PATH]; if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, appDataPath))) { std::string searchPath = std::string(appDataPath) + "\\Tencent\\WeChat\\XPlugin\\Plugins\\WeChatOCR"; // 使用 Windows API 查找文件 WIN32_FIND_DATAA findData; std::string searchPattern = searchPath + "\\*"; HANDLE hFind = FindFirstFileA(searchPattern.c_str(), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0) { std::string subDir = searchPath + "\\" + findData.cFileName; std::string exePath = subDir + "\\WeChatOCR.exe"; if (std::filesystem::exists(exePath)) { FindClose(hFind); return exePath; } } } } while (FindNextFileA(hFind, &findData)); FindClose(hFind); } } return ""; } void Utilities::WriteBytesToFile(const std::string& filePath, const unsigned char* bytes, size_t length) { std::ofstream fileStream(filePath, std::ios::binary); if (fileStream.is_open()) { fileStream.write(reinterpret_cast(bytes), length); fileStream.close(); } } void Utilities::CopyMmmojoDll(const std::string& wechatFullDir) { char currentDir[MAX_PATH]; GetCurrentDirectoryA(MAX_PATH, currentDir); std::string targetPath = std::string(currentDir) + "\\" + Constant::WeChatOcrData; // 创建目标目录(如果不存在) if (!std::filesystem::exists(targetPath)) std::filesystem::create_directories(targetPath); std::string mmMojoFullPath = wechatFullDir + "\\" + MmMojoDll; std::string mmMojo64FullPath = wechatFullDir + "\\" + MmMojo64Dll; std::string targetMmMojoFullPath = targetPath + "\\" + MmMojoDll; std::string targetMmMojo64FullPath = targetPath + "\\" + MmMojo64Dll; // 复制文件(如果目标文件不存在) if (!std::filesystem::exists(targetMmMojoFullPath) && std::filesystem::exists(mmMojoFullPath)) std::filesystem::copy_file(mmMojoFullPath, targetMmMojoFullPath); if (!std::filesystem::exists(targetMmMojo64FullPath) && std::filesystem::exists(mmMojo64FullPath)) std::filesystem::copy_file(mmMojo64FullPath, targetMmMojo64FullPath); } bool Utilities::RemoveMmmojoDll(std::string& error) { bool ret = true; error = ""; char currentDir[MAX_PATH]; GetCurrentDirectoryA(MAX_PATH, currentDir); std::string targetMmMojoFullPath = std::string(currentDir) + "\\" + MmMojoDll; std::string targetMmMojo64FullPath = std::string(currentDir) + "\\" + MmMojo64Dll; try { if (std::filesystem::exists(targetMmMojoFullPath)) std::filesystem::remove(targetMmMojoFullPath); if (std::filesystem::exists(targetMmMojo64FullPath)) std::filesystem::remove(targetMmMojo64FullPath); } catch (const std::exception& ex) { error = ex.what(); ret = false; } return ret; } // Base64解码函数 std::string Utilities::Base64Decode(const std::string& encoded_string) { if (encoded_string.empty()) return ""; // Base64编码表 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; // 解码过程 size_t in_len = encoded_string.size(); size_t i = 0; size_t j = 0; size_t in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; while (in_len-- && (encoded_string[in_] != '=') && (isalnum(encoded_string[in_]) || (encoded_string[in_] == '+') || (encoded_string[in_] == '/'))) { char_array_4[i++] = encoded_string[in_]; in_++; if (i == 4) { for (i = 0; i < 4; i++) char_array_4[i] = base64_chars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; i < 3; i++) ret += char_array_3[i]; i = 0; } } if (i) { for (j = i; j < 4; j++) char_array_4[j] = 0; for (j = 0; j < 4; j++) char_array_4[j] = base64_chars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; j < i - 1; j++) ret += char_array_3[j]; } return ret; } std::vector Utilities::Base64Decode_Raw(const std::string& input) { const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; std::vector decode_table(256, -1); for (int i = 0; i < 64; i++) decode_table[BASE64_CHARS[i]] = i; // 构建解码表[6,8](@ref) std::vector decoded; int val = 0, bits = -8; // val:24位缓冲区, bits:当前有效位数 for (unsigned char c : input) { if (c == '=') break; // 遇填充符终止 if (decode_table[c] == -1) continue; // 跳过非法字符 val = (val << 6) + decode_table[c]; // 合并6位数据[6](@ref) bits += 6; if (bits >= 0) { // 每凑够8位输出1字节 decoded.push_back((val >> bits) & 0xFF); bits -= 8; } } return decoded; }