- /*
- ============================================================================
- Name : Helloworld.cpp
- Author : huzhangyou
- Copyright : Your copyright notice
- Description : Exe source file
- ============================================================================
- */
- // Include Files
- #include "Helloworld.h"
- #include <e32base.h>
- #include <e32std.h>
- #include <e32cons.h> // Console
- //CConsoleBase类使用
- // Constants
- //定义常量
- _LIT(KTextConsoleTitle, "Console");
- _LIT(KTextFailed, " failed, leave code = %d");
- _LIT(KTextPressAnyKey, " [press any key]/n");
- // Global Variables
- //表示 LOCAL_C static
- //LOCAL_D static
- //定义静态CConsoleBase指针对象
- LOCAL_D CConsoleBase* console; // write all messages to this
- // Local Functions
- //静态方法 MainL 主调函数 L表示可能会抛出异常
- LOCAL_C void MainL()
- {
- //
- // add your program code here, example code below
- //
- console->Write(_L("Hello, world!/n"));
- }
- //主调用MainL函数
- LOCAL_C void DoStartL()
- {
- // Create active scheduler (to run active objects)
- /*
- class CActiveScheduler : public CBase;
- Controls the handling of asynchronous requests as represented by active objects.
- An active scheduler is used to schedule the sequence in which active object request completion events are handled by a single event-handling thread.
- An active scheduler can be instantiated and used directly if either:
- the RunL() function of all of its active objects is guaranteed not to leave, or
- each of its active objects implements a suitable RunError() function to provide suitable cleanup
- 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.
- There is one active scheduler per thread and the static functions provided by the class always refer to the current active scheduler.
- */
- CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
- /*
- How to use the cleanup stack
- The cleanup stack is used as follows:
- Use CleanupStack::PushL() to push a pointer to the object onto the cleanup stack before any operation which might leave is performed
- Use CleanupStack::Pop() to pop the pointer from the cleanup stack when all operations that might leave have completed
- 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.
- */
- CleanupStack::PushL(scheduler);
- /*
- static IMPORT_C void Install(CActiveScheduler *aScheduler);
- Description
- Installs the specified active scheduler as the current active scheduler.
- The installed active scheduler now handles events for this thread.
- The current active scheduler can be uninstalled by passing a NULL pointer.
- Parameters
- CActiveScheduler *aScheduler A pointer to the active scheduler to be installed. If this is NULL, the current active scheduler is uninstalled.
- Panic codes
- E32USER-CBase 43 if If there is already an installed active scheduler.
- */
- CActiveScheduler::Install(scheduler);
- //调用 主执行函数
- MainL();
- // Delete active scheduler
- /*
- Description
- Constructs and pushes a TCleanupItem object onto the cleanup stack.
- The TCleanupItem encapsulates:
- the pointer aPtr to the object of type class T which is to be cleaned up
- an associated cleanup operation.
- 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().
- 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.
- */
- CleanupStack::PopAndDestroy(scheduler);
- }
- // Global Functions
- //主函数入口点
- GLDEF_C TInt E32Main()
- {
- // Create cleanup stack
- /*
- Marks the start of checking the current thread's heap.
- This macro is defined only for debug builds.
- This macro must be matched by a corresponding call to __UHEAP_MARKEND or __UHEAP_MARKENDC.
- Calls to this macro can be nested but each call must be matched by corresponding call to __UHEAP_MARKEND or __UHEAP_MARKENDC.
- */
- __UHEAP_MARK;
- /*
- Cleanup stack interface.
- The creation and destruction of a cleanup stack is done automatically by GUI applications and servers.
- */
- CTrapCleanup* cleanup = CTrapCleanup::New();
- // Create output console
- TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
- if (createError)
- return createError;
- // Run application code inside TRAP harness, wait keypress when terminated
- TRAPD(mainError, DoStartL());
- //如果出错 输出错误信息
- if (mainError)
- console->Printf(KTextFailed, mainError);
- //输出press any key
- console->Printf(KTextPressAnyKey);
- //等待用户输入之后退出
- console->Getch();
- //释放资源
- delete console;
- delete cleanup;
- //清理堆栈
- __UHEAP_MARKEND;
- return KErrNone;
- }
Symbian Helloworld 分析
最新推荐文章于 2019-07-03 15:09:09 发布