简体   繁体   English

JNI我可以使用哪些类型而不是给定(unsigned int,const char *,const wchar_t *,...)

[英]JNI what types can I use instead of given (unsigned int, const char*, const wchar_t*, … )

Stackoverflow Users ! Stackoverflow用户!

I am trying to covert some functions that I had written in c++ to java. 我试图将我用c ++编写的一些函数转换为java。 Exactly I have a .so library witch I have written in c++ and now I must call functions from my Android application. 我确实有一个用c ++编写的.so库,现在我必须从我的Android应用程序中调用函数。 Functions like this: 这样的功能:

unsignd int Uninitialize();
---------------------------------------
unsignd int DeviceOpen(
    const wchar_t* deviceID,
    unsigned long* phDevice);
---------------------------------------
typedef struct BlobData_s {
     unsigned long     length;
     unsigned char     data[1];
} dsmBlobData_t;

unsignd int Enroll(
    unsigned long       hDevice,
    const char*         userID,
    BlobData_s*         pInputInfo,
    void*               pUserData);

Now how you can see I have functions and typedefs that I want to write in java style, but my problem is that there are no unsigned int java type ( link ). 现在你怎么看我有我想用java风格编写的函数和typedef,但我的问题是没有unsigned int java type( link )。 I knew that instead of int I can use jint , instead of char* I can use jchar* but what I must do with 我知道而不是int我可以使用jint ,而不是char*我可以使用jchar*但我必须做什么

  • const wchar_t*
  • unsigned long
  • unsigned char

What can I use instead of that types. 我可以使用什么而不是那种类型。

I try in this way 我试试这种方式

jint Java_com_example_testApp_TestApp_DeviceOpen( jchar* deviceID, int* phDevice)
{ 
      return DeviceOpen((const wchar_t*)deviceID, (unsigned long*)phDevice);
}

Am I right ?* 我对吗 ?*

Thanks in advance, ViToBrothers. 在此先感谢ViToBrothers。

I don't believe you can use jchar* as it not a valid type for Java. 我不相信你可以使用jchar*因为它不是Java的有效类型。

I suggest you use javah to generate the Java header from a Java class with native methods. 我建议你使用javah从Java类生成带有本机方法的Java头。 This will show you the correct types to use. 这将显示正确的类型。 (I don't know what the equivalent for Android is) (我不知道Android的等价物是什么)

In many cases there is no direct equivalents so you have to used methods to copy the data. 在许多情况下,没有直接的等价物,因此您必须使用方法来复制数据。

BTW: I wouldn't be so worried about unsigned vs signed as the number involved just store an id. 顺便说一句:我不会那么担心unsigned vs签名,因为涉及的号码只是存储一个id。 These are not values you can perform arithmetic operations on. 这些不是您可以执行算术运算的值。

For starters, you'll have to map your types to Java types. 对于初学者,您必须将您的类型映射到Java类型。 As you say, there is no unsigned in Java, and no wchar_t . 正如你所说,Java中没有unsigned ,也没有wchar_t Once you've decided what types to use on the Java side (eg int instead of unsigned , or maybe long ; and probably java.lang.String for both char* and wchar_t* ), you'll have to declare your native methods using them, and implement the JNI in a way which maps them to your internal values. 一旦你决定在Java端使用什么类型(例如int而不是unsigned ,或者long ;并且可能是char*wchar_t* java.lang.String ),你必须使用声明你的本机方法它们,并以一种将它们映射到内部值的方式实现JNI。

Also, if your C++ code uses exceptions, you'll have to catch them and remap them in the JNI interface. 此外,如果您的C ++代码使用异常,则必须捕获它们并在JNI接口中重新映射它们。

For starters, I'd see if some existing tool, like swig, would do the job. 对于初学者,我会看一些现有的工具,比如swig,是否可以完成这项工作。 If it didn't, I'd still use it to get example code, which can then be copy/pasted and edited. 如果没有,我仍然会用它来获取示例代码,然后可以进行复制/粘贴和编辑。

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

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