最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

JNI和Qt通信(Part2)

来源:懂视网 责编:小采 时间:2020-11-09 08:33:16
文档

JNI和Qt通信(Part2)

JNI和Qt通信(Part2):Part2 JNI数据转换成C数据 e.g. jstring -GetStringUTFChars(),NewStringUTF(),ReleaseStringUTFChars() 1 2 3 4 5 JNIEXPORT void JNICALL Java_JNISample_sampleFunction(JNIEnv* env, jobject o
推荐度:
导读JNI和Qt通信(Part2):Part2 JNI数据转换成C数据 e.g. jstring -GetStringUTFChars(),NewStringUTF(),ReleaseStringUTFChars() 1 2 3 4 5 JNIEXPORT void JNICALL Java_JNISample_sampleFunction(JNIEnv* env, jobject o

Part2 JNI数据转换成C数据 e.g. jstring -GetStringUTFChars(),NewStringUTF(),ReleaseStringUTFChars() 1 2 3 4 5 JNIEXPORT void JNICALL Java_JNISample_sampleFunction(JNIEnv* env, jobject obj, jstring name) { const char * pname = env-GetStringUT

Part2

JNI数据转换成C数据

e.g. jstring - GetStringUTFChars(), NewStringUTF(), ReleaseStringUTFChars()

1

2

3

4

5

JNIEXPORT void JNICALL Java_JNISample_sampleFunction(JNIEnv* env, jobject obj, jstring name)

{

const char* pname = env->GetStringUTFChars(name, NULL);

env->ReleaseStringUTFChars(name, pname);

}

e.g. Array

1

2

3

4

5

6

7

8

9

10

11

12

JNIEXPORT jint JNICALL Java_IntArray_sumArray

(JNIEnv *env, jobject obj, jintArray arr) {

jint buf[10];

jint i, sum = 0;

// This line is necessary, since Java arrays are not guaranteed

// to have a continuous memory layout like C arrays.

env->GetIntArrayRegion(arr, 0, 10, buf);

for (i = 0; i < 10; i++) {

sum += buf[i];

}

return sum;

}

http://ironurbane.iteye.com/blog/425513

JNI的数据定义

1

2

3

4

5

6

7

8

9

10

11

12

// In "win\jni_mh.h" - machine header which is machine dependent

typedef long jint;

typedef __int64 jlong;

typedef signed char jbyte;

// In "jni.h"

typedef unsigned char jboolean;

typedef unsigned short jchar;

typedef short jshort;

typedef float jfloat;

typedef double jdouble;

typedef jint jsize;


C++ 调用Java方法

Read: http://stackoverflow.com/questions/819536/how-to-call-java-function-from-c

Windows http://public0821.iteye.com/blog/423941

Linux http://blog.sina.com.cn/s/blog_48eef8410100fjxr.html


JNI数据类型

Java Type Native Type Description
boolean jboolean 8 bits, unsigned
byte jbyte 8 bits, signed
char jchar 16 bits, unsigned
double jdouble 64 bits
float jfloat 32 bits
int jint 32 bits, signed
long jlong 64 bits, signed
short jshort 16 bits, signed
void void N/A

JNI的类型签名

Java Type Signature
boolean Z
byte B
char C
double D
float F
int I
long J
void V
object Lfully-qualified-class;
type[] [type
method signature ( arg-types) ret-type

e.g.

Java side

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

class JNISample

{

public native void launchSample();

static

{

System.loadLibrary("Sample");

}

public static int add(int a,int b) {

return a+b;

}

public boolean judge(boolean bool) {

return !bool;

}

}

C++side

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

JNIEnv *env = GetJNIEnv(); //Get env from JNI

jclass cls;

cls = env->FindClass("JNISample");

if(cls !=0)

{

printf("find java class success\n");

// constructor

mid = env->GetMethodID(cls,"","()V");

if(mid !=0)

{

jobj=env->NewObject(cls,mid);

}

// static function

mid = env->GetStaticMethodID( cls, "add", "(II)I");

if(mid !=0)

{

square = env->CallStaticIntMethod( cls, mid, 5,5);

}

// function returns boolean

mid = env->GetMethodID( cls, "judge","(Z)Z");

if(mid !=0){

jnot = env->CallBooleanMethod(jobj, mid, 1);

}

}


查看属性和方法的签名

Java版本 "java -version"

反编译工具 javap:

1

javap -s -p -classpath R:\test.Demo

Check JNI version

1

2

3

#ifdef JNI_VERSION_1_4

printf("Version is 1.4 \n");

#endif

使用API

1

jint GetVersion(JNIEnv *env);

返回值需要转换, Need convert the result from DEC to HEX;


JNI实现过程中的Issue

x86 or x64 "Can't load load IA 32-bit dll on a amd 64 bit platform"

确定本机上的默认JVM的版本和动态库的版本一致(x86或x64), Make sure JAVA's default path; check with "java -version" in command line.

3rdParty can't find dependent libraries 保证所依赖的动态库都能被找到;

1) copy the dll into executable file's folder 2) System.load() the dlls by dependecy orders

JNI_CreateJavaVM failed

C++创建JVM调用Java方法

http://docs.oracle.com/javase/1.4.2/docs/guide/jni/jni-12.html#JNI_CreateJavaVM & http://blog.csdn.net/louka/article/details/7318656

[我机器上装了多个版本的Java, 测试的时候没有成功]

jvm.dll(C:\Program Files (x86)\Java\jdk1.7.0_17\jre\bin\client; C:\Program Files (x86)\Java\jdk1.7.0_17\jre\bin\server; need check); jvm.lib(C:\Program Files (x86)\Java\jdk1.7.0_17\lib)


http://home.pacifier.com/~mmead/jni/cs510ajp/ & http://www.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

Sample http://chnic.iteye.com/category/20179

JNI doc http://docs.oracle.com/javase/7/docs/technotes/guides/jni/

>JNA https://github.com/twall/jna/ XstartOnFirstThread

---End---

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

JNI和Qt通信(Part2)

JNI和Qt通信(Part2):Part2 JNI数据转换成C数据 e.g. jstring -GetStringUTFChars(),NewStringUTF(),ReleaseStringUTFChars() 1 2 3 4 5 JNIEXPORT void JNICALL Java_JNISample_sampleFunction(JNIEnv* env, jobject o
推荐度:
标签: 数据 换成 通信
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top