[fpc-pascal] android JNI JString to String

Ludo Brands ludo.brands at free.fr
Tue Apr 26 11:53:51 CEST 2011


PJChar is defined as a pointer to a word. Casting to WideString isn't
working since internally Widestring has extra bytes for reference count and
string length. These extra bytes are locate before the actual string. This
explains why assigning Pointer(WStr) to PJChar works.
Try
 Result := WideString(PWideChar(Chars));  
PWideChar matches probably PJChar and the compiler knows how to convert
PWideChar to WideString.

Ludo



-----Message d'origine-----
De : fpc-pascal-bounces at lists.freepascal.org
[mailto:fpc-pascal-bounces at lists.freepascal.org] De la part de herux
Envoyé : mardi 26 avril 2011 10:37
À : fpc-pascal at lists.freepascal.org
Objet : [fpc-pascal] android JNI JString to String


I have a strange problem, I pass data from the android String via JNI. pass
data from FreePascal wideString successfully using the following code

NewString: function (Env: PJNIEnv; const Unicode: PJChar; Len: JSize):
JString; cdecl; 
GetStringChars:function(Env:PJNIEnv;Str:JString;IsCopy:PJBoolean):PJChar;
cdecl;

function JNI_WideStringToJString(Env: PJNIEnv; const WStr: WideString):
JString; begin
  Result := Env^.NewString(Env, Pointer(WStr), Length(WStr)); end;

but when passing data JString to widestring, using this code :

function JNI_JStringToWideString(Env: PJNIEnv; JStr: JString): WideString;
var
  IsCopy: JBoolean;
  Chars: PJChar;
begin
  if (JStr = nil) then begin
    Result := '';
    Exit;
  end;

  Chars := Env^.GetStringChars(Env, JStr, IsCopy);
  if (Chars = nil) then begin
    Result := ''
  end else begin
    Result := WideString(Chars);
    Env^.ReleaseStringChars(Env, JStr, Chars);
  end;
end;

return only 1 character leading. 
if anyone has ever experienced ? or there's something wrong with JNI header
?

thanks


-----
-
--
View this message in context:
http://free-pascal-general.1045716.n5.nabble.com/android-JNI-JString-to-Stri
ng-tp4340388p4340388.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list