JNI: Retrieve Java string from native code

Joined
Apr 23, 2008
Messages
3
Reaction score
0
Greetings,

I'm trying to get a string variable from a java class into a delphi program. I've tried to access it with a function and directly via GetObjectField and some other ways. All that I'm getting is an AV at any point for any reason.

Can anyone give me a tiny example how I can get a string from a function or the class object ? C or C++ examples are welcome.

Any hint will be appreciated very much.
 
Joined
Apr 23, 2008
Messages
3
Reaction score
0
Not really...

I can get the JAVA variables as return values of routines, but not themselves. Example Code:

------------------- snip -----------------------
function TJavaObject.GetStringVar( Name: String ): String;
var
FSig: string;
fid: jFieldID;
CharBuf : PChar;
len: integer;
isCopy: jBoolean;
penv : PJNIEnv;
js: jString;
begin
penv := getPenv;
fid := penv^.GetFieldId(penv, ClassRef, PAnsiChar(Name), '()Ljava/lang/String;');
js := penv^.GetObjectField(penv, Handle, fid);

CharBuf := penv^.GetStringUTFChars(penv, js, IsCopy);
len := penv^.GetStringUTFLength(penv, js);
SetLength(Result, 1+len);
StrLCopy(PChar(result), Charbuf, len);
if IsCopy then
penv^.ReleaseStringUTFChars(penv, js, CharBuf);
end;
------------------- snip -----------------------

As you can see I use the JNIWrapper, but the methods are the same...
... it's crashing in the GetStringUTFChars function.


Ideas anybody ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top