| Author |
Reference to a String in jni to call a dll function of c++.
|
phooton misra
Ranch Hand
Joined: Jul 31, 2012
Posts: 35
|
|
Hi All,
i have a function c++ dll file as follows
boolean getName(String *outStrname){};
it will set a name for a String.
Now i want to call the method from java(jni code);
so i did it like this
in java
Pointer p;
String s =new String("hiiiiii");
p.setvalue(s);
getName(p.getPointer(0));
But when i try to print the value pointed by p its coming as null?
Somebody please suggestme how can resolve this problem.
(how to call by reference of c++ dll function in jni that is of string type.[color=red][/color][size=24][/size])
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1151
|
|
Please UseCodeTags (← click) when posting code.
i have a function c++ dll file as follows
boolean getName( String *outStrname){};
it will set a name for a String.
Why do you have a method called getName() that sets a name, surely it should be called setName()?
But when i try to print the value pointed by p its coming as null?
In the code shown you do not create a Pointer object to assign a reference to 'p' so 'p' will be null.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Tony Docherty wrote:
i have a function c++ dll file as follows
boolean getName(String *outStrname){};
it will set a name for a String.
Why do you have a method called getName() that sets a name, surely it should be called setName()?
In C it's quite common to pass the desired results through pointer arguments, and make the method return a boolean or error code to indicate success. The Windows API is full of those calls. However, in C++ that shouldn't be necessary anymore since exceptions are part of the language. Instead of returning true / false, the name should be returned and an exception should be thrown if the name could not be returned.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1151
|
|
Rob Spoor wrote:In C it's quite common to pass the desired results through pointer arguments, and make the method return a boolean or error code to indicate success.
Oops I used to be a C programmer once upon a time - believe it or not!
I'm more than a bit rusty on JNI - will that work for passing a String back to Java from C++?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
No, you can't pass a C++ String to Java. It needs to be converted to char* which can then be converted to a Java String using JNI's NewStringUTF function. The other way around is a bit more cumbersome, as you first need to call GetStringUTFChars to get a const char*, then ReleaseStringUTFChars when you're done with it.
|
 |
 |
|
|
subject: Reference to a String in jni to call a dll function of c++.
|
|
|