This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
passing Strings throught CNI
- To: java at gcc dot gnu dot org
- Subject: passing Strings throught CNI
- From: stefan dot wanner at ntb dot ch (Wanner Stefan)
- Date: Sat, 13 Oct 2001 16:10:07 +0200
hello
i'm new to cni. what i want to do is, to pass a string from java to a
native call, and from there to a pure c++ call. i have this code fragment:
---------------------------------------------------------------------
sample.java:
public class sample {
public native String cniString(String s);
public static void main(String args[]) {
String s = "abcd";
sample x = new sample();
System.out.println(x.cniString(s));
}
}
---------------------------------------------------------------------
sampleNAT.cc:
#include <gcj/cni.h>
#include "sample.h"
#include <java/io/PrintStream.h>
#include <java/lang/System.h>
#include <java/lang/String.h>
#include <stdio.h>
extern jchar* stlString(jchar* s);
static jchar ch = ' ';
jstring sample::cniString(jstring s){
jchar *ss= JvGetStringChars(s);
ss = stlString(ss);
java::lang::System::out->print(ch);
return s;
}
---------------------------------------------------------------------
sampleSTL.cc:
#include <iostream>
using namespace std;
wchar_t* stlString(wchar_t* s){
wcout << s << " ";
return s;
}
---------------------------------------------------------------------
the output is:
ac abcd
---------------------------------------------------------------------
why ac?
and how can i get the length of a jchar* string? i also want to pass
a string from the stlString funtion back to java.
maybe it's a stupid question (i think it realy is), but
thanks for helping
stefan