How to set values of members of a class (if the member is another class)?

Hi,

I'm a C++ programmer but new to Java. I have a structure (or class in Java) that has two members, one int and another GUID (which is another class). I'm trying to access that from C++ DLL and I can access the int member but when I try to access members of GUID, it fails. Can someone guide me if I'm doing it correctly?

I appreciate your help. Here is the code...

//Java code

publicclass CSMSample

{

publicstaticclass GUID

{

publicintData1;

publicshortData2;

publicshortData3;

publicbyte[]Data4;//unsigned char Data4[8]

}

publicstaticclass SYSTEMTIME

{

publicshort wYear;

publicshort wMonth;

publicshort wDayOfWeek;

publicshort wDay;

publicshort wHour;

publicshort wMinute;

publicshort wSecond;

publicshort wMilliseconds;

}

publicstaticclass Result

{

publicint ReturnVal;

public GUID ComponentGUID;

}

publicnativestaticint SMProcess(Result result);

publicstaticvoid main(String[] args)

{

Result result =new Result();

SMProcess(result);

//.....

}

static

{

try

{

// load library...

}

catch (Exception e)

{

//....

}

}

}

// C++ code

// When I do GetObjectField on Result object (that I received as parameter), to get the member which is GUID, it fails.

JNIEXPORT jint JNICALL Java_CSMSample_SMProcess (

JNIEnv* env, jclass, jobject joResult)

{

jclass jclsResult = env->GetObjectClass(joResult);// Success

// Set ReturnVal.ReturnVal (int)

jfieldID fid_RES_ReturnVal = env->GetFieldID(jclsResult,"ReturnVal","I");// Success

env->SetIntField(joResult, fid_RES_ReturnVal, 777);// Success

// Set ReturnVal.ComponentGUID (GUID)

jfieldID fid_RES_ComponentGUID = env->GetFieldID(jclsResult,"Purpose","LCSMProcess$GUID;");// Success

jobject joRES_ComponentGUID = env->GetObjectField(joResult, fid_RES_ComponentGUID);//This FAILS (joRES_ComponentGUID is zero)

jclass jclsRES_ComponentGUID = env->GetObjectClass(joRES_ComponentGUID);

jfieldID fid_RES_CGUID_Data1 = env->GetFieldID(jclsRES_ComponentGUID,"Data1","I");

jfieldID fid_RES_CGUID_Data2 = env->GetFieldID(jclsRES_ComponentGUID,"Data2","S");

jfieldID fid_RES_CGUID_Data3 = env->GetFieldID(jclsRES_ComponentGUID,"Data3","S");

jfieldID fid_RES_CGUID_Data4 = env->GetFieldID(jclsRES_ComponentGUID,"Data4","[B");

jint jtmpsize = sizeof(g_myGuid.Data4);

jbyteArray jbaCGUID_Data4 = env->NewByteArray (jtmpsize);

env->SetByteArrayRegion(jbaCGUID_Data4, 0, jtmpsize, (const jbyte*)g_myGuid.Data4);

env->SetIntField(jclsRES_ComponentGUID, fid_RES_CGUID_Data1, (jint) g_myGuid.Data1);

env->SetShortField(jclsRES_ComponentGUID, fid_RES_CGUID_Data2, (jint) g_myGuid.Data2);

env->SetShortField(jclsRES_ComponentGUID, fid_RES_CGUID_Data3, (jint) g_myGuid.Data3);

env->SetObjectField(jclsRES_ComponentGUID, fid_RES_CGUID_Data4, (jint) g_myGuid.Data4);

return S_OK;

}

[6126 byte] By [johngummadia] at [2007-11-27 1:30:33]
# 1
Any ideas!? Was wondering how if JNI supports complex structures?John.
johngummadia at 2007-7-12 0:31:57 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

When doing JNI calls which mimic java method calls if it fails then you MUST check for exceptions after the call and normally exit the JNI call immediately (if you don't exit you better understand what an exception means in terms of processing.)

An exception provides more information about what the problem is.

There is no "Purpose" in your code.

And I am not going to check for JNI API specifics but I can note that "ComponentGUID" is null. So you certainly are not going to be able to do anything with it.

jschella at 2007-7-12 0:31:57 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Oops, my bad. "Purpose" should read "ComponentGUID". I just tried to make a sample code out of my original code and missed that "Purpose".

And setting ComponentGUID's values are also wrong in the code. But that was just a mistake while changing the original code to sample code.

But is it not possible to get the object inside an object? I thought GetObjectField should give the member objects of the main object. It returns zero every time.

Is GetObjectField for some other purpose?

John.

johngummadia at 2007-7-12 0:31:57 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

> Oops, my bad. "Purpose" should read "ComponentGUID".

> I just tried to make a sample code out of my original

> code and missed that "Purpose".

>

>

> And setting ComponentGUID's values are also wrong in

> the code. But that was just a mistake while changing

> the original code to sample code.

It helps to post code that actually demonstrates the problem.

>

> But is it not possible to get the object inside an

> object? I thought GetObjectField should give the

> member objects of the main object. It returns zero

> every time.

>

> Is GetObjectField for some other purpose?

>

It returns the value.

I would suggest that you use the code that you posted and create an actual test app which either demonstrates your problem or in fact ensures that the JNI methods do return values.

jschella at 2007-7-12 0:31:57 > top of Java-index,Java HotSpot Virtual Machine,Specifications...