user1931849 user1931849 - 6 months ago 70
Android Question

Delphi xe5 exec root command conversion

i tried to covert Runtime and
Process to send shell command to the rooted phone i don't understand why i had segmentation fault.
On internet i found java code like:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});` for reboot of the phone or `Runtime.getRuntime().exec("su");

for linux root privileges.

I tried only to send the "su" command with a function after the conversion but i think i wrong something... I think that one possible problem can be the array of Jstring conversion from java type.

unit Androidapi.JNI.Root;

procedure AskRoot;
uses System.SysUtils,

JProcess = interface;
JRuntime = interface;
JProcessClass = interface(JObjectClass)

JProcess = interface(JObject)
procedure destroy; cdecl;
function exitValue:integer;cdecl;
function getErrorStream:JInputStream; cdecl;
function getInputStream:JOutputStream; cdecl;
function waitFor:integer;cdecl;
TJProcess = class(TJavaGenericImport<JProcessClass, JProcess>) end;
JRuntimeClass = interface(JObjectClass)
JRuntime = interface(JObject)
procedure addShutdownHook(hook:JThread);cdecl;
function availableProcessors:integer; cdecl;
function exec(progArray,envp:array of JString):Jprocess; overload;
function exec(progArray:Jstring; envp:array of JString;directory:JFile):Jprocess; overload;
function exec(progArray,envp:array of JString;directory:JFile):Jprocess; overload;
function exec(prog:JString;envp:array of JString):Jprocess; cdecl; overload;
function exec(progArray:array of JString):Jprocess; overload;
function exec(prog:JString):Jprocess; cdecl; overload;
procedure Exit(code:Integer);cdecl;
function freeMemory:LongInt;cdecl;
procedure gc; cdecl;
function getLocalizedInputStream(stream:JInputStream):JInputStream; cdecl;
function getLocalizedOutputStream(stream:JOutputStream):JOutputStream; cdecl;
function getRuntime:JRuntime;cdecl;
procedure halt(code:Integer);cdecl;
procedure load(pathName:JString);cdecl;
procedure loadLibrary(libName:JString); cdecl;
function maxMemory:LongInt;cdecl;
function RemoveShutdownHook(hook:JThread):Boolean;cdecl;
procedure runFinalization;cdecl;
procedure runFinalizersOnExit(run:Boolean);cdecl;
function totalMemory:LongInt;cdecl;
procedure traceInstructions(enable:Boolean);cdecl;
procedure traceMethodCalls(enable:Boolean); cdecl;
TJRuntime = class(TJavaGenericImport<JRuntimeClass, JRuntime>) end;

procedure AskRoot;
var root:JRuntime;

Answer Source

Several of your exec overrides are not marked cdecl.

That won't help - the stack will get messed up and potentially lead to segmentation faults.

However the one you call is marked cdecl.

On the other hand, you are calling a method of the root object/interface reference, which you have not initialised. That sort of action is sure to give you a segmentation fault.

getRuntime looks to be a class method of the Runtime class, so you've put in the wrong interface. When you've moved it to the right one, I'd imagine something like this might do it:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download