using System.Runtime.InteropServices; using System.Windows.Forms; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SparkClient.Model.Entity.ApiEntity; namespace SparkClient.Model.Services { public class AlgorithmServer { // 导入 C++ DLL 中的 DetectDiamond 函数 [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr DetectDiamond(string jsonData); // 导入 C++ DLL 中的 FreeMemory 函数 [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern void FreeString(IntPtr ptr); // 导入 C++ DLL 中的 Echo 函数 [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern string Echo(string jsonData); // 导入 C++ DLL 中的 ParseJsonAndReturnActions 函数 [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ParseJsonAndReturnActions(string jsonData); // 添加公共方法 public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_file_base_path, string image_files, string algo_config) { try { // 将所有变量拼接成一个 JSON 对象 JObject jsonData = new JObject( new JProperty("shape", shape), new JProperty("shape_mode", shape_mode), new JProperty("image_file_base_path", image_file_base_path), new JProperty("image_files", JToken.Parse(image_files)), new JProperty("algo_config", JObject.Parse(algo_config)) ); //string jsonDataString = jsonData.ToString(); string jsonDataString = "123"; //byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString); // 调用 C++ DLL 函数解析 JSON IntPtr resultPtr = DetectDiamond(jsonDataString); string resultJson = Marshal.PtrToStringAnsi(resultPtr); // 释放 DLL 分配的内存 FreeString(resultPtr); Console.WriteLine("log999999999999999999"+resultJson); // 检查返回的 JSON 字符串是否为空或无效 if (string.IsNullOrEmpty(resultJson)) { // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 return new AlgorithmResultEntity { facets = new List(), measurements = new Measurements() }; } // 反序列化 JSON 字符串为 AlgorithmResultEntity 对象 var result = JsonConvert.DeserializeObject(resultJson); // 检查反序列化结果是否为 null if (result == null) { // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 return new AlgorithmResultEntity { facets = new List(), measurements = new Measurements() }; } return result; } catch (Exception ex) { // 记录日志或处理异常 Console.WriteLine($"Error in CallParseJsonAndReturnActions: {ex.Message}"); Console.WriteLine($"Stack Trace: {ex.StackTrace}"); // 如果有 InnerException,打印出来 if (ex.InnerException != null) { Console.WriteLine($"Inner Exception: {ex.InnerException.Message}"); Console.WriteLine($"Inner Stack Trace: {ex.InnerException.StackTrace}"); } // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 return new AlgorithmResultEntity { facets = new List(), measurements = new Measurements() }; } } } }