From f54b69e887b8f5d006b014380556330d44efaa4b Mon Sep 17 00:00:00 2001 From: MistEO Date: Thu, 6 Jan 2022 18:08:55 +0800 Subject: [PATCH] =?UTF-8?q?interface.=E4=BF=AE=E6=94=B9=E5=9B=9E=E8=B0=83j?= =?UTF-8?q?son=E4=B8=BAutf-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/AsstCaller.cpp | 3 +-- src/MeoAsstGui/Helper/AsstProxy.cs | 22 +++++++++++++++++++++- src/Python/sample.py | 12 ++---------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index 7dff33cf0d..97a21e1e75 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -7,7 +7,6 @@ #include "Assistant.h" #include "AsstDef.h" -#include "AsstUtils.hpp" #include "Version.h" #if 0 @@ -38,7 +37,7 @@ AsstCallback _callback = nullptr; void CallbackTrans(asst::AsstMsg msg, const json::value& json, void* custom_arg) { - _callback(static_cast(msg), asst::utils::utf8_to_gbk(json.to_string()).c_str(), custom_arg); + _callback(static_cast(msg), json.to_string().c_str(), custom_arg); } asst::Assistant* AsstCreate(const char* dirname) diff --git a/src/MeoAsstGui/Helper/AsstProxy.cs b/src/MeoAsstGui/Helper/AsstProxy.cs index 880eae5a7e..1dba26afe9 100644 --- a/src/MeoAsstGui/Helper/AsstProxy.cs +++ b/src/MeoAsstGui/Helper/AsstProxy.cs @@ -80,9 +80,29 @@ namespace MeoAsstGui tvm.Idle = true; } + + [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)] + internal static extern int lstrlenA(IntPtr ptr); + + private static string PtrToStringCustom(IntPtr ptr, System.Text.Encoding enc) + { + if (IntPtr.Zero == ptr) + { + return null; + } + if (lstrlenA(ptr) == 0) + { + return string.Empty; + } + int len = lstrlenA(ptr); + Byte[] bytes = new Byte[len + 1]; + Marshal.Copy(ptr, bytes, 0, len + 1); + return enc.GetString(bytes); + } + private void CallbackFunction(int msg, IntPtr json_buffer, IntPtr custom_arg) { - string json_str = Marshal.PtrToStringAnsi(json_buffer); + string json_str = PtrToStringCustom(json_buffer, System.Text.Encoding.UTF8); //Console.WriteLine(json_str); JObject json = (JObject)JsonConvert.DeserializeObject(json_str); ProcCallbckMsg dlg = new ProcCallbckMsg(proc_msg); diff --git a/src/Python/sample.py b/src/Python/sample.py index ab67d56970..7555af268a 100644 --- a/src/Python/sample.py +++ b/src/Python/sample.py @@ -1,6 +1,5 @@ import json import pathlib -import platform from interface import Asst, Message @@ -9,18 +8,11 @@ if __name__ == "__main__": @Asst.CallBackType def my_callback(msg, details, arg): m = Message(msg) - if platform.system().lower() == 'windows': - d = json.loads(details.decode('gbk')) - else: - d = json.loads(details.decode('utf-8')) + d = json.loads(details.decode('utf-8')) print(m, d, arg) - if platform.system().lower() == 'windows': - dirname: str = (pathlib.Path.cwd().parent.parent / 'x64' / 'Release').__str__() - else: - dirname: str = (pathlib.Path.cwd().parent.parent / 'build').__str__() - + dirname: str = (pathlib.Path.cwd()).__str__() asst = Asst(dirname=dirname, callback=my_callback) print('version', asst.get_version())