欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > spring boot 调用C#封装的DLL文件中的函数

spring boot 调用C#封装的DLL文件中的函数

2024/11/29 21:38:05 来源:https://blog.csdn.net/weixin_41463944/article/details/144106461  浏览:    关键词:spring boot 调用C#封装的DLL文件中的函数

1、C#方法

using Infrastructure;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;namespace PH.Resistance;/// <summary>
/// 预热器 阻力计算
/// </summary>
public class PHResistance
{private double? C1_outlet_YP01; //C1出口压力Aprivate double? C1_outlet_YP02; //C2出口压力Bprivate double? C2_outlet_YP01; //C2出口压力Aprivate double? C2_outlet_YP02; //C2出口压力Bprivate double? C3_outlet_YP01; //C3出口压力Aprivate double? C3_outlet_YP02; //C3出口压力B//private decimal C4_outlet_YP01; //C4出口压力A//private decimal C4_outlet_YP02; //C4出口压力B//private decimal C5_outlet_YP01; //C5出口压力A//private decimal C5_outlet_YP02; //C5出口压力B/// <summary>////// </summary>/// <param name="c1_outlet_yp01"></param>/// <param name="c1_outlet_yp02"></param>/// <param name="c2_outlet_yp01"></param>/// <param name="c2_outlet_yp02"></param>/// <param name="c3_outlet_yp01"></param>/// <param name="c3_outlet_yp02"></param>public PHResistance(double? c1_outlet_yp01 = null, double? c1_outlet_yp02 = null,double? c2_outlet_yp01 = null, double? c2_outlet_yp02 = null,double? c3_outlet_yp01 = null, double? c3_outlet_yp02 = null){C1_outlet_YP01 = c1_outlet_yp01;C1_outlet_YP02 = c1_outlet_yp02;C2_outlet_YP01 = c2_outlet_yp01;C2_outlet_YP02 = c2_outlet_yp02;C3_outlet_YP01 = c3_outlet_yp01;C3_outlet_YP02 = c3_outlet_yp02;}/// <summary>/// 计算预热器阻力值/// </summary>/// <returns></returns>public Response CalcPHResistance(){if (!C1_outlet_YP01.HasValue) return Response.Error("C1旋风筒(A列)阻力值无效", "");if (!C1_outlet_YP02.HasValue) return Response.Error("C1旋风筒(B列)阻力值无效", "");if (!C2_outlet_YP01.HasValue) return Response.Error("C2旋风筒(A列)阻力值无效", "");if (!C2_outlet_YP02.HasValue) return Response.Error("C2旋风筒(B列)阻力值无效", "");if (!C3_outlet_YP01.HasValue) return Response.Error("C3旋风筒(A列)阻力值无效", "");if (!C3_outlet_YP02.HasValue) return Response.Error("C3旋风筒(B列)阻力值无效", "");try{bool res = true;StringBuilder strB = new();//C1旋风筒if (Math.Abs(C1_outlet_YP01.Value) > 6000){strB.Append("C1旋风筒(A列)阻力高");res = false;}if (Math.Abs(C1_outlet_YP02.Value) > 6000){strB.Append("C1旋风筒(B列)阻力高!");res = false;}//C2旋风筒if (Math.Abs(C2_outlet_YP01.Value) > 5000){strB.Append("C2旋风筒(A列)阻力高!");res = false;}if (Math.Abs(C2_outlet_YP02.Value) > 5000){strB.Append("C2旋风筒(B列)阻力高!");res = false;}//C3旋风筒if (Math.Abs(C3_outlet_YP01.Value) > 4200){strB.Append("C3旋风筒(A列)阻力高!");res = false;}if (Math.Abs(C3_outlet_YP02.Value) > 4200){strB.Append("C3旋风筒(B列)阻力高!");res = false;}if (res){return Response.Ok("预热器阻力正常", "");}else{return Response.Error("预热器阻力高", strB.ToString());}}catch (Exception ex){throw;}}/// <summary>////// </summary>public static class NativeExports{[UnmanagedCallersOnly(EntryPoint = "CreatePHResistance")]public static IntPtr CreatePHResistance(double a, double b, double c,double d, double e, double f){var instance = new PHResistance(a, b, c, d, e, f);GCHandle handle = GCHandle.Alloc(instance);return (IntPtr)handle;}[UnmanagedCallersOnly(EntryPoint = "CalcPHResistance")]public static IntPtr CalcPHResistance(IntPtr instancePtr){var handle = (GCHandle)instancePtr;var instance = (PHResistance)handle.Target!;var res = instance?.CalcPHResistance();// 将字符串转换为 UTF-8 字节数组byte[] utf8Bytes = Encoding.UTF8.GetBytes(res.ToString());// 将字节数组复制到非托管内存并返回指针IntPtr unmanagedPointer = Marshal.AllocHGlobal(utf8Bytes.Length);Marshal.Copy(utf8Bytes, 0, unmanagedPointer, utf8Bytes.Length);return unmanagedPointer;}[UnmanagedCallersOnly(EntryPoint = "DeletePHResistance")]public static void DeletePHResistance(IntPtr instancePtr){var handle = (GCHandle)instancePtr;handle.Free();}}
}

2、spring boot maven依赖

<dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>4.1.0</version></dependency>

3、将DLL文件放入到resources文件夹下面
在这里插入图片描述
4、定义与DLL对应的接口

public interface IIotService extends Library {IIotService INSTANCE=(IIotService) Native.loadLibrary("PH.Resistance.dll", IIotService.class);//构造函数Pointer CreatePHResistance(double c1_outlet_yp01, double c1_outlet_yp02,double c2_outlet_yp01, double c2_outlet_yp02,double c3_outlet_yp01, double c3_outlet_yp02);//具体方法Pointer CalcPHResistance(Pointer pointer);//释放对象void DeletePHResistance(Pointer pointer);
}

5、业务调用

@GetMapping("/testCalcPHResistance")public String testCalcPHResistance(){//初始化构造函数Pointer constructPointer=IIotService.INSTANCE.CreatePHResistance(10.0,2.0,3.0,4.0,5.0,6.0);//执行逻辑函数Pointer pointerResult=IIotService.INSTANCE.CalcPHResistance(constructPointer);//获取结果String result=pointerResult.getString(0);//释放构造函数的对象IIotService.INSTANCE.DeletePHResistance(constructPointer);return result;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com