×
Namespaces

Variants
Actions

Archived:Sharing file handles between processes using Symbian C++

From Nokia Developer 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
Compatibility
Platform(s): S60 3rd Edition
S60 3rd Edition (initial release)
Article
Created: User:Technical writer 2 (25 Jan 2007)
Last edited: hamishwillee (14 Jun 2012)

Overview

Sharing file handles between processes

Description

It is a common requirement to share file handles and file sessions between different processes. This can be done either in a Parent – Child or a Client – Server relationship.

Solution

Parent – Child Relationship:
File handles are shared between parent and child using the RFile::TransferToProcess and RFile::AdoptFromCreator methods. The TransferToProcess() methods are used by the owner of an open RFile object to transfer ownership to its child process. Note that the file-server session must be marked as shareable by calling RFs::ShareProtected() before any file handles are transferred – otherwise RFile::TransferToProcess will return KErrBadHandle. Similarly the various AdoptFromCreator() methods are used by the receiving process to adopt the RFile object. For example:
// In the parent process:
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
User::LeaveIfError(fs.ShareProtected());
RFile file;
User::LeaveIfError(file.Replace(fs, KFile, EFileWrite | EFileShareAny));
CleanupClosePushL(file);
// Create test process
RProcess p;
User::LeaveIfError(p.Create(KChildProcess,  KNullDesC));
CleanupClosePushL(p);
// Transfer to process storing the RFs handle into environment  slot 1 and the RFile handle into slot 2
// NB slot 0 is reserved for the command line
User::LeaveIfError(file.TransferToProcess(p, 1, 2));
// Wait for handle to be transferred; wrap in an active object if blocking this thread is undesirable
TRequestStatus transStatus;
p.Rendezvous(transStatus);
if(transStatus != KRequestPending)
               { // Process creation failed
               p.RendezvousCancel(transStatus);
               p.Kill(0);
               User::Leave(transStatus.Int());
               }
// Start the process
p.Resume();
User::WaitForRequest(transStatus);
User::LeaveIfError(transStatus.Int());

        // Now we can safely close the fs
CleanupStack::PopAndDestroy(3); // close p, file, and fs
// In the child process:
RFile file;
// Adopt the file using the RFs handle into environment  slot 1 and the RFile handle into slot 2
User::LeaveIfError(file.AdoptFromCreator(1,2));
CleanupClosePushL(file);
RProcess::Rendezvous(KErrNone); // Signal transfer completed successfully

        // . . . Do the file operations

        CleanupStack::PopAndDestroy(); // close file
Client - Server relationship

File handle can be shared between Client and Server similar to Parent and Child using  RFile::TransferXXX and RFile::AdoptXXX APIs. The TransferXXX() methods are used by the owner of an open RFile object to transfer ownership to another process. Note that the file-server session must be marked as shareable by calling RFs::ShareProtected() before any file handles are transferred – otherwise RFile::TransferXXX will return KErrBadHandle. Similarly the various AdoptXXX methods are used by the receiving process to adopt the RFile object.
If the handle is transferred from the server to the client, then the server uses the RFile::TransferToClient() method and the client uses RFile::AdoptFromServer(). To pass a handle in the other direction, the client uses RFile::TransferToServer(), and the server uses RFile::AdoptFromClient() to take ownership of the file handle.

This page was last modified on 14 June 2012, at 10:20.
62 page views in the last 30 days.