EXE and parameters ?

Good morning,
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

[274 byte] By [Deasun] at [2007-12-16]
# 1
NM found the answer to this first part of my problem.
For those interested heres what I cam up with:
Imports Microsoft.VisualBasic
and in the load event use: Dim strTestlink As String = Command()
Thanks
Deasun at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

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
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
If e.CommandLine.Count > 0 Then
' Get the 2 character command line argument
Dim arg As String = e.CommandLine(0).ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim().Substring(0, 2)
Select Case arg
Case "/c"
' Show the options dialog
Me.MainForm = My.Forms.OptionsForm
Case "/p"
' Don't do anything for preview
Case "/s"
Me.MainForm = My.Forms.ScreenSaverForm
Case Else
MessageBox.Show("Invalid command line argument :" + arg, "Invalid Command Line Argument", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
Else
' If no arguments were passed in, show the screensaver
Me.MainForm = My.Forms.ScreenSaverForm
End If

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
Public Function Main(ByVal CmdArgs() As String) As _
Integer
Dim frm As New Form1()
Dim i As Integer

frm.Text = UBound(CmdArgs) + 1 & " arguments"
For i = 0 To UBound(CmdArgs)
frm.lstCommands.Items.Add(CmdArgs(i))
Next i
frm.ShowDialog()

Return 0
End Function
End Module



Hope this helps.

Best,

Paul Yuknewicz
Visual Basic

PaulYuk_MS at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

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.Diagnostics

Private 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 Then

MessageBox.Show(cmd)

Me.Close()

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Process.Start("C:\Program Files\Applications\test.exe", "VariableToBePassed")

Me.Close()

End Sub

JGrove805 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

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.Command

TextBox2.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 Sub

I 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

JohnX.C. at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

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

JGrove805 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

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 String

e.CommandLine.CopyTo(Args, 0)

DirectCast(Me.MainForm, Form1).showArgs(Args)

End If

End Sub

There 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

jwooley at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...