using Newtonsoft.Json; using System; using System.Net.Http; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using HandyControl.Controls; using HandyControl.Tools; using Newtonsoft.Json.Linq; using MessageBox = HandyControl.Controls.MessageBox; namespace SparkClient.Model.Services { public class AlgorithmServer { // 使用 P/Invoke 声明 C++ 函数 [DllImport("D:/workspace/dayuAI/SparkClient/Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] private static extern double Add(double a, double b); [DllImport("D:D:/workspace/dayuAI/SparkClient/Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] private static extern double Multiply(double a, double b); // 导入 C++ DLL 中的 ParseJsonAndReturnActions 函数 [DllImport("D:/workspace/dayuAI/SparkClient/Libs/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ParseJsonAndReturnActions(byte[] jsonData); public AlgorithmServer() { // 调用 C++ 函数进行加法运算 double resultAdd = Add(3.5, 4.5); MessageBox.Show($"Add(3.5, 4.5) = {resultAdd}"); // 调用 C++ 函数进行乘法运算 double resultMultiply = Multiply(3.5, 4.5); MessageBox.Show($"Multiply(3.5, 4.5) = {resultMultiply}"); // 测试 JSON 数据 // 定义并初始化 JObject JObject jsonData = new JObject( new JProperty("dataAttributes", new JArray( new JObject(new JProperty("name", "你好")), new JObject(new JProperty("name", "hello")), new JObject(new JProperty("name", "666")), new JObject(new JProperty("name", "是的")), new JObject(new JProperty("name", "确认")) )) ); // 将 JObject 序列化为字符串 string jsonDataString = jsonData.ToString(); // 将 JSON 字符串转换为字节数组 byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString); // 调用 C++ DLL 函数解析 JSON IntPtr resultPtr = ParseJsonAndReturnActions(jsonDataBytes); // 检查返回的指针是否为空 if (resultPtr != IntPtr.Zero) { string? result = Marshal.PtrToStringAnsi(resultPtr); MessageBox.Show(result, "解析结果", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("解析结果为空", "解析结果", MessageBoxButton.OK, MessageBoxImage.Warning); } } } }