Introducing XDA:DevCon – A Conference For Developers By Developers
XDA Developers Android and Mobile Development Forum
Forgot your password?
 
Post Reply+
Tip us?
 
grawkiller73
Old
#1  
grawkiller73's Avatar
Senior Member - OP
Thanks Meter 58
Posts: 176
Join Date: Jan 2011
Location: Tinley Park, Illinois
Question [Q] ADB Shell Input from C++?

Hi XDA. I am in the process of developing a very user-friendly C++ CLI application to root Android phones. I will just come straight out and say it - is there any way to input ADB Shell commands from a C++ application, or any application for that matter? Just to clear up any confusion, my code is as so:

Code:
system("adb shell");
???????
From that point on, how would I input shell commands?

Thanks in advance.
The Following User Says Thank You to grawkiller73 For This Useful Post: [ Click to Expand ]
 
Smasher816
Old
#2  
Smasher816's Avatar
Senior Member
Thanks Meter 154
Posts: 388
Join Date: Jan 2011
Location: Missouri

 
DONATE TO ME
Well I can tell you how to do it in Java, you should be able to do it in some similar way in c++.
In Java you create a process (program). Then you can get its input and output streams.
Then you can inject commands to its input, and read from its output.
Hope it helps. (Ignore my accidental thanks if you want, or return the favor )
and remember use the THANKS button

Retired: Samsung Captivate
Current: Samsung Galaxy S III

Glitch Updater App (Developed by yours truly)
The Following User Says Thank You to Smasher816 For This Useful Post: [ Click to Expand ]
 
Mayazcherquoi
Old
#3  
Mayazcherquoi's Avatar
Senior Member
Thanks Meter 105
Posts: 175
Join Date: Feb 2011
Location: Sydney, New South Wales, Australia
Create the process using CreateProcess(), that will give you heaps of control over the program you're calling:
http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx

I'm not entirely sure how ADB works, but if possible, you could send it command line parameters (just like any other console application) with the lpCommandLine parameter of CreateProcess. However, it would be much more simple to do such a thing with the system method.

If the ADB shell isn't like any other console application, you could use window messages and emulate keystrokes/data sent to the application.

You could also look at this library.

Hope I helped,
Regards,
Mayazcherquoi

EDIT: Whilst googling the subject out of my own interest, I came across this. Hopefully that should sufficiently answer your question
The Following User Says Thank You to Mayazcherquoi For This Useful Post: [ Click to Expand ]
 
grawkiller73
Old
#4  
grawkiller73's Avatar
Senior Member - OP
Thanks Meter 58
Posts: 176
Join Date: Jan 2011
Location: Tinley Park, Illinois
Thank you very much for that sufficient answer Maya. Ill check you links when I get home. Smasher, could you reply with an example code snippet doing the process is Java? Thanks.

Sent from my T-Mobile myTouch 3G Slide using XDA App
 
Smasher816
Old
(Last edited by Smasher816; 24th July 2011 at 07:12 PM.)
#5  
Smasher816's Avatar
Senior Member
Thanks Meter 154
Posts: 388
Join Date: Jan 2011
Location: Missouri

 
DONATE TO ME
Ya sure. sorry for the delay, i have been busy.
Hope there are enough comments
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;

try {
	//Create Process
	ProcessBuilder pb = new ProcessBuilder("adb");	//Create process builder (settings)
	pb.redirectErrorStream(true);			//Combine output and error -> stdout
	Process p = pb.start();			//Start program and get process handle
	//Run command
	OutputStream in = p.getOutputStream();		//Get output stream, to write commands into
	string command = "whatever\n";		//DO WHATEVER COMMANDS AND STUFF HERE
	in.write(command.getBytes()); in.flush()		//Write the command to the programs stdin
	//Get output
	in.close(); p.waitFor();			//Stop input, wait for program to be done (optional)
	BufferedReader out = new BufferedReader(new InputStreamReader(p.getInputStream()));	//Get a buffered reader for the processes output (our input)
	String output=""; String line="";			//Response, and temperary line (used to break loop when null) 
	while (line!=null) { line=out.readline(); response+=input;}	//Keep reading lines untill no more (null)
	out.close();				//Close output stream (good practice)
} catch (Exception e) { e.printStackTrace(); }		//Print error trace back. Do whatever here
//do whatever with "output"
and remember use the THANKS button

Retired: Samsung Captivate
Current: Samsung Galaxy S III

Glitch Updater App (Developed by yours truly)
 
Post Reply+
Tags
adb, c++, cli, program, shell
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

report this ad
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Go to top of page...