springboot調(diào)用C#封裝的DLL文件中函數(shù)的實(shí)現(xiàn)那
1、C#方法
using Infrastructure;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
namespace PH.Resistance;
/// <summary>
/// 預(yù)熱器 阻力計(jì)算
/// </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>
/// 計(jì)算預(yù)熱器阻力值
/// </summary>
/// <returns></returns>
public Response CalcPHResistance()
{
if (!C1_outlet_YP01.HasValue) return Response.Error("C1旋風(fēng)筒(A列)阻力值無效", "");
if (!C1_outlet_YP02.HasValue) return Response.Error("C1旋風(fēng)筒(B列)阻力值無效", "");
if (!C2_outlet_YP01.HasValue) return Response.Error("C2旋風(fēng)筒(A列)阻力值無效", "");
if (!C2_outlet_YP02.HasValue) return Response.Error("C2旋風(fēng)筒(B列)阻力值無效", "");
if (!C3_outlet_YP01.HasValue) return Response.Error("C3旋風(fēng)筒(A列)阻力值無效", "");
if (!C3_outlet_YP02.HasValue) return Response.Error("C3旋風(fēng)筒(B列)阻力值無效", "");
try
{
bool res = true;
StringBuilder strB = new();
//C1旋風(fēng)筒
if (Math.Abs(C1_outlet_YP01.Value) > 6000)
{
strB.Append("C1旋風(fēng)筒(A列)阻力高");
res = false;
}
if (Math.Abs(C1_outlet_YP02.Value) > 6000)
{
strB.Append("C1旋風(fēng)筒(B列)阻力高!");
res = false;
}
//C2旋風(fēng)筒
if (Math.Abs(C2_outlet_YP01.Value) > 5000)
{
strB.Append("C2旋風(fēng)筒(A列)阻力高!");
res = false;
}
if (Math.Abs(C2_outlet_YP02.Value) > 5000)
{
strB.Append("C2旋風(fēng)筒(B列)阻力高!");
res = false;
}
//C3旋風(fēng)筒
if (Math.Abs(C3_outlet_YP01.Value) > 4200)
{
strB.Append("C3旋風(fēng)筒(A列)阻力高!");
res = false;
}
if (Math.Abs(C3_outlet_YP02.Value) > 4200)
{
strB.Append("C3旋風(fēng)筒(B列)阻力高!");
res = false;
}
if (res)
{
return Response.Ok("預(yù)熱器阻力正常", "");
}
else
{
return Response.Error("預(yù)熱器阻力高", 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();
// 將字符串轉(zhuǎn)換為 UTF-8 字節(jié)數(shù)組
byte[] utf8Bytes = Encoding.UTF8.GetBytes(res.ToString());
// 將字節(jié)數(shù)組復(fù)制到非托管內(nèi)存并返回指針
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對(duì)應(yīng)的接口
public interface IIotService extends Library {
IIotService INSTANCE=(IIotService) Native.loadLibrary("PH.Resistance.dll", IIotService.class);
//構(gòu)造函數(shù)
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);
//釋放對(duì)象
void DeletePHResistance(Pointer pointer);
}
5、業(yè)務(wù)調(diào)用
@GetMapping("/testCalcPHResistance")
public String testCalcPHResistance(){
//初始化構(gòu)造函數(shù)
Pointer constructPointer=IIotService.INSTANCE.CreatePHResistance(10.0,2.0,3.0,4.0,5.0,6.0);
//執(zhí)行邏輯函數(shù)
Pointer pointerResult=IIotService.INSTANCE.CalcPHResistance(constructPointer);
//獲取結(jié)果
String result=pointerResult.getString(0);
//釋放構(gòu)造函數(shù)的對(duì)象
IIotService.INSTANCE.DeletePHResistance(constructPointer);
return result;
}
到此這篇關(guān)于springboot調(diào)用C#封裝的DLL文件中函數(shù)的實(shí)現(xiàn)那的文章就介紹到這了,更多相關(guān)springboot調(diào)用C# DLL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中float類型的范圍及其與十六進(jìn)制的轉(zhuǎn)換例子
這篇文章主要介紹了Java中float類型的范圍及其與十六進(jìn)制的轉(zhuǎn)換例子,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
SpringBoot+RabbitMq具體使用的幾種姿勢(shì)
這篇文章主要介紹了SpringBoot+RabbitMq具體使用的幾種姿勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Java類加載機(jī)制實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Java類加載機(jī)制實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot yaml語法與JRS303校驗(yàn)超詳細(xì)講解
YAML 是 “YAML Ain’t Markup Language”(YAML 不是一種標(biāo)記語言)的遞歸縮寫。在開發(fā)的這種語言時(shí),YAML 的意思其實(shí)是:“Yet Another Markup Language”(仍是一種標(biāo)記語言),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別
這篇文章主要介紹了Spring中@Service注解的作用與@Controller和@RestController之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03

