Using Command Line Arguments in Symbian

in

Most Symbian programs are launched through a graphical shell rather than through a command-line. The common exceptions are .exes on the PC platform, which can be run from a Windows command prompt.

For .exe programs, control is received through the E32Main() function, which, however does not supply arguments. Instead, access to arguments is provided by CCommandLineArguments. Conventional C and C++ programs receive their arguments from main(int argc, char* argv[]).

For a program that is launched as a new process, the arguments are available as part of the process command line and may be obtained in full using code such as

RProcess me;
TPtrC program=me.FileName();
TPtrC args=me.CommandLine(); // User::CommandLine from Symbian 9.1 onwards

However, the arguments are in a raw form. For convenience, the CCommandLineArguments class provides functions to access the program name as argument 0, and each command-line argument as argument 1, 2 etc.

Arguments beginning with a quote may contain blanks and doubled quotes. Arguments not beginning with a quote are terminated by a blank.

void ReadAndWriteArgsL()
{
 console=Console::NewL(_L("Command-line arguments reader test"), TSize(KConsFullScreen, KConsFullScreen));
 CleanupStack::PushL( console );

 //Create CommandLine Arguments and read it.
 CCommandLineArguments* args=CCommandLineArguments::NewLC();
 for (TInt i=0; i<args->Count(); i++)
 {
   TPtrC argumentPrt(args->Arg(i));
   console->Printf(_L("Arg %d == %S\n"), i, &argumentPrt);
 }

 CleanupStack::PopAndDestroy(2);
}


GLDEF_C TInt E32Main()
{
 __UHEAP_MARK; // mark heap state
 CTrapCleanup* cleanup=CTrapCleanup::New();
 TRAPD(error, ReadAndWriteArgsL());
 __ASSERT_ALWAYS(!error, User::Invariant());
 delete cleanup;
 __UHEAP_MARKEND;
 return 0;
}

If Application Name is TestCmdArgs.exe, launch it as TestCmdArgs.exe Nokia India Pvt Ltd from command prompt And the output will be

Arg 0 == TestCmdArgs.exe

Arg 1 == Nokia

Arg 2 == India

Arg 3 == Pvt

Arg 4 == Ltd

Girish

AttachmentSize
Example.txt714 bytes

> Using Command Line Arguments in Symbian

Great article, thank you!