This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: passing Strings throught CNI


>>>>> "Wanner" == Wanner Stefan <stefan.wanner@ntb.ch> writes:

Wanner>      public native String cniString(String s);
 [ ... ]
Wanner> 	String s = "abcd";
 [ ... ]
Wanner> 	System.out.println(x.cniString(s));

Wanner> extern jchar* stlString(jchar* s);
Wanner> static jchar ch = ' ';

Wanner> jstring sample::cniString(jstring s){
Wanner>      jchar *ss= JvGetStringChars(s);
Wanner>      ss = stlString(ss);
Wanner>      java::lang::System::out->print(ch);
Wanner>      return s;
Wanner> }

Wanner> wchar_t* stlString(wchar_t* s){

Wanner> the output is:
Wanner> ac abcd
Wanner> why ac?

You're using JvGetStringChars to get the characters.  This doesn't
return a 0-terminated buffer (which is what I assume stlString
expects).  So you're lucky you don't get more data, I guess.

Also, note that there is no guarantee that a jchar and a wchar_t will
be the same thing.  jchar is always a 16 bit Unicode character.
wchar_t is system- and sometimes locale-dependent.

Wanner> and how can i get the length of a jchar* string?

You can't get the length if you just have a jchar*.
However in your code you can use `s->length()' to get the length of
the String, just like you would use `s.length()' from Java.

Wanner> i also want to pass a string from the stlString funtion back
Wanner> to java.

There are various ways, but you probably want
    JvNewString(const jchar *, jsize)
The second argument is the length.

Tom


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]