Populating C Strucutre.

Hi,I would like to populate data from Java to C Structure. The structure has about 8 to 10 elements. What is the best way of achiv. this. Can I send a collection to the Native method & populate the C struct.
[225 byte] By [Shashank.Tilwallia] at [2007-11-27 0:41:54]
# 1
See http://www.sharewareplaza.com/Java-Platform-Invoke-API-Demo-version-downloads_49212.html
vitallisa at 2007-7-11 22:55:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Yes, you can populate a Java Structure into C Structure. A java structure or a collection is interpreted as any java object on native side. Here is a small example of sending a java object (TextSegment.java) to C++ and printin it.

TextSegment.java

package com.text;

public class TextSegment {

public String text;

public int x;

public int y;

public TextSegment() {

}

public TextSegment(String text, int start, int end) {

this.text = text;

this.x = start;

this.y = end;

}

}

Main.java

package com.text;

public class Main {

public static void main(String[] args) throws InterruptedException {

System.loadLibrary("processEvents.dll");

System.out.println("Loading dll");

TextSegment ts = new TextSegment("testString",10,20);

Main.populateObject(ts);

}

private static native void populateObject(TextSegment ts);

}

Native method:

/*

* Class:com.test.Main

* Method:populateObject

* Signature: (Lcom//test/TextSegment;)V

*/

JNIEXPORT void JNICALL Java_com_test_Main_populateObject

(JNIEnv * env, jclass jcls, jobject obj){

//#define JNISTRING "Ljava/lang/String;"

jboolean bt = true;

jclasscls = env->GetObjectClass(obj);

jfieldID fidText = env->GetFieldID(cls, "text", "Ljava/lang/String;");

jstring txt = (jstring) env->GetObjectField(obj,fidText);

const char* tx = new char[256];

tx = env->GetStringUTFChars(txt, &bt);

jfieldID fidx = env->GetFieldID(cls, "x", "I");

int x = env->GetIntField(obj,fidx);

jfieldID fidy = env->GetFieldID(cls, "y", "I");

int y = env->GetIntField(obj,fidy);

printf("text=%s,x=%d, y=%d\n", tx,x, y);

}

kteegalaa at 2007-7-11 22:55:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Hey, Can we do the same without using JNI. What are the alternatives?
Shashank.Tilwallia at 2007-7-11 22:55:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Hi,You can try a generic wrapper like JNative, to do this.--Marc ( http://jnative.sf.net)
mdentya at 2007-7-11 22:55:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...