Java引入C++的库 并使用相应的接口

Java引入C++的库

1、创建类实现Library

package com.zkhx.jna;

import com.sun.jna.Library;
import com.sun.jna.Native;

public interface IData2CCompentLibrary extends Library {

//    IData2CCompentLibrary INSTANCE = Native.loadLibrary("C:\\Users\\Rosa\\Desktop\\CPlusPlusNetworkModule.dll", IData2CCompentLibrary.class);
       IData2CCompentLibrary INSTANCE = Native.loadLibrary("D:\\devlopment_tool\\CPlusPlusNetworkModule.dll", IData2CCompentLibrary.class);

//    String setJStringToJson(String pcServiceIp,int size);
//    const char* setJStringToJson(const char * pcServiceIp,int size);


    /**
     *
     * @param pcdata 网络包数据
     * @param size   数据长度
     * @return json数据
     */
    String CplusplussetJStringToJson(String pcdata, int size);
	
}

使用

protected void channelRead0(ChannelHandlerContext channelHandlerContext, 
DatagramPacket msg) throws Exception {

 //读取网络包数据,转换为16进制字符串
  ByteBuf content2 = msg.content();


  ByteBuf copy = content2.copy();
  int sidInt = copy.readIntLE();//获取信源地址
  int didInt = copy.readIntLE();//获取目的地址
  copy.readIntLE();//信息类别
  String sidHex = Integer.toHexString(sidInt).toUpperCase();
  String didHex = Integer.toHexString(didInt).toUpperCase();

  if (!didHex.equalsIgnoreCase("A001")) return;

  //调用dll库解析网络包数据
  byte[] bytes = new byte[content2.readableBytes()];
  content2.readBytes(bytes);
  String data = bytesToHexString(bytes);
  int length = data.length();

  String resultJson = null;
     switch (sidHex) {
            case "1001": 
  				resultJson = IData2CCompentLibrary.INSTANCE.CplusplussetJStringToJson(data, length);
  		
  		}
}		

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值