EXE and parameters ?
I can not find anywhere, books or internet, an answer to this ?
I want to create a exe that takes in 1 parameter, a string value.
How do you declare this in a VB.net app ?
Please help, its hair pulling time here.
Thanks
Deasun
Hi,
If you are using VB 2005, there is a very easy way to pass in command line arguments to the .EXE. Here is some code that lives in the MyProject code behind file for the Card Game Starter Kit (this sample ships in VB 2005!):
Class MyApplication #If _MyType = "WindowsForms" Then End Sub |
If you are using VB 2002 or 2003, you will write similar code in a custom Sub Main. Define a Sub Main in a Startup.VB module, and choose this Sub main as your startup object in the project properties.
Here is a code sample from vb-helper.com http://www.vb-helper.com/howto_net_list_command_line_arguments.html>:
Module StartupModule frm.Text = UBound(CmdArgs) + 1 & " arguments" Return 0 |
Best,
Paul Yuknewicz
Visual Basic
Here is an easy way: This little program recieves info on the command line (if info is being passed to your app) and puts it in a messagebox. It also has a button which launches test.exe and passes it "VariableToBePassed"
Hope this helps!
Imports
System.DiagnosticsPrivate
Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim cmd As String = Microsoft.VisualBasic.Command If cmd IsNot String.Empty ThenMessageBox.Show(cmd)
Me.Close() End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickProcess.Start(
"C:\Program Files\Applications\test.exe", "VariableToBePassed") Me.Close() End Sub
I have a related question at
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=600652&SiteID=1
how do you make an application the only instance that can be running and when you double click a file that's assiciated with the application it opens in the already running instance?
Windows Media Player does this with songs, you can only have one instance of media player running but when you click a song it strikes something in the already running instance and it plays?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cmd As String = Microsoft.VisualBasic.CommandTextBox2.Text = cmd 'puts the first parameter used to start the single instance program, but when i double click again the cmd is the same.
End SubI tried Microsoft.VisualBasic.Command in Me.Load, it works the first time but after you make the program a single instance application by clicking on myproject in solution explorer it doesn't change the command line argument the second time you click the shortcut like testprogram.exe parameter. Anybody know how to clear it or something? It's still only seeing the first parameter i put in, the doesnt change when i run the single instance application for the second time.
Thanks
Hi John,
To make your application a single instance application simply right click on your project in Solution Explorer and click the checkbox "Make Single Instance Application".
Now to addresss your second question. In order to send command-line arguments to an already open application, that, you will have to use the Win32 API.
If you send me your code or email me, I can help you with this.
John
the_grove_man@yahoo.com
Once you use the 2005 framework, passing the parameter is easy. In your ApplicationEvents, add a handler for the StartupNextInstance event. The below code takes the CommanLineArgs from the next instance, wraps them up in the same format as the original args and passes it on to the main form for processing.
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance If TypeOf Me.MainForm Is Form1 Then Dim Args(e.CommandLine.Count) As Stringe.CommandLine.CopyTo(Args, 0)
DirectCast(Me.MainForm, Form1).showArgs(Args) End If End SubThere is a working demo and write-up at http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx
I hope this helps.
Jim Wooley
http://devauthority.com/blogs/jwooley