How to modify a parameter in C++ passed from java (structure Newbie)

Hi,

I'm a brand new to Java (basically a C+ programmer). I have a C++ DLL that takes a structure as parameter from Java and I want to change (or populate) it in C++ DLL.

Here is what I'm trying...

// Java Code...

publicclass CSMSample

{

publicstaticclass MyDetails

{

String myName;

long myAge;

byte[] myBinaryData;

}

publicnativestaticlong SMGetMyDetails(MyDetails myDetails);

publicstaticvoid main(String[] args)

{

MyDetails myDetails =new MyDetails();

SMGetMyDetails(myDetails);

System.out.println("My Name is " + myDetails.myName +" and I am " + myDetails.myAge +" years old");

}

static

{

try

{

System.loadLibrary("SMSample");//SMSample.DLL

}

catch (Exception e)

{

// Handle exception here ...

}

}

}

// C++ code

JNIEXPORT jlong JNICALL Java_CSMSample_SMGetMyDetails(

JNIEnv* env, jclass, jobject jMyDetails)

{

long error = 0;

// I need to modify jMyDetails here and my Java program should be able to read the changes!

return error;

}

Can someone help me please!?

Thanks in advance,

John.

[2537 byte] By [johngummadia] at [2007-11-27 1:23:17]
# 1
This describes how to access the fields of a Java object from C++: http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/functions.html#wp16536Good luck!
kindofbluea at 2007-7-12 0:11:49 > top of Java-index,Java Essentials,Java Programming...
# 2

> This describes how to access the fields of a Java object from C++: http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/functions.html#wp16536

> Good luck!

Thanks for the reply, I tried doing GetFieldID and SetObjectField. It gives NoSuchFieldError exception when I run Java sample. May be I'm not doing it right. Do you have a sample code by any chance?

John.

johngummadia at 2007-7-12 0:11:49 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi

I'm doing the following:

// Java class...

public static class BYTE_ARRAY

{

public longLength;

public byte[]Data;

}

JNIEXPORT jlong JNICALL Java_CSMSample_SMGetCode(

JNIEnv* env, jclass, jobject jByteArrayCode)

{

BYTE_ARRAY Code;

Code.Length = 8;

Code.Data = new BYTE[Code.Length];

Code.Data[0] = 'a'; Code.Data[1] = 'b'; Code.Data[2] = 'c'; Code.Data[3] = 'd';

Code.Data[4] = 'e'; Code.Data[5] = 'f'; Code.Data[6] = 'g'; Code.Data[7] = '\0';

jbyteArray code_data = env->NewByteArray (Code.Length);

env->SetByteArrayRegion(code_data, 0, Code.Length, (const jbyte*) Code.Data);

jclass cls = env->GetObjectClass(jByteArrayCode);

jfieldID fid_Data = env->GetFieldID(cls, "Data", "[B");

jfieldID fid_Length = env->GetFieldID(cls, "Length", "J");

env->SetIntField(jByteArrayCode, fid_Length, Code.Length);

env->SetObjectField(jByteArrayCode, fid_Data, code_data);

}

Java application gets the Length properly but the data part is garbage (When I print, it gives [B@9304b1, same text even if I change the values of Code.Data). Am I missing something?

Thanks,

John.

johngummadia at 2007-7-12 0:11:49 > top of Java-index,Java Essentials,Java Programming...
# 4
Looks good to me (not that I know what I'm talking about ;) Try checking the elements of the array individually on the Java side rather than printing the byte array itself-- I don't think [B@... is the contents
kindofbluea at 2007-7-12 0:11:49 > top of Java-index,Java Essentials,Java Programming...
# 5
Yup, you are right! Writing the elements one by one gives correct values. And one more thing I missed was 'long' (long Length). I thought long in Java and C++ are same, but it looks they are not. I changed it to int in Java.Thanks man!John.
johngummadia at 2007-7-12 0:11:49 > top of Java-index,Java Essentials,Java Programming...