diff --git a/WeChatFerry/com/util.cpp b/WeChatFerry/com/util.cpp index ed3473b..bff975a 100644 --- a/WeChatFerry/com/util.cpp +++ b/WeChatFerry/com/util.cpp @@ -279,4 +279,31 @@ std::vector parse_wxids(const std::string &wxids) return wx_members; } +WxString *CreateWxString(const std::string &s) +{ + std::wstring ws = util::s2w(s); + WxString *wxStr = reinterpret_cast(HeapAlloc(GetProcessHeap(), 8, sizeof(WxString))); + if (!wxStr) return nullptr; + size_t len = ws.length(); + wchar_t *ptr = reinterpret_cast(HeapAlloc(GetProcessHeap(), 8, (len + 1) * sizeof(wchar_t))); + if (!ptr) { + HeapFree(GetProcessHeap(), 8, wxStr); + return nullptr; + } + wmemcpy(ptr, ws.c_str(), len + 1); + wxStr->wptr = ptr; + wxStr->size = static_cast(ws.size()); + wxStr->length = static_cast(ws.length()); + wxStr->clen = 0; + wxStr->ptr = nullptr; + return wxStr; +} + +void FreeWxString(WxString *wxStr) +{ + if (wxStr) { + if (wxStr->wptr) HeapFree(GetProcessHeap(), 8, const_cast(wxStr->wptr)); + HeapFree(GetProcessHeap(), 8, wxStr); + } +} } // namespace util diff --git a/WeChatFerry/com/util.h b/WeChatFerry/com/util.h index de61c80..9031c27 100644 --- a/WeChatFerry/com/util.h +++ b/WeChatFerry/com/util.h @@ -65,6 +65,19 @@ inline std::wstring get_pp_len_wstring(uint64_t addr) return (addr && len) ? std::wstring(*reinterpret_cast(addr), len) : L""; } inline std::string get_str_by_wstr_addr(uint64_t addr) { return w2s(get_pp_len_wstring(addr)); } +inline void *AllocFromHeap(size_t size) { return HeapAlloc(GetProcessHeap(), 8, size); } +inline void FreeBuffer(void *buffer) +{ + if (buffer) HeapFree(GetProcessHeap(), 8, buffer); +} + +template static T *AllocBuffer(size_t count) +{ + return reinterpret_cast(HeapAlloc(GetProcessHeap(), 8, sizeof(T) * count)); +} + +WxString *CreateWxString(const std::string &s); +void FreeWxString(WxString *wxStr); std::unique_ptr new_wx_string(const char *str); std::unique_ptr new_wx_string(const wchar_t *wstr);