×
Namespaces

Variants
Actions

在两个进程间共享文件句柄

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
huwell 在 28 Dec 2007 创建
最后由 hamishwillee 在 15 Dec 2011 编辑
  • 开发伙伴平台:

S60 3rd Edition


  • 详细描述

我们经常需要在不同的进程间共享文件句柄和文件session。它们即可以是Parent-Child关系,也可以是Client-Server关系。



  • 解决方案

Parent-Child关系,我们可以通过使用RFile::TransferToProcess()和RFile::AdoptFromCreator方法在parent和child之间共享文件句柄。打开RFile对象的句柄拥有者可以使用TransferToProcess()方法向child进程传递句柄。注意如果使用RFs::ShareProtected()方法,则在任何文件句柄传递前,file-server session必须标记为共享。否则RFile::TransferToProcess将会返回KErrBadHandle。


// 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


如果从server向client传递句柄。server可以使用RFile::TransferToClient方法,而client可以使用RFile::AdoptFormServer()方法接受。要传递句柄到其他地方,则client要使用RFile::TransferToServer(),而server要使用RFile::AdoptFromClient来移交文件句柄所有权。

This page was last modified on 15 December 2011, at 09:00.
66 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×