本文介绍了Delphi xe5 exec root命令转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图隐瞒和
发送shell命令到root电话我不明白为什么我有分段故障。
在互联网上,我找到了类似的代码:

i tried to covert Runtime andProcess 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");

用于linux root权限。

for linux root privileges.

我试过在转换后只用函数发送su命令,但我觉得我错了什么......我认为一个可能的问题可能是数组来自java类型的Jstring转换。

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;

interface
    procedure AskRoot;
implementation
   uses  System.SysUtils,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  FMX.Helpers.Android;

 type
  JProcess = interface;
  JRuntime = interface;
 //----------------------------------JProcess----------------------
  JProcessClass = interface(JObjectClass)
  ['{7BFD2CCB-89B6-4382-A00B-A7B5BB0BC7C9}']

  end;
  [JavaSignature('java/lang/Process')]
  JProcess = interface(JObject)
  ['{476414FD-570F-4EDF-B678-A2FE459EA6EB}']
    {Methods}
    procedure destroy; cdecl;
    function exitValue:integer;cdecl;
    function getErrorStream:JInputStream; cdecl;
    function getInputStream:JOutputStream; cdecl;
    function waitFor:integer;cdecl;
  end;
  TJProcess = class(TJavaGenericImport<JProcessClass, JProcess>) end;
  //----------------------------------Jruntime----------------------
  JRuntimeClass = interface(JObjectClass)
    ['{3F2E949D-E97C-4AD8-B5B9-19CB0A6A29F3}']
    {costant}
  end;
  [JavaSignature('java/lang/Runtime')]
  JRuntime = interface(JObject)
  ['{C097A7EC-677B-4BCB-A4BD-7227160750A5}']
       {Methods}
      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;
  end;
  TJRuntime = class(TJavaGenericImport<JRuntimeClass, JRuntime>) end;

  procedure AskRoot;
  var root:JRuntime;
  begin
    root.getRuntime.exec(StringToJString('su'));
  end;
end.


推荐答案

你的几个 exec 覆盖未标记 cdecl

这无济于事 - 堆栈将获得搞砸了并可能导致分段错误。

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

但是你拨打的那个标记为 cdecl

However the one you call is marked cdecl.

另一方面,您正在调用 root 对象/接口引用的方法,该方法尚未初始化。这种行为肯定会给你一个分段错误。

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 看起来是一个类方法运行时类,所以你输错了界面。当你把它移到右边时,我会想象这样的事情可能会这样:

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:

TJRuntime.JavaClass.getRuntime.exec(StringToJString('su'));

这篇关于Delphi xe5 exec root命令转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:00