VB.net 2015 Prevent a second CMD instance to show up - vb.net

Shell("cmd.exe", AppWinStyle.MinimizedFocus)
Thread.Sleep(50)
My.Computer.Keyboard.SendKeys("cd c:\users\Administrator\desktop\captcharuc", True)
My.Computer.Keyboard.SendKeys("{Enter}", True)
Thread.Sleep(50)
My.Computer.Keyboard.SendKeys("tesseract.exe imagen.jpg leerca -psm 7", True)
My.Computer.Keyboard.SendKeys("{Enter}", True)
Thread.Sleep(50)
My.Computer.Keyboard.SendKeys("exit", True)
My.Computer.Keyboard.SendKeys("{Enter}", True)
im streaming commands to a CMD instance, everything works fine but when i execute the tesseract.exe another CMD instance opens to execute such program.
this is the regular behavior of tesseract if i do all those steps manually, a second CMD instance will show up to execute the tesseract commands.
this is part of a windows app that im developing and really dont want that second instance to show up.
I have tried with
Const strCmdText As String = "/c cd c:\users\Administrator\desktop\captcharuc\&tesseract.exe imagen.jpg leerca -psm 7"
Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
startInfo.Arguments = strCmdText
Process.Start(startInfo)
and with
Shell("cmd /c """ & "cd c:\users\Administrator\desktop\captcharuc\" & "&tesseract.exe imagen.jpg leerca -psm 7" & """", AppWinStyle.Hide, True)
in the 3 cases the main instance will either run minimized or hidden as intended but the second instance will bring up the tesseract instance.
would really appreciate if i get some help here as I ran out of ideas on how to prevent such event from happening.
thanks in advance

after a good night sleep i came up with a solution to the problem, i did create a .bat file like this
#Echo off
Set _SourcePath=C:\Users\Administrator\Desktop\captcharuc\*.jpg
Set _OutputPath=C:\Users\Administrator\Desktop\captcharuc\
Set _Tesseract="C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%leerca
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
and then called it with
Shell("c:\users\Administrator\desktop\captcharuc\tese.bat", AppWinStyle.Hide)
and thats it.. mods feel free to mark this thread as solved. thanks

Related

VB.NET Process.Start Arguments not passing to CMD

This is my first post here, so please go easy, I also have a good knowledge of VB.
I am making an app that is to create a WiFi hotspot at a click of a button so that I can use my laptop as a WiFi extender for my devices like my phone, however, I am doing this with command prompt. This is my code so far:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim startInfo As New ProcessStartInfo("cmd")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.Arguments = "netsh show wlan drivers"
Process.Start(startInfo)
End Sub
The problem is, it's not passing the arguments. Cmd launches fine but receives nothing.
Things I have tried:
1. Using process.start and ProcessStartInfo
2. Change working directory
3. Send arguments after cmd has launched (after process.start)
4. Change target framework
5. Run in x86 and x64
6. Run as administrator
7. Trying other commands such as "color 2f". Failed.
Any help would be greatly appreciated!
Edit: Not even the WindowStyle argument was passed.
You need to add
startInfo.Arguments = "/C netsh wlan show drivers"
without that flag (/C) the CMD command exits immediately and nothing is executed
In any case your command is wrong. The correct syntax is
netsh wlan show drivers
You could change your code to capture the output of the command in this way
Dim startInfo As New ProcessStartInfo("cmd")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.Arguments = "/C netsh wlan show drivers"
startInfo.RedirectStandardOutput = true
startInfo.UseShellExecute = false
startInfo.CreateNoWindow = true
Dim p = Process.Start(startInfo)
Dim result = p.StandardOutput.ReadToEnd()
p.Close()

Execute DOS command within console application VB

I need to be able to execute a DOS command, such as 'ipconfig', using a command line application in Visual Basic. I can simply use start.process("CMD", "ipconfig"), but that opens a new instance of CMD. I want to be able to run a command like I would with CMD, using a console application, without opening another CMD window. Thanks!
You can use this to run the ipconfig command in hidden console window and redirect the output to local variable. From here you can manipulate it as needed:
Dim cmdProcess As New Process
With cmdProcess
.StartInfo = New ProcessStartInfo("cmd.exe", "/C ipconfig")
With .StartInfo
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With
.Start()
.WaitForExit()
End With
' Read output to a string variable.
Dim ipconfigOutput As String = cmdProcess.StandardOutput.ReadToEnd

VB.Net Printing PDF Fails (But equivalent works from PowerShell)

I'm trying to write a small desktop app that just sits around and prints files as they get downloaded.
Sending a PDF file to the printer with PowerShell works fine and it prints:
.\AcroRd32.exe /N /T C:\Path\to\201402124_label.pdf "Brother QL-700"
However, doing the same in Visual Studio 2012 does not work. The Adobe Reader window opens with the label, and closes, but the file never show up at the printer to be printed. This doesn't make sense because this same code is currently working to send larger PDFs to a duplex printer in the same manner (just using a different printer saved in My.Settings):
For Each file As IO.FileInfo In files
If file.CreationTime > My.Settings.LastRunDate Then
MsgBox(file.Name)
Dim startInfo As New ProcessStartInfo
Dim proc As Process = New Process
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.Verb = "print"
startInfo.Arguments = My.Settings.LabelPrinterSettings.PrinterName.ToString
startInfo.UseShellExecute = True
startInfo.CreateNoWindow = True
startInfo.FileName = file.FullName
proc.StartInfo = startInfo
proc.Start()
proc.WaitForInputIdle()
proc.CloseMainWindow()
End If
Next
I can't figure out why doing this over the CLI/PowerShell works, but not inside VB.net
Why not simply use Process.Start(String, String)? Much more straight forward and clean. You can then use the returned Diagnostics.Process to run your other commands like WaitForInputIdle().
You would probably use something like this:
Dim proc As Process = Process.Start("AcroRd32.exe", _
String.Format("/N /T {0} ""{1}""", _
"C:\Path\to\201402124_label.pdf", "Brother QL-700")

Run already opened process in vb.net

I have a windows form that, on the click of a button, runs a process (MATLAB) and executes a file.
Dim myProcesses() As Process
myProcesses = Process.GetProcessesByName("Matlab")
If myProcesses.Count > 0 Then
'~~~~ what goes here? ~~~~
Else
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "C:\Program Files\MATLAB\R2011b\bin\matlab.exe"
startInfo.WorkingDirectory = MatlabDir 'MatlabDir is defined elsewhere
startInfo.Arguments = "matlab.exe -r test_plot2"
Process.Start(startInfo)
End If
The above code opens MATLAB and executes the script "test_plot2.m" if MATLAB isn't already open. But what do I write in the first IF statement, if MATLAB is already open, and all I want to do is run the file?
Thanks in advance.
Its supposed to be the same. I mean, it doesn't matter if its opened or not, unless the application (Matlab) manages something different, then you have to guess how. Have you tried just using the same code?
Example:
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "notepad.exe"
startInfo.Arguments = "C:\temp\test.txt"
Process.Start(startInfo)
It doesn't matter if you have Notepad already opened or not.

Running CMD command on windows service

I have created a windows service that requires executing an EXE file with the CMD process. I have used the following code:
Str = "C:\PCounter\Staff\account.exe CHARGE " & Name & " " & Amount & " TO" & Id
Dim procStartInfo As New System.Diagnostics.ProcessStartInfo(Str)
procStartInfo.RedirectStandardOutput = True
procStartInfo.UseShellExecute = False
procStartInfo.CreateNoWindow = True
Dim proc As New System.Diagnostics.Process
proc.StartInfo = procStartInfo
proc.Start()
proc.Dispose()
However the above code will return
system cannot find the file specified
I have tried same code on the Windows form, and its works fine. To make sure the path is correct I have added a text file in the same location as EXE file, and load the content of the text file in the service. It works fine.
I can't think of anything else; I really would appreciate it if you can help me on this.
ProcessStartInfo has two properties. One for the executable to run, and the other for the arguments to pass to the executable. The symantics for the Arguments property are the exact same as the command line.
You can not include the arguments in the same property as the executable. They must be separated.
Create service:
sc create Vm-Symantec04 binPath= "\"C:\App32\VMware\VMware Workstation\vmrun.exe\" -T ws start \"D:\VM\Sym04\Sym04.vmx\" nogui" depend= "VMAuthdService/VMnetDHCP/VMUSBArbService/VMware NAT Service" start= auto
Delete service:
sc delete Vm-Symantec04