Viktor Apoyan
Viktor Apoyan

Reputation: 10755

JNI what types can I use instead of given (unsigned int, const char*, const wchar_t*, .... )

Stackoverflow Users !

I am trying to covert some functions that I had written in c++ to java. Exactly I have a .so library witch I have written in c++ and now I must call functions from my Android application. 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). I knew that instead of int I can use jint, instead of char* I can use jchar* but what I must do with

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.

Upvotes: 0

Views: 2072

Answers (2)

James Kanze
James Kanze

Reputation: 153967

For starters, you'll have to map your types to Java types. As you say, there is no unsigned in Java, and no wchar_t. Once you've decided what types to use on the Java side (e.g. 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.

Also, if your C++ code uses exceptions, you'll have to catch them and remap them in the JNI interface.

For starters, I'd see if some existing tool, like swig, would do the job. If it didn't, I'd still use it to get example code, which can then be copy/pasted and edited.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533640

I don't believe you can use jchar* as it not a valid type for Java.

I suggest you use javah to generate the Java header from a Java class with native methods. This will show you the correct types to use. (I don't know what the equivalent for Android is)

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. These are not values you can perform arithmetic operations on.

Upvotes: 1

Related Questions