dcsimg
Setting the value of a jstring passed in as a parameter.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sammuel_Thumbsy
Posted On:   Friday, July 25, 2003 10:48 AM

Hello, I'm new to the JNI and I would like to know if there is a way to set the value of a jstring passed in as a method parameter. For example: /* wrapper code */ JNIEXPORT jint JNICALL Java_MyTest_setString (JNIEnv *env, jobject this, jstring outbuff, jint len) { int answer; char buff[512]; answer = SOMEFUNCTION(buff,512); /* obviously the strcpy won't work, */ /* but i need the data put into outbuff */ /* without creating a new outbuff. */ strcpy(outbuff,buff); return (jint) answer; } /* c code */ int SOMEFUNCTION(char *b, int len) { strncpy(char,DATA,len)   More>>

Hello,


I'm new to the JNI and I would like to know if there is a way to set the value of a jstring passed in as a method parameter.



For example:

			
/* wrapper code */
JNIEXPORT jint JNICALL Java_MyTest_setString
(JNIEnv *env, jobject this, jstring outbuff, jint len)
{
int answer;
char buff[512];
answer = SOMEFUNCTION(buff,512);

/* obviously the strcpy won't work, */
/* but i need the data put into outbuff */
/* without creating a new outbuff. */
strcpy(outbuff,buff);

return (jint) answer;
}


/* c code */
int SOMEFUNCTION(char *b, int len)
{
strncpy(char,DATA,len);
return 1;
}
   <<Less

Re: Setting the value of a jstring passed in as a parameter.

Posted By:   Alexander_Krapf  
Posted On:   Saturday, September 6, 2003 11:36 AM

Sammuel,


String instances are immutable in Java. While it is technically possible to alter the contents of a String instance using JNI, it is highly counter-recommended because you would be breaking all kinds of assumptions that the JVM makes about objects.


What you want to do instead is pass in a StringBuffer instance and use its methods to modify the value. Alternatively, you could cook up your own StringHolder class that just contains one field which happens to be a string. You can change the field value in your JNI method.


All this gets mighty more complicated and you might want to check out a tool like our JunC++ion tool. It allows you to implement native methods in C++, having full access to Java fields as C++ fields, Java methods as C++ methods.


Good luck,


Alex

About | Sitemap | Contact