最近参与项目,其中用到一些S60中的进程间通信功能,现总结部分使用方法:
1.通过一个命令行参数实现带参数启动应用程序:
//*启动代码, 在需要启动一个程序时使用*/
//[1]定义命令行参数
CApaCommandLine *cmd=CApaCommandLine::NewLC();
//[2]设置命令类型
cmd->SetCommandL(EApaCommandRun);
//[3]设置要启动的应用程序名
cmd->SetExecutableNameL(_L("HelloWorld.exe"));
//[4]设置参数,该参数为一个文件名称
cmd->SetDocumentNameL(_L("x.txt"));
//[5]设置参数,该参数为一个字符串
cmd->SetTailEndL(_L8("message tail"));
//[6]启动程序
RApaLsSession als;
User::LeaveIfError(als.Connect());
CleanupClosePushL(als);
User::LeaveIfError(als.StartApp(*cmd));
CleanupStack::PopAndDestroy(2);
//*在被启动的应用程序中接收参数*/
//该功能通过在AppUik中重载CEikAppUi:: ProcessCommandParametersL方法实现。
TBool CHello2AppUi::ProcessCommandParametersL(TApaCommand aCommand,TFileName& aDocumentName,const TDesC8& aTail)
{
//接收启动代码中第[4]行参数
TFileName file = aDocumentName;
//接收启动代码中第[2]行参数
TApaCommand com = aCommand;
//接收启动代码中第[5]行参数
HBufC8* str = aTail.AllocL();
delete str;
}
2.通过StartDocument实现带参数启动应用程序:
//*启动代码, 在需要启动一个程序时使用*/
RApaLsSession als;
TThreadId id;
TUid uid;
//被启动程序UID
uid.iUid = 0xE469529F;
als.Connect();
als.StartDocument(_L("hello2.txt"), uid, id);
als.Close();
被启动程序参数接收参考实例1中的参数接收。
3.通过RProcess实现带参数启动应用程序:
//*启动代码, 在需要启动一个程序时使用*/
RProcess* process = new RProcess();
TUidType uidtype(KNullUid);
process->Create(_L("//sys//bin//HelloWorld.exe"), _L("teststring"), uidtype);
process->Resume();
process->Close();
delete process;
//*在被启动的应用程序中接收参数*/
RBuf16 buf;
buf.Create(50);
User::CommandLine(buf); //buf=”teststring”
buf.Close();
4.通过TFindProcess实现查找一个启动的应用程序:
//要查找的进程名称
_LIT(KPROCESSNAME, "Hello2_0xE469529F*");
//构造进程搜索对象
TFindProcess findProcess(KPROCESSNAME);
TFullName processName;
//开始搜索应用程序,通过processName返回结果
findProcess.Next(processName);
if (processName == KNullDesC)
{
//查找的应用程序未启动
}
5.向一个启动的应用程序发送消息:
TUid uidApp = TUid::Uid(0xE469529F);
TApaTaskList taskList(CEikonEnv::Static()->WsSession());
TApaTask task = taskList.FindApp(uidApp);
TBuf<256> arg;
arg.Append(_L("my text"));
HBufC8* pBuf;
if(task.Exists())
{
//send message to the task; uid is not used, but it doesn's work.
pBuf = HBufC8::NewL(arg.Length());
TPtr8 ptr = pBuf->Des();
ptr.Copy(arg);
nErr = task.SendMessage(TUid::Uid( KUidApaMessageSwitchCreateFileValue ), *pBuf);
task.BringToForeground();
}
//*在被启动的应用程序中接收参数*/
//该功能通过在AppUik中重载CEikAppUi:: ProcessMessageL方法实现。
void CHello2AppUi::ProcessMessageL(TUid aUid, const TDesC8 &aParams)
{
TUid u = aUid;
}
以上是总结的进程间通信的方法。未完。。。
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/nie_feilong/archive/2010/01/18/5208892.aspx