Mã:
#include <jni.h>#include "Hello.h"#include <stdio.h> struct student{ char *name; int age;}student1; student1.name="Jack";student1.age=23; JNIEXPORT jobject JNICALLJava_Hello_sayHello(JNIEnv *env, jobject obj, jintArray arr){ return arr;}
java của mình thế này
Mã:
public class Hello{ static { System.loadLibrary("hello"); } public native Object sayHello(int [] arr); public static void main(String[] args) { Hello p = new Hello(); int arr[] = new int[10]; for (int i = 0; i < 10; i++) { arr[i] = i; } int [] len = (int[]) p.sayHello(arr); System.out.println("sum = " + len.length); //length of the array }}
bây giờ từ C mình muốn return lại số sinh viên đã đăng ký, phỉa làm thế nào vậy ???
sửa tiếp
Mã:
public class Hello{ static { System.loadLibrary("hello"); } public static native Student sayHello(); public static void main(String[] args) { Hello p = new Hello(); Student len = p.sayHello(); System.out.println("Name = " + len.name); System.out.println("Age = " + len.age); } public class Student { private int age; private String name; public Student(int age, String name) { this.age = age; this.name = name; } // the rest of the class } }
Mã:
#include <jni.h>#include "Hello.h"#include <stdio.h> struct student{ char *name; int age; }student1; JNIEXPORT jobject JNICALLJava_Hello_sayHello(JNIEnv *env, jclass cls){ jmethodID constructor; jvalue args[2]; jobject object; cls = (*env)->FindClass(env, "Hello$Student"); constructor = (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String V"); args[0].i = 23; args[1].l = (*env)->NewStringUTF(env, "Jack"); object = (*env)->NewObjectA(env, cls, constructor, args); return object;}
A – Exemple
Programmes Java :
Mã:
public class Appel { private native Objet AppelNative(); public static void main(String[] args) { Objet newObj = null; Appel a = new Appel(); newObj = a.AppelNative(); System.out.println(newObj.getS()); } static { System.loadLibrary("Librairie"); }}public class Objet { private int i; private String s; public Objet(int i, String s) { this.i = i; this.s = s; } public String getS() { return s; }}
Programme C :
Mã:
#include <jni.h>#include "Appel.h"JNIEXPORT jobject JNICALL Java_Appel_AppelNative (JNIEnv *env, jobject obj) { jclass classe = (*env)->FindClass(env, "Objet"); jmethodID mid = (*env)->GetMethodID(env, classe, "<init>", "(ILjava/lang/String;)V"); jint valeur = 10; jstring chaine = (*env)->NewStringUTF(env, "hello"); jobject objet = (*env)->NewObject(env, classe, mid, valeur, chaine); return objet;}
View more random threads:
Pallet nhựa Lâm Đồng giải pháp hoàn hảo cho nhà kho Pallet nhựa Lâm Đồng là một giải pháp đa năng và hiệu quả trong việc vận chuyển và lưu trữ hàng hóa. Trong đó, pallet nhựa Lâm Đồng không chỉ đáp...
Pallet nhựa Lâm Đồng giải pháp...