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]

passing Strings throught CNI


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


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