简体   繁体   English

使用java代码中的windows静态库,只需很少的工作量(优先使用JNA)

[英]Use windows static libraries from java code with minimal effort (prefereably using JNA)

I have to use some static windows library ( *.lib ) from java code, I can write C++ JNI wrappers, but I'd rather not do it (not that experienced in C++). 我必须使用java代码中的一些静态Windows库( *.lib ),我可以编写C ++ JNI包装器,但我宁愿不这样做(不是在C ++中有经验)。

What would be the easiest (least developement effort) way use it, performance is not important, since that code will just be fetching some data, that most probrably will be worked on on JAVA side. 什么是最简单(最少开发)的方式使用它,性能并不重要,因为该代码只是获取一些数据,最可能的是将在JAVA方面进行。

Most probably I would use JNA , but AFAIK it needs dynamic libraries, and I have static ones. 最有可能我会使用JNA ,但AFAIK需要动态库,我有静态库。

I also asked questio n on converting static libraries to dynamic ones. 我还询问了关于将静态库转换为动态库的问题。

您始终可以创建一个DLL(动态库)项目,该项目直接调用您的静态库,然后您可以将JNA与新创建的DLL一起使用。

For what it's worth, I had a project like this awhile back. 为了它的价值,我有一个像这样的项目。 It was pretty easy to auto-generate the JNI wrappers. 自动生成JNI包装器非常容易。 I think we had about 350 function exports to wrap. 我认为我们有大约350个函数导出来包装。 It took us about 3 hours to put together a script to auto-generate the wrapper (sorry, don't have the script laying around handy or I'd post it). 我们花了大约3个小时来编写一个脚本来自动生成包装器(抱歉,没有脚本放置得很方便或者我发布它)。

We wrote almost no C++ code ourselves - but it did require understanding how JNI works... That's actually a pretty good learning opportunity/project - if you have the time, don't be afraid of JNI - you'll be amazed at how much you learn about how the JVM works... 我们自己写的几乎没有C ++代码 - 但它确实需要理解JNI如何工作......这实际上是一个非常好的学习机会/项目 - 如果你有时间,不要害怕JNI - 你会惊讶于如何你了解JVM的工作原理......

If you do go this route, I recommend that you keep your wrapper functions really, really lightweight - literally no processing in them at all. 如果你选择这条路线,我建议你保持你的包装功能真正,非常轻巧 - 根本就没有处理它们。 Just transform the necessary arguments from JNI values to native (this is mostly needed for strings), call your native function, and transform the results back. 只需将必要的参数从JNI值转换为native(这通常是字符串所需),调用本机函数,然后将结果转换回来。

If you have a function that passes in a string pointer and expects the string to come back in the pointer, use a string array with size 1 from the Java side and populate it with the result from the native call. 如果你有一个传入字符串指针的函数并希望字符串返回指针,请使用Java端的大小为1的字符串数组,并使用本机调用的结果填充它。

Or if you are pressed for time, compile your .lib to a .dll and use JNA :-) 或者,如果你按时间,将.lib编译为.dll并使用JNA :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM