NetRun with command line parameters?

NetRun with command line parameters?

Old forum URL: forums.lhotka.net/forums/t/1581.aspx


ScottHutchinson posted on Tuesday, October 24, 2006

We use NetRun on .NET 1.1. We would like to be able to pass command line parameters to our application when launching it via NetRun. For instance, we want to pass a username and password from another application, so the user would not need to login again. However, as soon as we add ByVal Args() As String to Sub Main, we can no longer launch the application with NetRun, because it throws a System.Reflection.TargetParameterCountException. I have tried to fix this by modifying NetRun, but could not find a solution using either LoadFrom/Invoke or ExecuteAssembly.

Has anybody been able to do this?

Thanks!

Brian Criswell replied on Tuesday, October 24, 2006

I did this a while back.  I am pretty sure you have to alter NetRun to pass its arguments to the application as an object or string array.

ScottHutchinson replied on Wednesday, November 15, 2006

I figured out how to do it. See example code snippets below. The important changes are highlighted (but for someone reason you might not be able to see the highlights).

Public Class Launcher

Public Sub RunApp(ByVal AppURL As String, ByVal params() As String)

' before we do anything, invoke the workaround

' for the serialization bug

SerializationWorkaround()

Try

' get and parse the URL for the app we are

' launching

mAppURL = AppURL

mAppDir = GetAppDirectory(mAppURL)

mAppName = GetAppName(mAppURL)

'Pass an empty instance of the Web Proxy

System.Net.GlobalProxySelection.Select = System.Net.GlobalProxySelection.GetEmptyWebProxy

SetSecurity()

' load the assembly into our AppDomain

Dim asm As [Assembly] = [Assembly].LoadFrom(AppURL)

Dim objArray(0) As Object

' run the program by invoking its entry point

With asm.EntryPoint

If .GetParameters().Length = 0 Then

' Set objArray = Nothing to maintain compatibility with applications

' that do not receive parameters (i.e., Sub Main() has no parameters defined).

objArray = Nothing

Else

objArray(0) = params

End If

.Invoke(asm.EntryPoint, objArray)

End With 'asm.EntryPoint

Finally

RemoveSecurity()

End Try

End Sub

 

Module Main

Public Sub Main(ByVal Args() As String)

Try

' launch the app based on the URL provided by the user

RunApplication(Args)

Private Sub RunApplication(ByVal Args() As String)

' The first command line argument (the application url) is always required.

Dim AppURL As String = Args(0)

' Depending on the particular application, there might also be additional

' command line arguments.

Dim params() As String

If Args.Length > 1 Then

ReDim params(Args.GetUpperBound(0) - 1)

' Put the other command line arguments in their own array.

Array.Copy(Args, 1, params, 0, params.Length)

End If

' create setup object for the new app domain

Dim setupDomain As New AppDomainSetup

With setupDomain

' give it a valid base path

.ApplicationBase = CurrentDomainPath()

' give it a config file name

.ConfigurationFile = AppURL & ".config"

End With 'setupDomain

' create new application domain

Dim newDomain As AppDomain = AppDomain.CreateDomain(GetAppName(AppURL), Nothing, setupDomain)

' create launcher object in new appdomain

Dim launcher As Launcher = CType(newDomain.CreateInstanceAndUnwrap("NetRun", "NetRun.Launcher"), Launcher)

' use launcher object from the new domain

' to launch the remote app in that appdomain

launcher.RunApp(AppURL, params)

End Sub

 

Copyright (c) Marimer LLC