JNI:从C代码到Java和JNI [英] JNI: From C code to Java and JNI

查看:142
本文介绍了JNI:从C代码到Java和JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse中开发一个应用程序,现在我有一个问题,需要你的帮助。所以我必须从JAVA应用程序调用 C 中的函数。但是,在编写代码的过程中,我有一些 问题 可以在下面看到。我正在等待你的答案和想法...

I am developing an application for android in eclipse and now I have a problem and I need your help. So I must Call function written in C from JAVA application. But on my way of writing code I have some Questions which you can see below. I am waiting for your answers and ideas ...

typdef struct blobData_s {
    unsigned long length;
    unsigned char data[1];
} blobData_t;

unsigned int CheckEnrollmentExist ( unsigned long hdevice, blobData_t* pInputInfo ) {
    // Function code goes here
    ..........................
    return some_value;
}






JAVA代码: h3>

在JAVA代码中代替 unsigned long 我使用 int 我可以写

class jblobData_c {
    public int langth;
    *Question 1.*
}

public class ApplicationMainClass extends Activity {
    // Some code goes here
    ......................

    public native int JCheckEnrollmentExist( int jhdevive, *Question 2.* );

}

问题1。 >


  • 我可以用JAVA代替 unsigned char 代码?

  • 我必须用JAVA代码编写代码,而不是 unsigned char data [1];

  • What I can use instead of unsigned char in JAVA Code ?
  • What I must write in JAVA code instead of unsigned char data[1]; ?

问题2。

Question 2.


  • 如何在JAVA代码中使用 class jblobData_c 而不是 blobData_t * pInputInfo

  • 我必须在JAVA中编写,而不是$ code> blobData_t * pInputInfo ?

  • How I can use class jblobData_c instead of blobData_t* pInputInfo in the JAVA Code ?
  • What I must write in JAVA instead of blobData_t* pInputInfo ?
JNIEXPORT jint JNICALL Java_com_Test_JCheckEnrollmentExist(JNIEnv* env, jobject obj, jint jhdevice, *Question 2.* ) {

    // Call the base function from C code.
    return CheckEnrollmentExist( jhdevice, *Question 3.*);
}

问题3。

Question 3.


  • 我必须在 CheckEnrollmentExist 中写入C代码函数而不是 blobData_t * pInputInfo 为了使此功能正常工作,给定参数相同

  • What I must write in CheckEnrollmentExist function that is C Code Function instead of blobData_t* pInputInfo in order this function works right and given parameter be the same


  1. 传递

  2. 通过JNI将结构对象从C返回到Java
  3. >
  4. 在Java之间传递数据和C

  5. 使用长的将JNI指针传递给Java

  6. 通过JNI传递C和Java之间的指针

  1. How to pass C structs back and forth to Java code in JNI ?
  2. Passing large C structure through JNI efficiently
  3. Return a Structure Object from C to Java Through JNI
  4. Pass data between Java and C
  5. Passing a pointer from JNI to Java using a long
  6. Passing pointers between C and Java through JNI


推荐答案

对于问题#1:

For question #1:

您可以使用jchar。 java中的原始字符串没有被签名,它是关于唯一不是的原语。请注意,jchar是一个UTF-16字符,所以您必须将jchar映射为一个常规的字符,就像您必须使用任何字符转换问题一样。对于简单的转换,这通常可以通过转换来实现。

You can use a jchar. Primitive chars in java are not signed, it's about the only primitive that isn't. Note that jchar is a UTF-16 char, so you will have to "map" the jchar to a regular char, as you would have to with any character conversion issue. For simple conversions, this can typically be done by casting

char c_char = (char)java_char;

因为核心ASCII在ASCII和UTF-16之间共享相同的数值。但是,如果任何人实际上尝试通过界面传递特殊字符,这很容易出错。一个更好的方法是(在java方面,因为它更容易)使用适合您的平台的字符集将字符转换为字节(以确保C层中的平台兼容性)。那么你只需要传递一个字节[]到JNI调用,这些字节将正确对应于C可能会期望的字符。

because the core ASCII shares the same numeric values between ASCII and UTF-16. However, this is prone to error should anyone actually attempt to pass a "special" character through the interface. A much better way would be to (in the java side, as it is easier) convert the characters to bytes using the appropriate character set for your platform (to ensure platform compatibility in the C layers). Then you only need to pass a byte[] to the JNI call, and the bytes will correctly correspond to the characters that C likely will expect.

对于问题#2:

For question #2:

如果您的 CheckEnrollmentExists(...)方法是JNI绑定入口点,不能安全地更改数据类型。这意味着所有输入输入必须是JNI数据类型值。虽然您可能能够选择C数据类型等同的(您可能可以让您的编译器来做),这样的技术应该被皱眉。这隐含意味着JNI入口点不能接受没有在JNI头中定义的结构数据结构。换句话说,您不能将自己的结构传递给该方法。

If your CheckEnrollmentExists(...) method is the JNI binding entry point, you cannot change data types safely. That means that all entry inputs must be JNI data type values. While you might be able to select the C data type equivalents (and you might be able to get your compiler to do it anyway) such techniques should be frowned upon. This implicitly means that JNI entry points cannot accept struct data structure not defined in the JNI headers. In other words, you can't pass your own struct to the method.

如果该方法需要通过调用访问C结构体,请使用其他方法。我看到人们将指针存储在一个成员整数或长的分配的数据结构中(做正确的转换)。然后,您可以重写本机代码端以从传递给该调用的this对象检索指针,并执行取消引用以获取所需的数据。

If the method needs access to a C struct across calls, use another means. I've seen people store the pointer to the allocated data structure in a member integer or long (doing correct casting). You can then rewrite the native code side to retrieve the pointer from the "this" object being passed into the call, and the do a dereference to obtain the required data.

对于问题#3:

For Question #3:

这与问题#2实际上是一样的。在你放置的绑定包装器中,您将在java对象的int或long字段中检索指针的存储值,将其转换为适当的struct指针,然后将其传递给内部方法。由于指针的传递是C到C调用,所以不需要额外的魔法。

This is actually the same as question #2. In the "binding wrapper" you put, you would retrieve the pointer's stored value in the java object's int or long field, cast it to the appropriate struct pointer and then pass it to the internal method. As the passing of the pointer is a C to C call, no extra magic is required.

这篇关于JNI:从C代码到Java和JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆