简体   繁体   English

使用JNI Java类的DLL

[英]DLL using JNI Java classes

I'm writing a C++ dll (file extension in Visual Studio is .cpp but the code is C) which uses JNI for loading and calling instance methods of some Java classes. 我正在写一个C ++ dll(Visual Studio中的文件扩展名是.cpp,但代码是C),它使用JNI加载和调用某些Java类的实例方法。 As a result my project is made up by C++ source and header files plus some Java class files (the class files for the Java objects used through JNI). 结果,我的项目由C ++源文件和头文件以及一些Java类文件(通过JNI使用的Java对象的类文件)组成。 I would like to create a single dll library including also the Java class files "in a single bundle". 我想创建一个单一的dll库,其中还包括“单个捆绑包”中的Java类文件。 Is it possible to put both the C and Java files into the dll? 是否可以将C和Java文件都放入dll?

This is perfectly possible, though not necessarily intuitive. 这完全有可能,尽管不一定直观。 I haven't tested this but I would imagine it would work easily if you knew what you were doing. 我还没有测试过,但是我想如果您知道自己在做什么,它将很容易工作。

First off, you're going to need a way to package the class file's binary into the native binary you own, and then of course be able to seamlessly read that chunk at any point in time. 首先,您将需要一种将类文件的二进制文件打包到您拥有的本机二进制文件中的方法,然后当然可以在任何时间点无缝读取该块。 How you achieve this is up to you, there is no real 'right way' to do it. 如何实现这取决于您,没有真正的“正确方法”来做到这一点。

That being said, if you can get a handle to the memory where the .class file's data resides (after loading the DLL) and pass it to Java as a byte[] or ByteBuffer , you'd then want to use any one of ClassLoader 's defineClass overloads along with a call to findLoadedClass and then finally loadClass . 话虽如此,如果您可以获取.class文件数据所驻留的内存的句柄(在加载DLL之后),并将其作为byte[]ByteBuffer传递给Java,那么您将想要使用ClassLoader任何一个的defineClass重载以及对findLoadedClass的调用,然后是对loadClass的调用。

This would allow you to load in a class from virtually any data source. 这将使您几乎可以从任何数据源加载类。 As I mentioned before, how you store/retrieve the native data for the class is up to you. 如前所述,如何存储/检索该类的本机数据由您决定。

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

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