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

相关推荐
猿小羽1 分钟前
基于 Spring AI 与 Streamable HTTP 构建 MCP Server 实践
java·llm·spring ai·mcp·streamable http
大模型微调Online4 分钟前
深度复盘:Qwen3-4B-Instruct-2507微调实战——打造“快思考、强执行”的 ReAct IoT Agent
java·后端·struts
仰泳的熊猫5 分钟前
题目1433:蓝桥杯2013年第四届真题-危险系数
数据结构·c++·算法·蓝桥杯·深度优先·图论
铁蛋AI编程实战6 分钟前
Agentic AI/GPT-4o替代/Spring AI 2.0/国产大模型轻量化
java·人工智能·spring
weixin_704266057 分钟前
Maven入门:构建与依赖管理全解析
java·maven
cyforkk7 分钟前
14、Java 基础硬核复习:数据结构与集合源码的核心逻辑与面试考点
java·数据结构·面试
零度@11 分钟前
专为 Java 开发者 整理的《Python编程:从入门到实践》前8章核心内容
java·开发语言·windows·python
一嘴一个橘子13 分钟前
idea Could not autowire. No beans of ‘xxxMapper‘ type found
java
海边的Kurisu14 分钟前
苍穹外卖日记 | Day9 用户端历史订单模块、商家端订单管理模块、用户下单功能优化
java·苍穹外卖
Trouvaille ~15 分钟前
【Linux】线程同步与互斥(四):线程池与任务管理
linux·运维·服务器·c++·操作系统·线程池·日志系统