You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.8 KiB

using System.Runtime.InteropServices;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
namespace SparkClient.Model.Services
{
public class AlgorithmServer
{
// 使用 P/Invoke 声明 C++ 函数
[DllImport("../../../Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern double Add(double a, double b);
[DllImport("../../../Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern double Multiply(double a, double b);
// 导入 C++ DLL 中的 ParseJsonAndReturnActions 函数
[DllImport("../../../Libs/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ParseJsonAndReturnActions(byte[] jsonData);
// 添加公共方法供外部调用
public string CallParseJsonAndReturnActions(JObject jsonData)
{
string jsonDataString = jsonData.ToString();
byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString);
// 调用 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}");
// 调用 C++ DLL 函数解析 JSON
IntPtr resultPtr = ParseJsonAndReturnActions(jsonDataBytes);
if (resultPtr != IntPtr.Zero)
{
return Marshal.PtrToStringAnsi(resultPtr) ?? "解析结果为空";
}
else
{
return "解析结果为空";
}
}
}
}