Question Getting argument from 2nd instance command line in single instance app

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
513
Programming Experience
3-5
How do i get the argument from and 2nd instance in a single instace app? I tried to catch it in MyApplication_StartupNextInstance. But the e.commandLine or the AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData seems to be null. I use "Appname.appref-ms" 123 to send 123 as the argument but how do i recieve it?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
I just tested and it worked exactly as expected for me. I created a project and made it single-instance. I then added this code:
VB.NET:
Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
    ' The following events are available for MyApplication:
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
            MessageBox.Show(String.Join(" ", e.CommandLine), "Startup")
        End Sub

        Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            MessageBox.Show(String.Join(" ", e.CommandLine), "StartupNextInstance")
        End Sub

    End Class
End Namespace
I then opened the Debug page of the project properties and entered "arg1 arg2" (without quotes) in the Command line arguments field. I then ran the project and the expected message popped up from the Startup event handler. I then right-clicked on the project in the Solution Explorer and selected Debug > Start New Instance and the expected message popped up from the StartupNextInstance event handler.
 

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
513
Programming Experience
3-5
When i do it the way u did, it works. Then it must mean that when i do it in the command prompt, it failed. "Appname.appref-ms" 123. That doesnt work. How do i pass argument from the command prompt?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
I'm not sure whether you can pass commandline arguments on to the application via an APPREF-MS file. If you create a LNK file, i.e. a standard shortcut, then you don't pass commandline arguments when you run that file and have them passed on to the application. The commandline arguments for the application are stored in the LNK file. I suspect that an APPREF-MS may be the same, although I'm not overly familiar with the file format.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
This seems relevant:

 
Top Bottom