Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Archived:Launching a new file in an existing app instance

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Created: cdavies (02 Jan 2009)
Last edited: hamishwillee (19 Jun 2012)

There are certain types of application that are designed to process a number of files, either in series or parallel, where one wishes to open one file and then open a second file in the same instance of the application without interrupting the task it is currently performing. This turns out to be strangely non-trivial.

The Nokia knowledge base offers Archived:Data handler applications must implement CAknAppUi::OpenFileL (Known Issue) on how to implement data handling applications that need to switch files. I don't regard this as terribly useful. In some instances it may be OK to terminate the old instance of the app, and start a fresh instance with the new document. However, other times you may wish to start the new document in a new tab in the same application, etc. etc.

Some problems

  1. In the platsec world, files are generally available only by handle. This is because a file may be in a private location inaccessible to the handling process. You cannot work backwards from a handle to a file name.
  2. In the platsec world, command line arguments are passed in environment slots. Once you read the data in an environment slot, it is gone for good.
  3. Because of the above, only the first call to CApaCommandLineArguments::GetCommandLineFromProcessEnvironment is ever going to return any meaningful results in any given process.
  4. The application framework call EikStart::RunApplication parses the command line before you ever get a chance to look at it.
  5. It isn't (seemingly) possible to get the command line arguments before the application's CEikAppUi derived class is instantiated.
  6. And finally, to close the circle, if an existing application instance is already running CEikApplication::PreDocConstructL will raise that instance of the app and exit the process.

These problems aren't insurmountable, we just have to bypass the standard application startup process if an instance of this application already exists.

Notifying an existing application instance of a new file

The following code will check if the application is already running on startup, raise it if it is and get the command line parameters for the current process:

const TUid KMyAppUid = { 0xDEADBEEF };
TInt IsAlreadyRunning(TBool& aRunning)
aRunning = EFalse;
RWsSession ws;
TInt err = ws.Connect();
if (KErrNone == err)
TInt wgId = 0;
TUid uid(KMyAppUid);
CApaWindowGroupName::FindByAppUid(uid, ws, wgId);
if (wgId >= 0)
/* task exists, raise it to the front and notify that a running instance exists */
aRunning = ETrue;
TApaTask task(ws);
return err;
TInt NotifyRunningInstanceOfNewFile()
CApaCommandLine* cmdl = NULL;
TInt err = CApaCommandLine::GetCommandLineFromProcessEnvironment(cmdl);
if (KErrNone == err)
/* do something useful with the file passed to us in the parameters */
delete cmdl;
return err;
TInt E32Main()
TBool running = EFalse;
TInt err = IsAlreadyRunning(running);
if (KErrNone == err)
if (!running)
err = EikStart::RunApplication(NewApplication);
err = NotifyRunningInstanceOfNewFile();
return err;

Things you can potentially do to notify the existing process include things as sophisticated using a semaphore (RSemaphore) and RFile::TransferToProcess, or as simple minded as copying the file to the process' private directory and using RFs::Notify to detect new files.

This page was last modified on 19 June 2012, at 08:11.
28 page views in the last 30 days.