Archived:Sharing file handles between processes using Symbian C++
Article Metadata
Compatibility
Article
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.


(no comments yet)