Creating my own CMD in vb.net - vb.net

I'm in need of help with this program which I'm trying to create.
I'm pretty new to programming in general, so please bear with me.
So what I'm trying to create right now, is my own command prompt, and with that said, let me tell you what I want in the command prompt.
As we know from the normal CMD on windows OS systems, we have our input box(or whatever) which we can use to type commands, such as "start google.com", or "ipconfig" etc...
and we have our output, which tells us the results from the commands we typed.
And I wanna create the exact same thing, just with my own UI and some extras which I'm gonna add later.
Here is my issue tho, whenever I type the shell command in my textbox and execute it, it prints out the exception message, so basically it doesn't run the command.
Here is the code:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tmp As System.Windows.Forms.KeyPressEventArgs = e
If tmp.KeyChar = ChrW(Keys.Enter) Then
Try
Shell(TextBox1.Text)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = TextBox1.Text
Catch ex As Exception
RichTextBox1.Clear()
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = ex.Message
End Try
TextBox1.Text = ""
Else
End If
End Sub
I'd really like some examples if possible, since I'm new and I probably wouldn't understand much without examples.

Using a process object instead of a shell is a lot better of an option for having a conversation with cmd. Here's an example of a simple function that you can pass commands to that will return the command's response. This example just does one command/response, but you can use the same process shown below to enter into a dialog with cmd.exe:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(ExecuteCommand("ipconfig /all"))
End Sub
Private Function ExecuteCommand(ByVal command As String) As String
Dim response As String = Nothing
Dim p As New System.Diagnostics.Process
Dim psi As New System.Diagnostics.ProcessStartInfo("cmd.exe")
With psi
.UseShellExecute = False
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.CreateNoWindow = True
End With
p = Process.Start(psi)
With p
.StandardInput.WriteLine(command)
.StandardInput.Close()
If .StandardError.Peek > 0 Then
response = .StandardError.ReadToEnd
.StandardError.Close()
Else
response = .StandardOutput.ReadToEnd
.StandardOutput.Close()
End If
.Close()
End With
Return response
End Function
And as you'll see if you run the code above, you can certainly use commands like ipconfig

Related

I need help my CMD working not the same result

Finally
enter code here
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arg As String
arg = " -X POST -H ""Authorization: Bearer LINE TOKEN HERE"" -F ""message=TEST"" -F ""imageFile=#C:\charts\PIC.png"" https://notify-api.line.me/api/notify"
ShellandWait("curl.exe", arg)
End Sub
Public Sub ShellandWait(ByVal ProcessPath As String, ByVal Arguments As String)
Dim objProcess As System.Diagnostics.Process
Try
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.Arguments = Arguments
objProcess.StartInfo.FileName = ProcessPath
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
objProcess.Start()
Application.DoEvents()
objProcess.WaitForExit()
Application.DoEvents()
Console.WriteLine(objProcess.ExitCode.ToString())
objProcess.Close()
Catch ex As Exception
MsgBox("Could not start process " & ProcessPath & " " & ex.StackTrace.ToString)
End Try`
End Sub
This is not an answer, but, because of my current reputation, I cannot write comments. I feel the need to inform you that blocked out codes in the image you linked are not properly hidden. It is possible to read the top one. I suggest you always use a completely opaque brush to hide important information.
I know that this is not how info is usually sent on StackOverflow, but figured that this guy's privacy is more important than convention in this scenario.
Have you tried the following:
Dim prc as Process
prc = New Process
prc = Process.Start([YourBatchFileLocation])
prc = Nothing
Or, you could also try the following:
Shell([YourBatchFileLocation], AppWinStyle.NormalFocus, True)

Visual Basic 2013 Text duplicating

I had issues with a program, so
I was making a notepad program for my XP overlay thingy, and whenever I reopen it twice, the text is duplicated.
For example when I type in
test
and i reopen it second, it outputs:
testtest
I have no idea what is causing this, but it certainly is annoying.
If I could get some help with this issue that would be awesome!
Heres my code:
Private Sub notepad_X_Click(sender As Object, e As EventArgs) Handles notepad_X.Click
Try
My.Computer.FileSystem.WriteAllText("C:\XP\notepad.txt", RichTextBox1.Text, True)
RichTextBox1.Text = ""
Catch Exc As System.IO.DirectoryNotFoundException
My.Computer.FileSystem.CreateDirectory("C:\XP")
End Try
notepad.Enabled = False
notepad.Visible = False
End Sub
Private Sub Notepad_btn_Click(sender As Object, e As EventArgs) Handles StartM_Notepad.Click
RichTextBox1.Text = ""
Try
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\XP\notepad.txt")
Catch Exc As System.IO.DirectoryNotFoundException
My.Computer.FileSystem.CreateDirectory("C:\XP")
End Try
notepad.Enabled = True
notepad.Visible = True
End Sub
This happens because you have explicitly specified it to do so. You are using the FileSystem.WriteAllText() overload with 3 parameters, where you've set the third parameter to True. That parameter (called append) specifies if you want to append text to your file or overwrite it.
As you've told it to append text to your file it will just write the new text you give it (whatever's in RichTextBox1) after the already existing text. Set the parameter to False so that the text gets overwritten instead:
My.Computer.FileSystem.WriteAllText("C:\XP\notepad.txt", RichTextBox1.Text, False)

VB 2013 - route add cmd elevated

After a few days of googeling and trying i thought let met ask it my self.
I am trying to make this happen:
http://gyazo.com/5274568fcb55a0fe042936e375c0b424
The show current routes is working just fine, and displays the text i ask him to show.
But the route adding not so much let me show you the code :)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmdThread As New Threading.Thread(AddressOf CmdAutomate2)
cmdThread.Start()
End Sub
Private Sub CmdAutomate2()
Dim myprocess As New Process
Dim startInfo As New System.Diagnostics.ProcessStartInfo
startInfo.FileName = "cmd"
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False
startInfo.CreateNoWindow = True
myprocess.StartInfo = startInfo
myprocess.Start()
Dim sR As System.IO.StreamReader = myprocess.StandardOutput
Dim sW As System.IO.StreamWriter = myprocess.StandardInput
sW.WriteLine("route add" & textbox1.text & " " & textbox2.text)
sW.WriteLine("exit") 'exits command prompt window
results = sR.ReadToEnd 'returns results of the command window
sW.Close()
sR.Close()
Invoke(finished)
End Sub
Now i know you should use different pieces of code here mainly:
startInfo.UseShellExecute = True
instead of:
startInfo.UseShellExecute = False
But that gives me errors with the redirect (which i found to be confimed as not possible somewhere else in this forum)
Now i dont care for it to show the output i have the other button for it.
But i cannot seem to et this to work as i get different errors all the time like cannot reditect ot redirect not started with different combinations of code from all over the web,,
What am i missing here??
To be able to edit the route table for your PC, your app needs to run as administrator. you can add a manifest to your application which declares that you require administrator privileges, which should prompt as required.

Console application doesn't want to read standard input

I am writing an application to manage other console application(game server - jampded.exe)
When it's running in console it writes data and reads commands with no problem.
In my application I redirected standard I/O to StreamWriter and StreamReader
Public out As StreamReader
Public input As StreamWriter
Dim p As New Process()
p.StartInfo.FileName = My.Application.Info.DirectoryPath & "\" &
TextBox6.Text 'PATH TO JAMPDED.EXE
p.StartInfo.Arguments = TextBox1.Text 'EXTRA PARAMETERS
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.UseShellExecute = False
p.Start()
input = p.StandardInput
out = p.StandardOutput
Dim thr As Thread = New Thread(AddressOf updatetextbox)
thr.IsBackground = True
thr.Start()
Sub updatetextbox()
While True
While Not out.EndOfStream
RichTextBox1.AppendText(out.ReadLine())
RichTextBox1.AppendText(vbNewLine)
End While
End While
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) _
Handles Button2.Click
input.WriteLine(TextBox4.Text)
TextBox4.Text = ""
input.Flush()
End Sub
When I am pressing Button2 that should write to STD/I text from my textbox, jampded.exe acts like it wasn't written. Also Output works well at startup, after that new lines are added rarely when there is a lot data in buffer.
Am I doing something wrong, or is it the application's fault?
For the standard input question:
Are you certain that the application you're starting is reading data from standard input (and not trapping keyboard events or something)? To test this, put some text that you're trying to send to the application in a text file (named, for example, commands.txt). Then send it to the application from a command prompt like so:
type commands.txt | jampded.exe
If that application reads those commands, then it is indeed reading from standard input. If it isn't, then redirecting standard input isn't going to help you get data to that application.
For the standard output question:
Instead of launching your own thread to handle the data coming from the other application, I would suggest doing something like this:
AddHandler p.OutputDataReceived, AddressOf OutputData
p.Start()
p.BeginOutputReadLine()
Private Sub AddLineToTextBox(ByVal line As String)
RichTextBox1.AppendText(e.Data)
RichTextBox1.AppendText(vbNewLine)
End Sub
Private Delegate Sub AddLineDelegate(ByVal line As String)
Private Sub OutputData(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
If IsNothing(e.Data) Then Exit Sub
Dim d As AddLineDelegate
d = AddressOf AddLineToTextBox
Invoke(d, e.Data)
End Sub
The Invoke call is required because OutputData may get called on a different thread, and UI updates all have to happen on the UI thread.
I've seen the same issue with data coming in batches when reading from the StandardOutput stream directly. The asynchronous read + event handler combo fixed it.

OpenFileDialog under the hood

This is my first question here because I ended up in dead end.
I'm using ZIP 2 Secure EXE (very good software from Chilkat) to create setup.exe for application. ZIP 2 Secure EXE can be run without GUI with one or more parameters.
The problem is that when I call ZIP 2 Secure EXE (ChilkatZipSE.exe) without using OpenFileDialog form to determine location of ChilkatZipSE.exe, it doesn't run process with System.Diagnostics.Process class. The way I call ChilkatZipSE.exe is "..\ChilkatZipSE.exe -cfg settings.xml". Everything is OK with settings.xml and there is UnlockCode node which is needed for creating setup.exe file. When I use OpenFileDialog ChilkatZipSE.exe creates desired setup.exe and it's working fine.
Bellow is my code that I use:
Private Sub btnStartApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartApp.Click
If txtExtAppPath.Text.Length > 0 AndAlso System.IO.File.Exists(txtExtAppPath.Text) Then
Dim myFile As New FileInfo(txtExtAppPath.Text)
txtExtAppLog.Text = StartApplication(myFile.FullName, txtExtParams.Text, chkIsHidden.Checked)
'txtExtAppLog.Text = StartApplication(txtExtAppPath.Text, txtExtParams.Text, chkIsHidden.Checked)
End If
End Sub
Public Function StartApplication(ByVal fileFullPath_ As String, ByVal fileParameter_ As String, ByVal isHidden_ As Boolean) As String
Dim lassie As String = String.Empty
Try
Dim newProcess As New ProcessStartInfo()
newProcess.FileName = fileFullPath_
newProcess.Arguments = fileParameter_
If isHidden_ Then newProcess.WindowStyle = ProcessWindowStyle.Hidden
If System.IO.File.Exists(fileFullPath_) Then
Using startedNewProcess As Process = Process.Start(newProcess)
'startedNewProcess.EnableRaisingEvents = True
startedNewProcess.WaitForExit()
End Using
Else
lassie = "File " + fileFullPath_ + " doesn't exist."
End If
Catch ex As Exception
lassie = ex.Message
End Try
Return lassie
End Function
Thanks, magnumx.
the problem was the given parameter. When using OpenFileDialog it knows where settings.xml is. But when calling "..\ChilkatZipSE.exe -cfg settings.xml" without OpenFileDialog it must be used as "..\ChilkatZipSE.exe -cfg ..\settings.xml"