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出口压力A
private double? C1_outlet_YP02; //C2出口压力B
private double? C2_outlet_YP01; //C2出口压力A
private double? C2_outlet_YP02; //C2出口压力B
private double? C3_outlet_YP01; //C3出口压力A
private 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;
}