Symbian Helloworld 分析

 
  1. /*  
  2.  ============================================================================  
  3.  Name    : Helloworld.cpp  
  4.  Author    : huzhangyou  
  5.  Copyright   : Your copyright notice  
  6.  Description : Exe source file  
  7.  ============================================================================  
  8.  */  
  9.   
  10. //  Include Files     
  11.   
  12. #include "Helloworld.h"   
  13. #include <e32base.h>   
  14. #include <e32std.h>   
  15. #include <e32cons.h>      // Console   
  16. //CConsoleBase类使用   
  17.   
  18. //  Constants   
  19. //定义常量   
  20. _LIT(KTextConsoleTitle, "Console");   
  21. _LIT(KTextFailed, " failed, leave code = %d");   
  22. _LIT(KTextPressAnyKey, " [press any key]/n");   
  23.   
  24.   
  25. //  Global Variables   
  26. //表示 LOCAL_C static   
  27. //LOCAL_D static   
  28. //定义静态CConsoleBase指针对象   
  29. LOCAL_D CConsoleBase* console; // write all messages to this   
  30.   
  31.   
  32. //  Local Functions   
  33. //静态方法 MainL 主调函数 L表示可能会抛出异常   
  34. LOCAL_C void MainL()   
  35.   {   
  36.   //   
  37.   // add your program code here, example code below   
  38.   //   
  39.   console->Write(_L("Hello, world!/n"));   
  40.   }   
  41. //主调用MainL函数   
  42. LOCAL_C void DoStartL()   
  43.   {   
  44.   // Create active scheduler (to run active objects)   
  45. /*  
  46. class CActiveScheduler : public CBase;  
  47. Controls the handling of asynchronous requests as represented by active objects.  
  48. An active scheduler is used to schedule the sequence in which active object request completion events are handled by a single event-handling thread.  
  49. An active scheduler can be instantiated and used directly if either:  
  50. the RunL() function of all of its active objects is guaranteed not to leave, or  
  51. each of its active objects implements a suitable RunError() function to provide suitable cleanup  
  52. If any of the active scheduler's active objects does not provide a RunError() function, then a CActiveScheduler derived class must be defined and an implementation of the Error() function provided to perform the cleanup required.  
  53. There is one active scheduler per thread and the static functions provided by the class always refer to the current active scheduler.  
  54. */  
  55.   CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();   
  56. /*  
  57. How to use the cleanup stack  
  58. The cleanup stack is used as follows:   
  59. Use CleanupStack::PushL() to push a pointer to the object onto the cleanup stack before any operation which might leave is performed   
  60. Use CleanupStack::Pop() to pop the pointer from the cleanup stack when all operations that might leave have completed   
  61. If a function leaves, then part of leave processing is to pop and destroy all objects on the cleanup stack. Thus, the cleanup stack may be used to prevent objects becoming orphaned if a leave occurs.   
  62. */  
  63.   CleanupStack::PushL(scheduler);   
  64. /*  
  65. static IMPORT_C void Install(CActiveScheduler *aScheduler);  
  66. Description  
  67. Installs the specified active scheduler as the current active scheduler.  
  68. The installed active scheduler now handles events for this thread.  
  69. The current active scheduler can be uninstalled by passing a NULL pointer.  
  70. Parameters  
  71. CActiveScheduler *aScheduler A pointer to the active scheduler to be installed. If this is NULL, the current active scheduler is uninstalled.  
  72.    
  73.    
  74. Panic codes  
  75. E32USER-CBase 43 if If there is already an installed active scheduler.  
  76. */  
  77.   CActiveScheduler::Install(scheduler);   
  78. //调用 主执行函数   
  79.   MainL();   
  80.   
  81.   // Delete active scheduler   
  82. /*  
  83. Description  
  84. Constructs and pushes a TCleanupItem object onto the cleanup stack.  
  85. The TCleanupItem encapsulates:  
  86. the pointer aPtr to the object of type class T which is to be cleaned up  
  87. an associated cleanup operation.  
  88. The cleanup operation is the private static function Delete() of the templated class CleanupDelete, and is called as a result of a subsequent call to CleanupStack::PopAndDestroy().  
  89. CleanupDelete::Delete() is passed a pointer to the class T object to be cleaned up, and the function implements cleanup by deleting the passed object.  
  90. */  
  91.   CleanupStack::PopAndDestroy(scheduler);   
  92.   }   
  93.   
  94. //  Global Functions   
  95. //主函数入口点   
  96. GLDEF_C TInt E32Main()   
  97.   {   
  98.   // Create cleanup stack   
  99. /*  
  100. Marks the start of checking the current thread's heap.  
  101. This macro is defined only for debug builds.  
  102. This macro must be matched by a corresponding call to __UHEAP_MARKEND or __UHEAP_MARKENDC.  
  103. Calls to this macro can be nested but each call must be matched by corresponding call to __UHEAP_MARKEND or __UHEAP_MARKENDC.  
  104. */  
  105.   __UHEAP_MARK;   
  106. /*  
  107. Cleanup stack interface.  
  108. The creation and destruction of a cleanup stack is done automatically by GUI applications and servers.   
  109. */  
  110.   CTrapCleanup* cleanup = CTrapCleanup::New();   
  111.   
  112.   // Create output console   
  113.   TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));   
  114.   if (createError)   
  115.   return createError;   
  116.   
  117.   // Run application code inside TRAP harness, wait keypress when terminated   
  118.   TRAPD(mainError, DoStartL());   
  119. //如果出错 输出错误信息   
  120.   if (mainError)   
  121.   console->Printf(KTextFailed, mainError);   
  122. //输出press any key   
  123.   console->Printf(KTextPressAnyKey);   
  124. //等待用户输入之后退出   
  125.   console->Getch();   
  126. //释放资源   
  127.   delete console;   
  128.   delete cleanup;   
  129. //清理堆栈   
  130.   __UHEAP_MARKEND;   
  131.   return KErrNone;   
  132.   }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值