03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

marking a function GLDEF_C, you show that you have thought about it being exported from<br />

the object module. E32Main() returns an integer – a TInt. You could have written int<br />

instead of TInt, but since C++ compilers don't guarantee that int is a 32-bit signed integer,<br />

Symbian OS uses typedefs for standard types to make sure they are guaranteed to be the<br />

same across all Symbian OS implementations and compilers.<br />

E32Main() sets up an error-handling framework. It sets up a cleanup stack and then calls<br />

ConsoleMainL() under a trap harness. The trap harness catches errors – more precisely,<br />

it catches any functions that leave. If you're familiar with the exception handling in standard<br />

C++ or Java, TRAP() is like try and catch all in one, User::Leave() is like throw, and a<br />

function with L at the end of its name is like a function with throws in its prototype. It's very<br />

important to know what these functions are: the Symbian OS convention is to give them a<br />

name ending in L().<br />

Here's ConsoleMainL():<br />

// Console harness<br />

void ConsoleMainL()<br />

{<br />

// Get a console<br />

gConsole = Console::NewL(_L("Hello Text"),<br />

CleanupStack::PushL(gConsole);<br />

// Call function<br />

MainL();<br />

// Pause before terminating<br />

User::After(5000000); // 5 second delay<br />

// Finished with console<br />

CleanupStack::PopAndDestroy(gConsole);<br />

}<br />

TSize(KConsFullScreen, KConsFullScreen));<br />

This function allocates a console before calling MainL() to do the Printf() of the 'Hello<br />

world!' message. After that, it briefly pauses and then deletes the console again. If we were<br />

creating this example for a target machine that had a keyboard, we could have replaced the<br />

delay code:<br />

// Pause before terminating<br />

User::After(5000000); // 5 second delay<br />

with something like:<br />

// Wait for key<br />

console->Printf(_L("[ press any key ]"));<br />

console->Getch(); // Get and ignore character<br />

so that the application would wait for a keypress before terminating.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!