tags:

views:

149

answers:

1

Hi All, I have been trying to call a C function that has the following signature

int changeFoo(L_TCHAR* pszFileSrc){....}

in my JNI call my method looks like this:

JNIEXPORT jint JNICALL Java_com_me_L_AFoo(JNIEnv * env, jclass jclass, jstring pSrc)
{
    jint retValue = -100;
    retValue = changeFoo(pSrc);
    return retValue;
}

I get the following error in visual studio.

Error 1 error C2664: 'L_FileConvert' : cannot convert parameter 1 from 'jstring' to 'L_TCHAR *' c:\Ayusman\Work\MyVCpp\LTExampleDll\LTExampleDll\LTExampleMain.cpp 46 LTExampleDll

When I looked at the definition of L_TCHAR *

here is what I got in the header files (in that sequence):

typedef TCHAR L_TCHAR;
typedef WCHAR TCHAR,*PTCHAR;
typedef wchar_t WCHAR; //wc, 16 bit UNICODE char

I work on java, this is a JNI application that I am trying to build. Can any body help as to how can I convert this properly?

A: 

You'll have to manually convert the string. Here's some (corrected) example code:

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "Foo.h"
#define SURROGATE_MASK 0xD800
#define is_surrogate(c) (((c) & SURROGATE_MASK) == SURROGATE_MASK)

static wchar_t calculate_code_point(wchar_t surrogate_1, wchar_t surrogate_2);

JNIEXPORT void JNICALL
Java_Foo_foo(JNIEnv *env, jobject obj, jstring bar) {
  const jchar *chars = NULL;
  wchar_t *result = NULL;
  size_t len;
  size_t source_pos, result_pos;

  if (bar == NULL) {
    return;
  }

  len = (*env)->GetStringLength(env, bar);
  chars = (*env)->GetStringChars(env, bar, NULL);
  if (chars == NULL) {
    return;
  }

  result = (wchar_t *) malloc(sizeof(wchar_t) * (len + 1));
  source_pos = result_pos = 0;
  while (source_pos < len) {
    wchar_t curr_char = chars[source_pos++];
    if (is_surrogate(curr_char)) {
      wchar_t surrogate_1 = curr_char;
      wchar_t surrogate_2 = chars[source_pos++];
      curr_char = calculate_code_point(surrogate_1, surrogate_2);
    }
    result[result_pos++] = curr_char;
  }
  result[result_pos] = L'\0';

  (*env)->ReleaseStringChars(env, bar, chars);

  printf("%ls\n", result);
  free(result);
}

/**
 * Based on example code from http://unicode.org/faq/utf_bom.hmtl
 */
static wchar_t calculate_code_point(wchar_t high_surrogate, wchar_t low_surrogate) {
  wchar_t x = (high_surrogate & ((1 << 6) - 1)) <<10 | low_surrogate & ((1 << 10) - 1);
  wchar_t w = (high_surrogate >> 6) & ((1 << 5) - 1);
  wchar_t u = w + 1;
  return u << 16 | x;
}

Note that this code only applies if you're using Java 5 or above and your wchar_t data type is four bytes long. If you're using Java 1.4 or below or your wchar_t data type is two bytes long then you don't have to worry about surrogates.

This code also leaves out some basic error checking and assumes that the first surrogate in the pair is the high-order surrogate (which is the case on my machine). You can definitively tell which surrogate is the high-order surrogate and which is the low-order surrogate by their respective values. The high-order surrogate is between 0xD800 and 0xDBFF, inclusive. The low-order surrogate is between 0xDC00 and 0xDFFF, inclusive. If you find a high-order surrogate that isn't paired with a low-order surrogate or a low-order surrogate that isn't paired with a high-order surrogate then the string is coded incorrectly.

Dennis Roberts
Your last sentence raises an interesting issue. To go from a Java String (UTF-16) to an array of wchar_t, shouldn't you also be mapping surrogate pairs to single wchar_t's ??
Stephen C
You're right; thanks for catching that.
Dennis Roberts
The answer's been corrected; thanks again.
Dennis Roberts
Thanks a lot. This helped.Just one question, is it really so hard for making type conversions in JNI? Should it not have some kind of simpler conversion formats?ThanksAyusman
Ayusman
You're welcome. Java might have some built-in conversion functions, but I couldn't find any in the JNI spec. The Java String class does have methods, codePointCount() and codePointAt(), but it seemed easier to just do the conversion in C.
Dennis Roberts
On second thought, you might be able to convert the string to an array of integers in Java using the codePointCount() and codePointAt() methods. Once you have the array of integers, you can pass it to your native method and simply copy the array elements into your string.
Dennis Roberts