3422 Old Capitol Trail – Suite 1125, Wilmington – DE 19808, United States

Passing command line arguments to VirtualUI apps

VirtualUI command-line argumentsAn application can accept command line arguments when invoked. This allows the user to send additional information to the application when it is executed.

When an application is launched, the OS passes the command line arguments to the application as a collection of string values, using a white-space as separator.

Applications receive external arguments on a regular basis: when you open a document by clicking on its icon, the OS selects the associated executable program and calls it, sending the full document filename to the program as an argument.

Thinfinity VirtualUI allows you to send external arguments to applications in a transparent way, which works exactly like sending arguments from the command line.

 

Setting command line arguments in the application profile

Thinfinity VirtualUI enables external argument definition from the VirtualUI Server Manager. When you create an application profile, you can specify the execution arguments in the General tab panel of the Application Profiles editor.

settingcommandlinearguments

These arguments will be received by the application as a list of string values, using the white-space character as argument delimiter.

application profile arguments

In Delphi you can get command line arguments using the System.ParamCount and System.ParamStr methods. ParamCount returns the number of arguments, and ParamStr(index) returns each argument, with ParamStr(0) always being the path and name of the invoked program. Here is a way you can show the received arguments list:

  Writeln(Format(‘Arguments = %d’, [ParamCount]))
  for p := 1 to ParamCount do
    Writeln(Format(‘argument %d = “%s”’, [p, ParamStr(p)]);

 

In C++, both the argument counter and the argument list will be received in the two main() function arguments. Like in Delphi, the first argument in the string array is the path and name of the program itself:

void main(int argCounter, char* arguments[]) {
  cout << "Arguments = " << argCounter << endl;
  for(int i = 1; i < argCounter; i++)
  cout << "arguments[" << i << "] = " << arguments[i] << endl;
}

You can also write the main function header in this way:

void main(int argCounter, char** arguments) { … }

 

Unlike Delphi and C++, C# excludes the name and path of the program from the arguments collection. Also, C# offers two ways to get the arguments, by using a traditional for…

public static void Main(string[] arguments)
{
    Console.WriteLine("Arguments = {0}", args.Length);
    for(int i = 0; i < arguments.Length; i++)
    {
        Console.WriteLine("Arguments[{0}] = [{1}]", i, arguments[i]);
    }
}

or by using the foreach:

foreach(string s in arguments)
{
    Console.WriteLine(s);
}

 

Sending command line arguments to the application in the VirtualUI url

VirtualUI allows you to send external arguments in the url. Instead of passing static arguments from the application profile definition, using this feature you can send dynamic application arguments in a simple way. The only requirement to be met is that the arguments must be url encoded.

 

To build the url you need:

  • The communication protocol (http or https).
  • The Thinfinity VirtualUI server domain or IP address, and port.
  • The application virtual path.

 

Build the url like this:

protocol://server.address:port/virtualpath/?arguments

As an example, we will send the “one”, “two” and “three” argument values to the TestArguments program:

http://192.168.0.229:6580/TestArguments/?one%20two%20three

The following image shows the submit result:

URL passed arguments

 

Building the URL in Javascript

When necessary, modern browsers encode URLs automatically, but it is convenient to do it by code. The Javascript lines below show how to build the URL used in the previous example:

var buildURL = function(server, virtualpath, arguments) {
  return server + “/” + virtualpath + “/?” + encodeURIComponent(arguments.join(‘ ‘));
}
var baseURL = “http://192.168.0.229:6580/”;
var applicationPath = “TestArguments”;
var arguments = [“one”, “two”, “tree”];
var url = buildURL(baseURL, applicationPath, arguments);

 

Combining both application profile and url command line arguments

If you have defined command line arguments in the application profile and need to send new arguments to the application by url, don’t worry. Thinfinity VirtualUI merges both argument lists, first adding the application profile arguments, and then the arguments that were passed in the url.

merged command-line arguments

Related Posts

Leave a comment