File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference to a String in jni to call a dll function of c++.

 
phooton misra
Ranch Hand
Posts: 52
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic