jna调用c++动态库实体类传参

1、win10平台代码 c++ 头文件

pch.h

cpp 复制代码
#ifndef PCH_H
#define PCH_H

// 添加要在此处预编译的标头
#include "framework.h"

#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else 
#define EXPORT __declspec(dllexport)
#endif


typedef struct Example3Struct {
	int val;
	char* name;
} Example3Struct;

EXPORT void example3_sendStruct(const Example3Struct* sval);
#endif //PCH_H

cpp文件 pch.cpp

cpp 复制代码
// pch.cpp: 与预编译标头对应的源文件

#include "pch.h"
#include "iostream"



	void example3_sendStruct(const Example3Struct* sval)
	{
		// note: printfs called from C won't be flushed
		// to stdout until the Java process completes
		std::cout << sval->val << std::endl;
		std::cout << sval->name << std::endl;
	}

使用Visual Studio 导出动态库

java 调用

复制代码
VideoAiLibrary.java
java 复制代码
package com.example.demo.jna;

import com.sun.jna.Library;
import com.sun.jna.Structure;


public interface VideoAiLibrary extends Library {

    @Structure.FieldOrder({"val","name"})
    public static class Example3Struct extends Structure {
        public static class ByReference extends Example3Struct implements Structure.ByReference {}

        public int val;
        public String name;
    }

    // unless otherwise specified, ByReference is assumed - but it can't hurt to be explicit
    public void example3_sendStruct(Example3Struct.ByReference sval);
}

测试

cpp 复制代码
package com.example.demo.jna;

import com.sun.jna.Native;

public class JnaTest {

    public static  void main(String ... args){
        final VideoAiLibrary clib = (VideoAiLibrary) Native.load("mycpp11", VideoAiLibrary.class);

        final VideoAiLibrary.Example3Struct.ByReference e3ref = new VideoAiLibrary.Example3Struct.ByReference();
        e3ref.val = 700;
        e3ref.name="朱dddp";
        clib.example3_sendStruct(e3ref);
    }

}

linux 代码唯一区别是 pch.h头文件 内容如下

cpp 复制代码
#ifndef PCH_H
#define PCH_H


#ifdef __cplusplus
#ifdef _WIN32
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT extern "C"
#endif
#else
#ifdef _WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
#endif

typedef struct Example3Struct {
	int val;
	char* name;
} Example3Struct;

EXPORT void example3_sendStruct(const Example3Struct* sval);
#endif //PCH_H
bash 复制代码
cd /aiprogram/cpp/JNA_TEST/

#编译c++ 动态库
 bash runcpp.sh

#运行java代码示例
 bash runjava.sh

linux 编译和java 代码如下

src 下c++代码,include 下c++的.h头文件,java_code 下java代码

通过百度网盘分享的文件:JNA_TEST.zip

链接:https://pan.baidu.com/s/1uA4r8gJM5-vut73OoAQZMg?pwd=4ob0

提取码:4ob0

相关推荐
武子康35 分钟前
Java-71 深入浅出 RPC Dubbo 上手 父工程配置编写 附详细POM与代码
java·分布式·程序人生·spring·微服务·rpc·dubbo
武子康3 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途3 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
还债大湿兄3 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
椰椰椰耶4 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员5 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
工业甲酰苯胺5 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
没有羊的王K6 小时前
SSM框架学习——day1
java·学习
珊瑚里的鱼7 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
又菜又爱coding7 小时前
安装Keycloak并启动服务(macOS)
java·keycloak