dcsimg
How to convert pointer to struct in c to java object reference
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rajeev_Misra
Posted On:   Tuesday, October 16, 2001 07:23 AM

Hi,

I am using JNI in my java program to call legacy c function. This fuction returns the pointer to c struct and this reference to this is required as i need to access the member variable of this struct in my java program.

any thoughts
Rajeev

Re: How to convert pointer to struct in c to java object reference

Posted By:   Anil_Datt  
Posted On:   Tuesday, October 23, 2001 01:51 PM

You can keep a long variable (to address 64 bit needs) in you java program.For example call it as



long cObjectID;


in your C/C++ code you can have funtion like the following


void setCObjectId
(JNIEnv *env, jobject thisObject, jlong lCObjectId){

jclass classzz = env->GetObjectClass(thisObject);
jfieldID fid =env->GetFieldID(classzz,"cObjectID","J");
env->SetLongField(thisObject,fid,lCObjectId);


}


which sets the pointer associated with you struct pointer which is passed through "lCObjectId".


You can have the additional method


jlong getCObjectId
(JNIEnv *env, jobject thisObject){

jclass classzz = env->GetObjectClass(thisObject);
jfieldID fid =env->GetFieldID (classzz,"cObjectID","J");
jlong id = env->GetLongField(thisObject,fid);
return id;

}


to get the Pointer.



Similar to to this you can have a set and get in you java class to reference the pointer.

About | Sitemap | Contact