File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chaning array contents in java native function

 
Shailesh Kendale
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 classes as follows :
class JavaClass
{
public static native void changeArrayContents(Object obj);
static
{
System.loadLibrary ("DLL_FILE_NAME");
}
public static void main (String[] args)
{
TestClass test_class = new TestClass ();
JavaClass.changeArrayContents (test_class);
}
}
class TestClass
{
byte[] byteArr = new byte[3];
TestClass ()
{
byteArr[0] = 1;
byteArr[1] = 2;
byteArr[2] = 3;
}
}

Then what could be the native code (in c languages) that will mopdify contents of test_class.byteArr.
------------------
 
Shailesh Kendale
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the sol.n
JNIEXPORT void JNICALL Java_PACEEncrypter_changeArrayContents
(JNIEnv *env, jclass cl, jobject obj)
{
int *intPtr;
int i;

jclass javaCl;
jfieldID javaCl_Fld_ID;
jintArray intArray;
//jintArray intArr = (int *)malloc(sizeof(jint) * 3);
javaCl = (*env)->GetObjectClass (env, obj);
javaCl_Fld_ID = (*env)->GetFieldID (env, javaCl, "arr", "[I");
intArray = (jintArray) (*env)->GetObjectField (env, obj, javaCl_Fld_ID);
intPtr = (*env)->GetIntArrayElements(env, intArray, NULL);

printf ("\n\n");
printf ("\nchanging in c program\n");
for (i = 0; i < (*env)->GetArrayLength(env, intArray); i++)
{
intPtr[i] = 55;
printf ("arr[%d] : %d\n", i, intPtr[i]);
}
(*env)->ReleaseIntArrayElements (env, intArray, intPtr, JNI_COMMIT);
printf ("\n\n");
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic