Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands
Executing cmd prompt commands Executing cmd prompt commands
Executing cmd prompt commands
Go Back  Xtreme Visual Basic Talk > > > Executing cmd prompt commands


Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2010, 02:13 AM
drbogger drbogger is offline
Newcomer
 
Join Date: Dec 2010
Posts: 3
Default Executing cmd prompt commands


Sorry about the bad title, i'm really tired and i'm not sure how to properly explain this, so bare with me...

Okay, heres the deal...

I need to open up a cmd prompt, then run a file called adb.exe which opens a shell to communicate with my android cell phone...

Heres what I need:

My Form1

Code:
Label1.Text = "adb.exe shell"
Label2.Text = "su"
Label3.Text = "mkdir /etc/folder"
Label4.Text = "cp /sdcard/file.zip /etc/folder/file.zip"
Label5.Text = "cd /etc/folder"
Label6.Text = "unzip file.zip"

I want the above inserted into Command Prompt, like so:
Code:
C:\> adb.exe shell
$ su
# mkdir /etc/folder
# cp /sdcard/file.zip /etc/folder/file.zip
# cd /etc/folder
# unzip file.zip
Heres the problem... I can execute any normal dos command automaticly, but once i open the ssh prompt (adb.exe shell), i dont know how to keep putting commands in there...

The commands are stored in strings, and i need to take the commands from the strings, and type them into the command prompt like above.

Anyone have any ideas?

I feel like a retard because I know i'm not explaining this right, but its 3am and I've spent the last 48 hours trying to figure out how to do this...
Reply With Quote
  #2  
Old 12-16-2010, 12:19 PM
AtmaWeapon's Avatar
AtmaWeaponExecuting cmd prompt commands AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It's either going to be easy or impossible; it all depends on how adb.exe is implemented. If it reads its input from standard input, all's going to be well. If it doesn't, it might take something much more complicated.

First, double-check that it's necessary to use input redirection. Many programs that work the way you're specifying have some kind of command-line switch that tells them to use some script file for input; it'd be much easier to save your commands in a file than it would be to write a program to communicate via input redirection. If you can't do that, read on.

I like to teach rather than just demonstrate, so let's talk about what we're going to do. Right now it looks like you need to open cmd.exe, then send the commands to that. That's actually adding an extra step. The command prompt only needs to be there so something with a standard input stream exists; when it starts adb.exe it creates the process and associates its I/O streams with the command prompt's own streams. So it'd be best to skip the middle-man and start adb.exe directly.

Starting a process is a pretty easy thing to do. The System.Diagnostics.Process class is what you want. It has a Start() method that starts a process. There's many overloads to help control how the process is started; in my opinion the most important one is the one that takes a [url=http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx]ProcessStartInfo[/i] parameter. That class has properties that support all of the various ways to start a process. Let's talk about using it.

Step 1 is starting adb.exe in shell mode. To do this, you need to pass a command-line argument to it. To do that, you can set the Arguments property of the ProcessStartInfo. Some people expect this to be an array, but it's not. It's a string, and it's used as-is. Imagine as if this is what Process.Start does:
Code:
SendToConsole(startInfo.FileName & " " & startInfo.Arguments)
That means you're on the hook for proper spaces, quotation marks, etc. We all lament the presence of a good argument building/parsing API in the framework. Here's what you might write to get the shell started:
Code:
Dim startInfo As New ProcessStartInfo("adb.exe") ' You might need to include the full path
startInfo.Arguments = "shell"

Dim adbProcess As Process = Process.Start(startInfo)
That'll start it, but if you run it right now you'll see abd in its own window waiting for you to type stuff to it. In order to send input to it, we need to add some stuff. If you set the ProcessStartInfo.RedirectStandardInput property to True, it will enable you to get a stream that lets you write to the process's standard input stream. (You also have to set UseShellExecute to false, the technical reasons aren't important.) You do this by using the Process.StandardInput property; it returns a StreamWriter you use to send text to the other process. Remember, if the target process reads input line-by-line you have to make sure to send line breaks too (WriteLine() will do that.) Here's some code that sends a line to abd.exe:
Code:
Dim startInfo As New ProcessStartInfo("adb.exe")
startInfo.Arguments = "shell"
startInfo.UseShellExecute = False
startInfo.RedirectStandardInput = True

Dim adbProcess As Process = Process.Start(startInfo)
Dim adbInput As IO.StreamWriter = adbProcess.StandardInput
adbInput.WriteLine("su")
adbInput.WriteLine("mkdir /etc/folder")
Run this, and you'll see something interesting but it will probably hang. We have to be able to get the output from adb.exe and we also need to be able to tell it when to quit (or tell our own program when to kill it.) One thing at a time. To read the output you do something like how you send input: set UseShellExecute to false, set RedirectStandardOutput to True, and use the StandardOutput property to get a StreamReader to read the output:
Code:
Dim startInfo As New ProcessStartInfo("adb.exe")
startInfo.Arguments = "shell"
startInfo.UseShellExecute = False
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True

Dim adbProcess As Process = Process.Start(startInfo)
Dim adbInput As IO.StreamWriter = adbProcess.StandardInput
Dim adbOutput As IO.StreamReader = adbProcess.StandardOutput

' Send the input
adbInput.WriteLine("su")
adbInput.WriteLine("mkdir /etc/folder")
adbInput.WriteLine("exit")
adbInput.Close()

' read the output:
While Not adbOutput.EndOfStream
    Console.WriteLine("|output:| {0}", adbOutput.ReadLine())
End While
adbOutput.Close()

adbProcess.WaitForExit()
There's some nastiness to discuss that I'm going to gloss over; I want to write a more detailed article for the general topic, not a specific application. Here's what you need to know.

(I tested against a fake program I renamed to adb.exe; my program printed out some lines and would print something in confirmation of each line it received. This is why I will seem ignorant of adb.exe despite having examples.)

Until the streams are closed, the process can't exit. If I hadn't called Close() on both the input and output streams, WaitForExit() would hang because while the streams are open, the application is still considered active. You'll also notice I didn't read the output until the very end. I *wanted* to display output as it was coming in, but that's actually kind of complicated. Methods like ReadLine(), Read(), and ReadToEnd() will hang if the target process hasn't closed its end of the stream. Why's this matter? My fake adb.exe output a few lines at the start based on command-line arguments. I thought I could read them without hard-coding the number of lines by looping until EndOfStream was true; unfortunately this never happens until the application closes its end of the stream. I tried to see what would happen when ReadLine() would read, and the result was a hang. I tried Peek(), and the results were inconsistent. You can use asynchronous methods to handle this problem, but it was a bit more complicated than I wanted to deal with.

So how come I can use ReadToEnd()? To answer that, let's answer a different question: what does it mean for the target application to close its end of the stream? My fake abd.exe responded to the "exit" command by breaking out of its loop and letting the End Sub of Sub Main() get hit. This transfers control away from my program code and into the bowels of whatever Windows does in that case. Part of that process is it disconnects the input/output streams. My program still holds the other end of that stream, and there's data in it. When the target application's output stream is disconnected like this, it does *something* that tells the stream there's an end to the data. I don't know what it is, nor do I really care. I just know that when you're sure adb.exe has exited, it's safe to call a method like ReadToEnd().

Do make sure to always close both the input and output stream though; if you leave them open Windows assumes you still want to send some input or output (it doesn't know the program code is finished for some reason) and methods like WaitForExit() will hang forever.

My code's for a console application, but you ought to be able to see how to use it from your application. As I stated before, if adb.exe does something fancy and doesn't expect input from standard input, none of this is the answer and I have no idea how you should progress.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 12-16-2010, 06:52 PM
drbogger drbogger is offline
Newcomer
 
Join Date: Dec 2010
Posts: 3
Default

Okay, that works great!

Just another quick question:

Is there a way to make the cmd prompt hidden?

Problem:
Code:
startInfo.WindowStyle = ProcessWindowStyle.Hidden

Will not work if:

startInfo.UseShellExecute = False
Reply With Quote
  #4  
Old 12-16-2010, 08:16 PM
drbogger drbogger is offline
Newcomer
 
Join Date: Dec 2010
Posts: 3
Default

Nevermind, this worked:

Code:
startInfo.CreateNoWindow = True
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands Executing cmd prompt commands
Executing cmd prompt commands
Executing cmd prompt commands
 
Executing cmd prompt commands
Executing cmd prompt commands
 
-->