VB.Net ProcessStartInfo not working in Windows Service - vb.net

I want to stop a windows service in a different service.
When I run the below code in a desktop app, it works correctly but same code doesn't work in a Windows Service app.
Is somebody has any idea?
PS: I tried to use SC stop instead of net stop but the result is exactly the same.
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = " " + "/K" + " " + "net stop MyService"
pi.CreateNoWindow = True
pi.UseShellExecute = True
pi.FileName = "cmd.exe"
pi.Verb = "runas"
p.StartInfo = pi
p.Start()

Related

standard outputread does not read anything until process is closed vb.net

I am trying to redirect the output from xfoil.exe with downloadables here. The issue is when I am redirecting the standardoutput, my code will not read anything until I use the "quit" command which closes the external xfoil.exe and hence, I cannot run any further commands.
first defining variables
Dim p as process = New Process()
Dim startinfo = New ProcessStartInfo()
Dim bt As Threading.Thread
I start the process using
With startinfo
.FileName = Application.StartupPath & "\appdata\xfoil.exe"
.Arguments = ""
.WorkingDirectory = Application.StartupPath
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.UseShellExecute = False
.CreateNoWindow = True
End With
p.StartInfo = startinfo
p.EnableRaisingEvents = True
If Not IsNothing(bt) Then bt.Abort()
bt = New Threading.Thread(AddressOf ReadThread)
bt.IsBackground = True
bt.Start()
p.Start()
The definition for the ReadThread is
Private Sub ReadThread()
Dim rLine As String
Do Until Leaving
Try
rLine = p.StandardOutput.Read()
logtext += (Chr(rLine))
If p.StandardOutput.Peek = -1 Then
Me.Invoke(Sub() txtLog.AppendText(logtext))
logtext = ""
End If
Catch ex As Exception
Console.WriteLine("error " & ex.Message)
End Try
Loop
End Sub
This setup works perfectly with another app from the same developer Mark Drela. When I use the other app AVL with download link here everything works fine. However, xfoil does not work with this setup and I have spent some hours on this without any luck. I am using VB.net on Windows 10 in Visual Studio.NET 2019 community edition.
Edit
Both methods using threading and events were implemented and no luck.
A sample code using both methods in VB.net is now available on Github for review comments. Let me know if anyone has any experience with this and can help. This would really help my students in the class!

How to run CMD commands and hide it

Hey i am programming in Visual Basic (VB.NET) and i am trying to run cmd commands without showing the cmd screen on the computer, I have the following code but i cant get it to hide it.. :(
Code:
Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
Thread.Sleep(5000)
Ugly code aside, you can basically do it like this:
Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Dim p2 As New Process
p2.StartInfo.FileName = "Cmd.exe"
p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt"
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p2.Start()
Thread.Sleep(5000)
Process.Start is a static method with a limited set of parameters for modifying its behavior.
So where you give the name of the .exe and the parameters via the static method
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
, is similar to:
Dim p2 As New Process
p2.StartInfo.FileName = "Cmd.exe"
p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt",
p2.Start()
But by doing it like that you have many more options available, including the option to set the WindowStyle to hidden:
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

Command line works in command prompt but not in VB.NET

My command:
c:\temp\abc.exe c:\temp\abc.ini >> c:\temp\log.log
This works fine when executed by the command prompt.
But from VB.NET, it doesn't work (the log file was created but blank, the log file should contain log processes of abc.exe, abc.exe was not executed also).
Dim p as Process = new Process()
Dim pi as ProcessStartInfo = new ProcessStartInfo()
pi.Arguments = "/C c:\temp\abc.exe c:\temp\abc.ini >> c:\temp\log.log "
pi.FileName = "cmd.exe"
p.StartInfo = pi
p.Start()
p.WaitForExit()
Why?
Update: While waiting for an explanation, this is my workaround.
Dim p as Process = new Process()
Dim pi as ProcessStartInfo = new ProcessStartInfo()
pi.Arguments = "c:\temp\abc.ini"
pi.FileName = "c:\temp\abc.exe"
p.StartInfo = pi
p.Start()
Dim output as String = p.StandardOutput.ReadToEnd()
p.WaitForExit()
WriteLog("c:\temp\log.log", output)

Change machine name from code, win7

i'm writing a little utility for my job, i need to change the actual machine name into serial number of the machine, i already found how to get serial number but i have no idea how to set the machine name and every solution i have tried haven't worked, someone have some suggestions?
Dim q As New SelectQuery("Win32_bios")
Dim search As New ManagementObjectSearcher(q)
Dim info As New ManagementObject
Try
For Each info In search.Get
Call MessageBox.Show("Serial Number: " & info("serialnumber").ToString +
vbCrLf + "Machine Name : " + Environment.MachineName)
Next
Return 1
Catch err As ManagementException
Call MessageBox.Show("Error: " & err.Message)
Return -99
End Try
I found a solution
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Verb = "Runas"
pi.WindowStyle = ProcessWindowStyle.Hidden
pi.Arguments = "/K WMIC computersystem where caption='" + Environment.MachineName + "' rename Prova"
pi.FileName = "cmd.exe"
pi.UseShellExecute = True
pi.CreateNoWindow = True
Process.Start(pi)
It's just an utility for me but there is a way if i don't want ask the permission to run the process?

How can I install Choco and a few applications from the same call in my application?

The first time I open and run the program it installs Choco but will not install the applications. If I close the application and run it again, it will then install the applications. My guess is the WinForm doesn't know it can use the choco command? Is there a way to have the application refresh its system?
When installing Chocolatey it does display this:
You may need to shut down and restart powershell and/or consoles first prior to using choco.
So I might just be SOL, but I figured I couldnt be the first person to attempt to do this type of thing.
Below is my code for installing Chocolatey and then installing a list of applications using it.
Sub InstallChocoApps()
RunCmd("#""%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"" -NoProfile -ExecutionPolicy Bypass -Command ""iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"" && SET ""PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin""", "", False, True)
Dim Packages() As String = {"notepadplusplus.install", "7zip.install", "firefox", "googlechrome", "putty.install", "sumatrapdf.install", "vlc"}
For Each p In Packages
RunCmd("choco install " & p & " --force -y --no-progress", "", False, True)
Next
End Sub
Sub RunCmd(command As String, arguments As String, permanent As Boolean, display As Boolean)
Try
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
pi.FileName = "cmd.exe"
pi.WindowStyle = ProcessWindowStyle.Hidden
pi.CreateNoWindow = True
pi.Verb = "runas"
pi.UseShellExecute = False
p.StartInfo = pi
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()
p.WaitForExit()
p.Close()
Catch ex As Exception
LogData(ex.ToString())
End Try
End Sub
Try installing Chocolatey and the packages in one call to RunCmd(). Either that or reference the absolute path of the Chocolatey executable when running the choco command. I believe right now it's C:\temp\MyChocolatey\bin\choco.exe.