当前位置: 首页 > article >正文

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;
    }

http://www.kler.cn/a/419090.html

相关文章:

  • 多线程篇-8--线程安全(死锁,常用保障安全的方法,安全容器,原子类,Fork/Join框架等)
  • linux内核面试题精选及参考答案
  • 【力扣】541.反转字符串2
  • 基于树莓派的安保巡逻机器人--项目介绍
  • 【AI系统】昇腾 AI 架构介绍
  • Vue项目Docker部署
  • 力扣3372.连接两棵树后最大目标节点数目I
  • 内网使用docker搭建librespeed测速网站
  • 挑战用React封装100个组件【004】
  • UaGateway:实现OPC DA和OPC UA的高效转换
  • FFmpeg一些常用的命令
  • ElasticSearch的学习
  • JAVA中HashMap、TreeMap、LinkedHashMap 的用法与注意事项
  • 简单搭建qiankun的主应用和子应用并且用Docker进行服务器部署
  • AI高中数学教学视频生成技术:利用通义千问、MathGPT、视频多模态大模型,语音大模型,将4个模型融合 ,生成高中数学教学视频,并给出实施方案。
  • MySQL索引与分区:性能优化的关键
  • openbmc dbus架构简析(二)
  • DDR3与MIG IP核详解(一)
  • ESP32-S3模组上跑通ES8388(12)
  • SpringBoot集成swagger3
  • 【Docker】部署nginx
  • 常用元器件使用方法36:USB转串口芯片CH340X
  • 【07】MySQL中的DQL(数据查询语言)详解
  • 【JavaWeb maven基础知识总结】
  • RabbitMQ rabbitmq.conf配置文件详解
  • 算法训练营day22(二叉树08:二叉搜索树的最近公共祖先,插入,删除)