以最小的努力使用来自java代码的Windows静态库(最好使用JNA)
我必须使用java代码中的一些静态Windows库(*.lib
),我可以编写C++ JNI包装器,但我宁愿不这样做(在C++方面没有那么丰富的经验)。
使用它的最简单(最少的开发工作)方式是什么,性能并不重要,因为该代码只是获取一些数据,很可能会在 JAVA 端工作。
我很可能会使用 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++).
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.
Most probably I would use JNA, but AFAIK it needs dynamic libraries, and I have static ones.
I also asked question on converting static libraries to dynamic ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以创建一个直接调用静态库的 DLL(动态库)项目,然后您可以将 JNA 与新创建的 DLL 一起使用。
You can always create a DLL (dynamic library) project which calls directly into your static library, and then you can use JNA with the new created DLL.
无论如何,我不久前就有过一个这样的项目。 自动生成 JNI 包装器非常容易。 我认为我们有大约 350 个函数导出需要包装。 我们花了大约 3 个小时来编写一个脚本来自动生成包装器(抱歉,手边没有该脚本,否则我会发布它)。
我们自己几乎没有编写任何 C++ 代码 - 但它确实需要了解 JNI 的工作原理...这实际上是一个非常好的学习机会/项目 - 如果你有时间,不要害怕 JNI - 你会惊讶于它是如何工作的你学到了很多关于 JVM 工作原理的知识...
如果你确实走这条路,我建议你保持你的包装函数非常非常轻量级 - 实际上根本不需要在它们中进行任何处理。 只需将必要的参数从 JNI 值转换为本机值(这主要是字符串所需的),调用本机函数,然后将结果转换回来。
如果您有一个函数传入字符串指针并期望该字符串返回到指针中,请使用 Java 端大小为 1 的字符串数组,并使用本机调用的结果填充它。
或者,如果您时间紧迫,请将 .lib 编译为 .dll 并使用 JNA :-)
For what it's worth, I had a project like this awhile back. It was pretty easy to auto-generate the JNI wrappers. I think we had about 350 function exports to wrap. 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).
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...
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.
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.
Or if you are pressed for time, compile your .lib to a .dll and use JNA :-)