Send or received arguments from process in VB
In your visual Studio program, you could send and received parameters or arguments. You could start a program with arguments; you could get parameters from another process. It isn�t complicate with this example.
First, I�ll explain a little before I show you the code.
Number 1, I created a windows application form with no control in it because. No textbox, no label, no button. Controls are not important here and not required. I call my first program SampleStartUpProgram.
If you start using the debug from Visual Studio 2010, it will send 1 argument named �toto� to the exe file from SampleStartUpProgram. Each time SampleStartUpProgram.exe is started, it will first try to load the arguments from the load function.
After, your program could do anything it has to do, calculate values, gather information, anything. Then, the first program will run a Process.Start with some arguments. In my example I haven�t change the �toto� for anything else. I am just simple passing the same argument again.
Here is a sample of the first program:
''' <summary> ''' this is a Sample. Acts like a startup program or splash screen. ''' It loads, do some work (to do...), get some parameters. ''' in this case the argument is "toto". ''' After the work in this program, it will start a new process and send ''' the arguments (in this case "toto") ''' The MainApplication.exe will receive "toto" and will do some other work ''' </summary> ''' <remarks>chance that your antivirus will intercept the second program</remarks> Public Class Form1 Private mArgs() As String 'les arguments #Region "load and constructors" Public Sub New() InitializeComponent() End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'MsgBox("000000 " & Application.StartupPath.ToString) Me.Text = My.Application.Info.AssemblyName.ToString LoadArguments() 'MsgBox(Application.StartupPath.ToString & "\" & "MainApplication.exe") Dim param_String As String param_String = "" For index1 = 1 To mArgs.Length - 1 Step 1 param_String = param_String & " " & mArgs(index1) Next Process.Start(Application.StartupPath.ToString & "\" & "MainApplication.exe", param_String) End Sub #End Region #Region "private methods" Private Function LoadArguments() As String() 'Throw New NotImplementedException mArgs = Environment.GetCommandLineArgs If mArgs.Length > 0 Then 'MsgBox(mArgs(0)) End If LoadArguments = mArgs End Function Private Function GetArguments() As String() 'Throw New NotImplementedException mArgs = Environment.GetCommandLineArgs If mArgs.Length > 0 Then End If GetArguments = mArgs End Function #End Region End Class |
The real work in the previous code is the Process.Start. param_String is simple a big phrase with a lot of argument separated by spaces.
Number 2, if you need to get argument after you started you program, is very easy. There is like 2 steps and unlike a sub or a function, you don�t need to read the argument at the beginning. That is why in your 2nd project, you could use the GetCommandLineArgs function to retrieve all the arguments from you command line. All argument will be in a string array.
The first argument will be your exe file. The second will be in fact your first parameter. So toto will be in the second value in your array.
Here is the code:
''' <summary> ''' this is the second program. It receive the arguments ''' created from the previous programs ''' </summary> ''' <remarks>this program might be intercepted by your antivirus</remarks> Public Class Form1 Private mArgs() As String 'les arguments #Region "load and constructors" Public Sub New() InitializeComponent() End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'MsgBox(Application.StartupPath.ToString) Me.Text = My.Application.Info.AssemblyName.ToString LoadArguments() End Sub #End Region #Region "private methods" Private Function LoadArguments() As String() 'Throw New NotImplementedException mArgs = Environment.GetCommandLineArgs If mArgs.Length > 0 Then 'MsgBox(mArgs(0)) Me.Text = Me.Text & " " & mArgs(1) End If LoadArguments = mArgs End Function Private Function GetArguments() As String() 'Throw New NotImplementedException mArgs = Environment.GetCommandLineArgs If mArgs.Length > 0 Then 'Me.Text = Me.Text & " " & mArgs(1) End If GetArguments = mArgs End Function #End Region End Class |
Note that the argument is simply printed in the title of the form. Nothing special there. You could use this simple technique to pass argument from one program to another when you start a program.
What it is use for?
One example I could give you is AutoCAD�. When you start AutoCAD� from the command line or your desktop (shortcut), normally you have something like:
�C:\Program Files\Autodesk\AutoCAD2011\acad.exe�
But when you start AutoCAD�, you could choose a profile automatically when you launch it:
�C:\Program Files\Autodesk\AutoCAD 2011\acad.exe� /p MyProfile
Where MyProfile is the name of you profile in the AutoCAD�options under the profile tab.
If you like this post, leave a comment or share it.
References :
My web site : Check Technologies
My Visual Basic program : Visual Studio 2010 Professional (Old Version)
A newer version for Visual Basic : Microsoft Visual Studio Pro 2012
A newer version for Visual Basic : Microsoft Visual Studio Pro 2012
Download Sample Project : available soon...
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :