欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > jna调用c++动态库实体类传参

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

2024/10/27 3:31:04 来源:https://blog.csdn.net/zsj777/article/details/143159604  浏览:    关键词:jna调用c++动态库实体类传参

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

pch.h

#ifndef PCH_H
#define PCH_H// 添加要在此处预编译的标头
#include "framework.h"#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else 
#define EXPORT __declspec(dllexport)
#endiftypedef struct Example3Struct {int val;char* name;
} Example3Struct;EXPORT void example3_sendStruct(const Example3Struct* sval);
#endif //PCH_H

cpp文件  pch.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 completesstd::cout << sval->val << std::endl;std::cout << sval->name << std::endl;}

使用Visual Studio 导出动态库

java 调用  

VideoAiLibrary.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 explicitpublic void example3_sendStruct(Example3Struct.ByReference sval);
}

测试

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头文件  内容如下

#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
#endiftypedef struct Example3Struct {int val;char* name;
} Example3Struct;EXPORT void example3_sendStruct(const Example3Struct* sval);
#endif //PCH_H

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com