From bf4a7eea7dec740ca3464a116f43731382f6fcda Mon Sep 17 00:00:00 2001 From: huangjiechen Date: Thu, 8 Aug 2024 17:10:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwindows=E4=B8=8B=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=8F=91=E9=80=81=E4=B8=AD=E6=96=87=E5=90=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeChatFerry/spy/rpc_server.cpp | 75 +++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 11 deletions(-) diff --git a/WeChatFerry/spy/rpc_server.cpp b/WeChatFerry/spy/rpc_server.cpp index 679d38f..1a8b842 100644 --- a/WeChatFerry/spy/rpc_server.cpp +++ b/WeChatFerry/spy/rpc_server.cpp @@ -33,6 +33,10 @@ #include "user_info.h" #include "util.h" +#ifdef _WIN32 +#include +#endif + #define URL_SIZE 20 #define BASE_URL "tcp://0.0.0.0" #define G_BUF_SIZE (16 * 1024 * 1024) @@ -252,12 +256,35 @@ bool func_send_img(char *path, char *receiver, uint8_t *out, size_t *len) if ((path == NULL) || (receiver == NULL)) { LOG_ERROR("Empty path or receiver."); rsp.msg.status = -1; - } else if (!fs::exists(path)) { - LOG_ERROR("Path does not exists: {}", path); - rsp.msg.status = -2; } else { - SendImageMessage(receiver, path); - rsp.msg.status = 0; + bool fileExist = false; +#ifdef _WIN32 + // 将 char* 路径转换为 wide-char 格式 + std::wstring wpath = String2Wstring(path); + + // 使用 GetFileAttributesW() 检查文件是否存在 + DWORD attributes = GetFileAttributesW(wpath.c_str()); + if (attributes == INVALID_FILE_ATTRIBUTES) { + LOG_ERROR("Path does not exist: {}", path); + rsp.msg.status = -2; + } else { + fileExist = true; + } +#else + // On Linux and other systems + std::string spath(path); + if (!fs::exists(spath)) { + LOG_ERROR("Path does not exist: {}", path); + rsp.msg.status = -2; + return false; + } else { + fileExist = true; + } +#endif + if(fileExist) { + SendImageMessage(receiver, path); + rsp.msg.status = 0; + } } pb_ostream_t stream = pb_ostream_from_buffer(out, *len); @@ -279,12 +306,38 @@ bool func_send_file(char *path, char *receiver, uint8_t *out, size_t *len) if ((path == NULL) || (receiver == NULL)) { LOG_ERROR("Empty path or receiver."); rsp.msg.status = -1; - } else if (!fs::exists(path)) { - LOG_ERROR("Path does not exists: {}", path); - rsp.msg.status = -2; - } else { - SendImageMessage(receiver, path); - rsp.msg.status = 0; + } + else { + bool fileExist = false; +#ifdef _WIN32 + // 将 char* 路径转换为 wide-char 格式 + std::wstring wpath = String2Wstring(path); + + // 使用 GetFileAttributesW() 检查文件是否存在 + DWORD attributes = GetFileAttributesW(wpath.c_str()); + if (attributes == INVALID_FILE_ATTRIBUTES) { + LOG_ERROR("Path does not exist: {}", path); + rsp.msg.status = -2; + } + else { + fileExist = true; + } +#else + // On Linux and other systems + std::string spath(path); + if (!fs::exists(spath)) { + LOG_ERROR("Path does not exist: {}", path); + rsp.msg.status = -2; + return false; + } + else { + fileExist = true; + } +#endif + if (fileExist) { + SendImageMessage(receiver, path); + rsp.msg.status = 0; + } } pb_ostream_t stream = pb_ostream_from_buffer(out, *len);