Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jstring到L_TCHAR*格式_Java_Java Native Interface - Fatal编程技术网

Java jstring到L_TCHAR*格式

Java jstring到L_TCHAR*格式,java,java-native-interface,Java,Java Native Interface,我一直在尝试调用具有以下签名的C函数 int changeFoo(L_TCHAR* pszFileSrc){....} 在我的JNI调用中,我的方法如下所示: JNIEXPORT jint JNICALL Java_com_me_L_AFoo(JNIEnv * env, jclass jclass, jstring pSrc) { jint retValue = -100; retValue = changeFoo(pSrc); return retValue; }

我一直在尝试调用具有以下签名的C函数

int changeFoo(L_TCHAR* pszFileSrc){....}
在我的JNI调用中,我的方法如下所示:

JNIEXPORT jint JNICALL Java_com_me_L_AFoo(JNIEnv * env, jclass jclass, jstring pSrc)
{
    jint retValue = -100;
    retValue = changeFoo(pSrc);
    return retValue;
}
我在VisualStudio中遇到以下错误

错误1错误C2664:“L_FileConvert”:无法将参数1从“jstring”转换为“L_TCHAR*”c:\Ayusman\Work\MyVCpp\LTExampleDll\LTExampleDll\LTExampleMain.cpp 46 LTExampleDll

当我看到L_TCHAR的定义时*

下面是我在头文件中得到的(按顺序):

我在java上工作,这是我正在尝试构建的一个JNI应用程序。
任何人都可以帮助我如何正确转换此字符串吗?

您必须手动转换字符串。下面是一些(更正的)示例代码:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括“Foo.h”
#定义代理项_掩码0xD800
#定义为代理项(c)((c)和代理项掩码)=代理项掩码)
静态wchar_t计算代码_点(wchar_t代理项_1,wchar_t代理项_2);
JNIEXPORT void JNICALL
Java_Foo_Foo(JNIEnv*env、jobject对象、jstring条){
常量jchar*chars=NULL;
wchar_t*result=NULL;
尺寸透镜;
大小\u t来源\u位置、结果\u位置;
如果(bar==NULL){
返回;
}
len=(*env)->GetStringLength(env,bar);
chars=(*env)->GetStringChars(env,bar,NULL);
if(chars==NULL){
返回;
}
结果=(wchar_t*)malloc(sizeof(wchar_t)*(len+1));
源位置=结果位置=0;
while(源位置释放字符串字符(env、bar、字符);
printf(“%ls\n”,结果);
自由(结果);
}
/**
*基于来自的示例代码http://unicode.org/faq/utf_bom.hmtl
*/
静态wchar\u t计算代码点(wchar\u t high\u代理、wchar\u t low\u代理){

wchar_t x=(high_subrogate&)((1)您的最后一句话提出了一个有趣的问题对于一个wchar___________________________________________________________________在JNI规范中找不到任何方法。Java String类确实有方法codePointCount()和codePointAt(),但用C进行转换似乎更容易。仔细想想,您可能可以使用codePointCount()和codePointAt()将字符串转换为Java中的整数数组方法。一旦有了整数数组,就可以将其传递给本机方法,只需将数组元素复制到字符串中。
#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;
}