VB.net - Running a java application using Shell() and set its appdata folder. multiple commands? - vb.net

Alright guys I have a copy of minecraft wich is a java program launched by Minecraft.exe.
Inside the same folder is my program (lets call it launcher.exe) wich I am programming in VB.net and a Folder called LocalAppData.
If I place a shortcut in the same folder as Minecraft.exe, clear the "start in" field and put this in the target field:
C:\Windows\System32\cmd.exe /c start cd LocalAppData&& set APPDATA=%cd%\LocalAppData&& javaw -Xms4096M -Xmx4096M -cp LocalAppData\Minecraft.exe net.minecraft.LauncherFrame
then minecraft launches with my custom memory allocation from inside the LocalAppData folder. Two command windows appear as well. One closes when minecraft does, but the other does not and needs to be closed by the user
My Question is: How do I acheive the same result in VB.net instead of with a windows shortcut and is there a way to either stop the command windows appearing or setting them both to close automatically?
My goal is to launch minecraft from a subfolder, so local filepaths would be far preferrable to global filepaths, but figuring out the location of the application at runtime and working from a subfolder would be ok as well.
I thought I would be able to use the same code inside a Shell() command to produce the same effect, but it appears not.
Ideally I want to create a program that runs minecraft with:
Custom memory allocation
Local filepaths so that it can be run portably
The appdata folder changed to the subfolder so that it can be run portably
Those command windows either gone or minimised and then close automatically when minecraft is closed by the user.
I know this is a big ask, but I'm 6 months into a programming course and I'll admit that I'm not the best programmer out there.
Once I know how to do this I can create the rest of the program that manages multiple installations in seperate subfolders and lets you choose wich one to launch, but I just need help with the actual launching of the java application itself.
Note:
I should clarify that Minecraft.exe is not something that I have made and that I don't program java. I'm just looking for a solution in VB.Net.
Thank you for reading all this and sorry for the long post.
Edit
Thank you for the help. This is what I have so far, but it produces an error "Error: Could not create the JavaVirtualMachine. Error: A fatal exception has occurred. Program will exit"
'Declare Processes
Dim appDataStartInfo As ProcessStartInfo = New ProcessStartInfo()
Dim javaStartInfo As ProcessStartInfo = New ProcessStartInfo()
Dim appPath As String = Application.StartupPath()
'Launch appdata relocation process
appDataStartInfo.FileName = "cmd.exe"
appDataStartInfo.Arguments = "/c start cd " & appPath & "&& set APPDATA=" & appPath & "\LocalAppData"
appDataStartInfo.UseShellExecute = True
Process.Start(appDataStartInfo)
'Launch Minecraft
javaStartInfo.FileName = "javaw.exe"
javaStartInfo.Arguments = "-Xms4096M -Xmx4096M -cp " & appPath & "\LocalAppData\.minecraft\bin\Minecraft.jar net.minecraft.LauncherFrame"
javaStartInfo.UseShellExecute = True
Process.Start(javaStartInfo)
Does anyone see where I've gone wrong?

The Process class (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx )allows you to launch a process. You set it up with a ProcessStartInfo instance (http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.80).aspx ).
I don't have the time to give you all the details, but this pseudo-code should get you started :
Dim startInfo As ProcessStartInfo = new ProcessStartInfo()
startInfo.FileName = "javaw.exe" 'That's the name of your executable
startInfo.Arguments = "your argument line"
startInfo.UseShellExecute = true 'Needed to open a command window
Process.Start(startInfo)

Related

Import-Module not working from within WinForms Application

I have a VB.net app where I invoke Import-Module on a PowerShell from within my vb.net Window Application but the error says it could not find the module. Error as below.
Import-Module : The specified module 'MSOnline' was not loaded because no valid module file was found in any module directory.
When I load the same Module by launching the PowerShell externally in the usual way it works fine. Image as below.
The VB script is as below
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim procStartInfo As New ProcessStartInfo
Dim procExecuting As New Process
With procStartInfo
.UseShellExecute = True
.FileName = "powershell.exe"
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "runas" 'add this to prompt for elevation
Dim psscript As String = My.Resources.mymsolPS
procStartInfo.Arguments = psscript
procExecuting = Process.Start(procStartInfo)
End With
End Sub
My PowerShell Script is saved in my.resource as a txt file. My PowerShell Script is as below.
Import-Module Msonline
Connect-msolService
I replaced the PowerShell script to Get-Help and that works only it dosnt work when I use Import-Module Msonline.
One more information that can be shared is the module is stored in the below location.
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\MSOnline.psd1
Any Help is greatly appreciated.
Thanks in Advance
Update 2:
More fiddling with it found some thing which i am not sure if is relevant.
If I launch the powershell from within my VB.net and run the below command I cant see the MSOnline module.
PS C:\windows\system32>> cd $env:WINDIR\System32\WindowsPowerShell\v1.0\Modules\
PS C:\windows\System32\WindowsPowerShell\v1.0\Modules>> dir
If I run the PowerShell directly from my system and run the above script I can see the Module
d----- 11/22/2017 2:59 PM MSOnline
Still a mystery for me which I cant crack. :(
A difference I notice is when launching from your app, or locally, the directory is either your user, or system.. so maybe the way PS is being loaded it can't find the module.
What about if you provide a full path to the module?
I've had much better luck using RunSpace - I use it to pass any powershell commands - here are a snippet from one of the sites and some examples to look at:
'Create the runspace.
Using R As System.Management.Automation.Runspaces.Runspace = _
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
'Create the pipeline
Using P As System.Management.Automation.Runspaces.Pipeline = R.CreatePipeline()
'Open the runspace.
R.Open()
'Create each command (in this case just one)...
Dim Cmd As New System.Management.Automation.Runspaces.Command("C:\script.ps1", True)
'...and add it to the pipeline.
P.Commands.Add(Cmd)
'Execute the commands and get the response.
Dim Result As System.Collections.ObjectModel.Collection(Of _
System.Management.Automation.PSObject) = P.Invoke()
'Close the runspace.
R.Close()
'Display the result in the console window.
For Each O As System.Management.Automation.PSObject In Result
Console.WriteLine(O.ToString())
Next
End Using
End Using
http://www.winsoft.se/2009/08/execute-a-cmdlet-or-ps1-script-from-visual-basic/
https://social.msdn.microsoft.com/Forums/vstudio/en-US/5d2279c8-e02c-45eb-a631-951c56067bb5/run-powershell-script-from-vbnet?forum=vbgeneral
https://code.msdn.microsoft.com/windowsdesktop/VBPowerShell-6b4f83ea
The last one provides a pretty solid breakdown of what it's doing. Let me know if you can't get it to work, I can try to see if this import works on an app.
I actually found the solution after hours of pain. This is pretty silly solution.
Went I went to my application Properties I found that the Preferred run was set to 32 bit hence when my PowerShell was launched from within it was looking for the module under SYSWOW where its suppose to look it under System32. I unchecked the "Preferred 32 BIT" and not it imports the module from system 32.
Thought I should share this silly miss so that others should not suffer the same.

VB.NET wait for command line input before continuing

I'm writing a program to interface with an inspection machine. The machine has the ability to output command lines during its program run.
The machine has an interface application which allows the passing and automatic start of programs into the machine. I can get this to work with a single instance, however I would like to be able to select several programs for it to run back to back.
Below is the code to pass a single program to the machine:
im ProgramName As String = cbProgram.Text
Dim ProgURL = GetXMLNode(1, ProgramName)
Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
Dim EXEPath = IO.Path.Combine(exeDir.DirectoryName, "iscmd.exe")
' New ProcessStartInfo created
Dim p As New ProcessStartInfo
' Specify the location of the binary
p.FileName = EXEPath
' Use these arguments for the process
p.Arguments = " /run""" & ProgURL & """ /nowait"
' Use a hidden window
p.WindowStyle = ProcessWindowStyle.Hidden
' Start the process
Dim ISCMD As Process = Process.Start(p)
ISCMD.WaitForExit()
MsgBox("PING!")
The PING will show immediately after the program has been passed to the machine (but not completed). What I want to be able to do is build an array of programs and launch this process once each time a program has completed. Is it possible to use a command line from the machine to signal to my software that the next program can be started? Below is a screenshot the machines command line.

launching an app within a vb.net project

I try to lunch an executable file within my windows form vb.net application, but the first screen lunched were maximized..i want to be as it is, in minimized state..
my code is:
Dim oProcess As System.Diagnostics.Process
Dim oPSI As New System.Diagnostics.ProcessStartInfo
oPSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized
oPSI.FileName = "path\application.exe"
oPSI.Arguments = ""
oProcess = Process.Start(oPSI)
oPSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized
Remove this line
I you launch you application from a terminal does i work?
if yes you can use
System.Console.Write()
to do it like in the terminal but from your programm.
I think you can tell the terminal to open it in minimize like so:
System.Console.WriteLine("start /min apli.exe")
Good luck!

VB.NET run BATCH from current directory

I have a windows batch file which I want to execute using vb.net however the batch as well as the VB.net exe that will execute it are run from the cd rom which means tat i want my vb.net to run the batch from current directory (as both will be placed in the current directory, on CD)
How can I achive this?
You need to create an instance of ProcessStartInfo class, set the property WorkingDir and FileName (eventually also the Arguments property) and pass this instance to the Start static method or the Process class.
Dim pi = new ProcessStartInfo()
pi.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath)
pi.FileName = "your_batch_file_name"
pi.Arguments = "arguments that you want to pass to the batch file"
Process.Start(pi)
Keep in mind that if you run from a CD then your current working directory is not writeable

How can I use VB.NET to execute a batch file on another computer?

In vb.net 2008 I want to execute a batch file that resides on another computer.
There is no error, but nothing happens.
Here is the code:
Dim pStart As New System.Diagnostics.Process
Dim startInfo As New System.Diagnostics.ProcessStartInfo(serverpath & "\file.bat")
startInfo.RedirectStandardOutput = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.UseShellExecute = False
pStart = System.Diagnostics.Process.Start(startInfo)
pStart.WaitForExit()
pStart.Close()
To run a process on a remote computer you can use Sysinternals free psexec.
You can call it with the proper parameters and having the required permissions like you are doing in your sample code.
I've never tried to create a Process using a batch file as the executable. I've always had to use cmd.exe as the program. This has worked for me in the past:
Dim startInfo As New System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " & serverpath & "\file.bat")
The "/c" as part of the argument list tells cmd.exe to exit after the batch file has completed.
If you are going to use RedirectStandardOutput, you really do want to use RedirectStandardError, and then also subscribe to the events of the Process class for catching data on those streams (OutputDataReceived and ErrorDataReceived). Otherwise you will have no way to debug your batch script.
This reads like a permissions problem. I would troubleshoot it that way if you haven't ruled it out yet.
Have you tried running the same batch file from the local computer?
If it is a permissions issue you can solve it either copying the file locally before executing it or map a drive to the remote computer where the file is and then execute the batch file from the new path.
Additionally, we don't really know what's in the batch file that could be causing an issue. I would either post the batch file or if you can't post the batch file use a batch file that you can post. Example a batch file would write the current date time to a file.