Click here to Skip to main content
11,921,746 members (68,292 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked

An Introduction to Palm Handheld Development

, 5 Nov 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
Your first Palm application using the venerable "Hello World!" example.

Sample Image - palmintro_helloworld.jpg


There have been several people asking Palm handheld questions lately in the various CP forums and, although Code Project is mainly a Windows programming site, I think it would benefit from a few Palm articles. So, without further adue, here's a little something to wet your appetite.

Palm Development Resources

The demo application and source code were created using Metrowerk's CodeWarrior for PalmOS IDE (version 6). The resources were made using Contstructor for PalmOS. I believe there is an evaluation version you can download from Metrowerks. Also, you can download the Palm Emulator from Palm and find most development information there as well.

If you want completely free dev tools, there is a GNU compiler for the Palm as well. The demo application was not designed using the GNU compiler so I don't know if it will work or not.

When you write a production Palm application, you will need to sign-up for a unique 4 digit ID number that will differentiate your application resources from any others (kind of like a GUID, but not as complex).

Palm Application Terminology

All OSes have their own terminology and the PalmOS is no exception. The following is a list of some of the more relevant terms used in PalmOS programming: 1


A rectangular area in which things like dialogs, forms, and menus are drawn by the application.
An application window that covers the entire screen. A form may also contain controls, text areas, and menus. Only one active form is allowed at a time.
A collection of persistent memory chunks. There are resource and record databases.
A piece of data stored in a resource database. Identified by a resource type and number.
A data structure identified by a unique record ID. Application data is typically stored in a record database.
Launch Code
A parameter passed to an application that specifies what the application should do when executed.
A warning or information dialog that needs to be dismissed by the user.

Like Microsoft Windows, the PalmOS is an event-driven operating system. Only one application can be open at a time and all applications contain an event loop which retrieves events and acts upon them.

Palm Application Structure

The entry point to every palm application is the PilotMain function.
UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags) 2

The first parameter, cmd, is the launch code for the application. There are several available launch codes, but to start an application normally, use the constant sysAppLaunchCmdNormalLaunch.

The second parameter, cmdPBP, is a pointer to a structure containing any launch-command-specific parameters, or NULL if the launch code has none. A MemPtr is simply a void *.

The final parameter, launchFlags, indicates whether your application's global variables are available, whether your application is now the active application, whether it already was the active application, and so on.

/* Here is an example PilotMain */
UInt32 PilotMain( UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
	switch (cmd)
		case sysAppLaunchCmdNormalLaunch:
	return 0;

The PiltoMain function calls the main event loop function. In this function, we continually process events for our application and the system (See Figure 1 for an illustration). We first get an event using the EvtGetEvent function, and then dispatch that event to 4 event handlers. If an event handler returns true, the event was handled and will not process further. If it is not handled, it is sent to each successive handler for processing. The loop continues until the appStopEvent is received.
/* Here is an example main event loop */
static void AppEventLoop(void)
	UInt16 error;
	EventType event;

		EvtGetEvent(&event, evtWaitForever);

		if (! SysHandleEvent(&event))
			if (! MenuHandleEvent(0, &event, &error))
				if (! ApplicationHandleEvent(&event))

	} while (event.eType != appStopEvent);
SysHandleEvent and MenuHandleEvent handle Palm system events and menu events respectively, but the ApplicationHandleEvent and FrmDispatchEvent are where most of the real action is handled in a Palm application.

The ApplicationHandleEvent function is where you set the event handlers for all of your forms using void FrmSetEventHandler (FormType *formP, FormEventHandlerType *handler). A form event handler routine is of the form: Boolean FormEventHandlerType (EventType *eventP). You are basically setting a callback function up as an event handler (just like Window's WindowProcedure).

/* Here is an example application event handler */

// a form event handler
static Boolean MainFormHandleEvent(EventPtr eventP)
   	Boolean handled = false;
   	FormPtr frmP = FrmGetActiveForm();
	switch (eventP->eType) 
		case frmOpenEvent:
			handled = true;
	return handled;
// the app event handler
static Boolean AppHandleEvent(EventPtr eventP)
	UInt16 formId;
	FormPtr frmP;
	Boolean bRetVal = false;

	if (eventP->eType == frmLoadEvent)  // is a form loading?
		// Load the form resource.
		formId = eventP->data.frmLoad.formID;
		frmP = FrmInitForm(formId);  // load a form resource
		FrmSetActiveForm(frmP);  // make this the active form

		// Set the event handler for the form.  The handler of the currently
		// active form is called by FrmHandleEvent each time is receives an
		// event.
		switch (formId)
			case MainForm:
				FrmSetEventHandler(frmP, MainFormHandleEvent);
		bRetVal = true;
	return bRetVal;

The FrmDispatchEvent routine provides indirect form-specific event handling. To provide this, FrmDispatchEvent calls the form's event handler (MainFormHandleEvent in the example above).

A "Hello World!" Example

As an example on how to write your first Palm application, I'll use the never tiresome "Hello World!" example. I will open a form with a button which, when pressed, will write "Hello World!" to the center of the screen.

The demo source code's PilotMain and main event loop are almost identical to what we covered in the Palm Application Structure section with the exception of the FrmGotoForm function call. void FrmGotoForm (UInt16 formId) sends a close event to the currently active form and load and open events to the form specified by fromId. The application event handler will then set the form event handler for this form given the formId. So in summary, FrmGotoForm closes the active form and opens a new one.

When the "Say Hello" button is pressed on the form, a ctlSelectEvent event is sent to the form which is then passed to the form button handler routine, MainFormButtonHandler.

static Boolean MainFormButtonHandler(FormPtr formP, EventPtr eventP)
	Boolean handled = false;
	switch (eventP->data.ctlEnter.controlID)
		case MainSayHelloButton:
			handled = true;

	return handled;
The above switch statement finds the control that was selected and calls my SayHello function (below).
static void SayHello()
	short nCharWidth = 0;
	short width = 0, height = 0;
	Char* pText = (Char*) MemPtrNew(13);  // allocate 13 bytes
	// copy Hello World! into the char ptr
	StrCopy(pText, "Hello World!");
	// get the width of the string		
	nCharWidth = FntCharsWidth(pText, StrLen(pText));  
	// get the width and height of the string
	WinGetWindowExtent(&width, &height);
	// draw the text in the center
	WinDrawChars(pText, StrLen(pText), (width/2) - (nCharWidth/2), height/2);

	// free the text memory
Most Palm code is similar to standard C/C++ but string and memory functions are optimized for the platform. Using the standard C/C++ functions may increase the size of your compiled executable and on the Palm, size does matter.

To begin with, we allocate 13 bytes for our "Hello World!" string using the MemPtrNew function which acts just like malloc.

We then use Palm's StrCopy function in place of the standard strcpy.

FntCharsWidth returns the width of a string in pixels using the current font.

WinGetWindowExtent receives the width and height of the Palm screen area. You should not assume that the Palm screen is 160x160 anymore (the new Palm Tungsten T has a 320x320 screen area).

After getting the screen size and string size, we finally draw the characters to the center of the screen using void WinDrawChars (const Char *chars, Int16 len, Coord x, Coord y).

We then free the memory allocated for the string using MemPtrFree.

After saying hello, the demo app will just sit there waiting for your input until you close the aplication using the standard Home button on the Palm.


I hope you found this article to be understandable and interesting. The Palm platform is certainly here to stay and fun to develop for so I encourage you to give it a try.

Let me know if you are interested in more articles, if so they may include information on how to create and use a palm database, plus palm "conduit" development.

Also, I have not included the PalmOS.h file with the demo project because it is readily available with the Palm SDK which can be downloaded from the PalmOS website.


1 Rhodes, McKeehan: "Palm Programming The Developer's Guide, First Edition", 67-69.
2 Palm Inc., System Manager {online} Available, 2001.

Helpful Links:
Palm OS® Programmer's API Reference


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jason Henderson
United States United States
I have been a professional developer since 1996. I live in the middle of no where in Illinois, USA. I am married and have four children.

You may also be interested in...

Comments and Discussions

GeneralPalm & Indic Scripts Pin
AKG15-Mar-06 22:58
memberAKG15-Mar-06 22:58 
Generalproblem in creating records Pin
ankitapatel30-Jan-06 21:03
memberankitapatel30-Jan-06 21:03 
Generalpocket pc application development Pin
mellvincent3-Jan-06 4:55
membermellvincent3-Jan-06 4:55 
GeneralI need help Pin
nailesh29-Aug-05 3:49
membernailesh29-Aug-05 3:49 
QuestionC++ or CodeWarior? Pin
Andrei Davydenko20-Jul-05 15:26
memberAndrei Davydenko20-Jul-05 15:26 
AnswerRe: C++ or CodeWarior? Pin
Jason Henderson21-Jul-05 4:01
memberJason Henderson21-Jul-05 4:01 
AnswerRe: C++ or CodeWarior? Pin
Anonymous7-Oct-05 2:04
sussAnonymous7-Oct-05 2:04 
Generalpda application development Pin
vaibhav377-Apr-05 23:49
membervaibhav377-Apr-05 23:49 
Generalusb communication with palm os Pin
vaibhavagarwal1@gmail.com26-Mar-05 6:58
membervaibhavagarwal1@gmail.com26-Mar-05 6:58 
GeneralCan't download "CodeWarrior for PalmOS " Pin
tuan1798330-Jul-04 6:50
membertuan1798330-Jul-04 6:50 
GeneralRe: Can't download "CodeWarrior for PalmOS " Pin
Jason Henderson30-Jul-04 7:11
memberJason Henderson30-Jul-04 7:11 
GeneralRe: Can't download "CodeWarrior for PalmOS " Pin
tuan1798330-Jul-04 8:24
membertuan1798330-Jul-04 8:24 
GeneralRe: Can't download "CodeWarrior for PalmOS " Pin
Jason Henderson31-Jul-04 9:55
memberJason Henderson31-Jul-04 9:55 
GeneralPalm OR PocketPC Pin
bfadi27-Sep-03 4:47
memberbfadi27-Sep-03 4:47 
GeneralRe: Palm OR PocketPC Pin
João Paulo Figueira27-Sep-03 5:56
memberJoão Paulo Figueira27-Sep-03 5:56 
GeneralRe: Palm OR PocketPC Pin
bfadi27-Sep-03 23:48
memberbfadi27-Sep-03 23:48 
GeneralRe: Palm OR PocketPC Pin
João Paulo Figueira28-Sep-03 0:29
memberJoão Paulo Figueira28-Sep-03 0:29 
GeneralRe: Palm OR PocketPC Pin
bfadi28-Sep-03 0:41
memberbfadi28-Sep-03 0:41 
GeneralRe: Palm OR PocketPC Pin
João Paulo Figueira28-Sep-03 8:44
memberJoão Paulo Figueira28-Sep-03 8:44 
GeneralBase de datos( FreeHandle) Pin
meryzu6-Sep-03 8:47
membermeryzu6-Sep-03 8:47 
GeneralRe: Base de datos( FreeHandle) Pin
Jason Henderson6-Sep-03 11:26
memberJason Henderson6-Sep-03 11:26 
GeneralMajor n00b here... Pin
Anonymous11-Apr-03 12:09
sussAnonymous11-Apr-03 12:09 
GeneralRe: Major n00b here... Pin
kalli235-Dec-03 1:12
memberkalli235-Dec-03 1:12 
GeneralStuff Pin
Steven Gasperino11-Apr-03 11:19
sussSteven Gasperino11-Apr-03 11:19 
GeneralRe: Stuff Pin
Jason Henderson11-Apr-03 11:40
memberJason Henderson11-Apr-03 11:40 
GeneralRe: Stuff Pin
Steven Gasperino11-Apr-03 11:49
sussSteven Gasperino11-Apr-03 11:49 
GeneralPalmOS.h Pin
Amit Dey6-Apr-03 5:11
memberAmit Dey6-Apr-03 5:11 
GeneralRe: PalmOS.h Pin
Jason Henderson7-Apr-03 4:19
memberJason Henderson7-Apr-03 4:19 
GeneralRe: PalmOS.h Pin
The Abominable Steven11-Apr-03 10:15
sussThe Abominable Steven11-Apr-03 10:15 
Generalhigh-resolution timers Pin
Amit Dey24-Mar-03 20:09
memberAmit Dey24-Mar-03 20:09 
GeneralRe: high-resolution timers Pin
Jason Henderson25-Mar-03 5:30
memberJason Henderson25-Mar-03 5:30 
GeneralRe: high-resolution timers Pin
Anonymous12-Apr-03 5:40
sussAnonymous12-Apr-03 5:40 
GeneralSomething for Chris Pin
Hugo Hallman5-Nov-02 12:59
memberHugo Hallman5-Nov-02 12:59 
GeneralRe: Something for Chris Pin
Christian Graus5-Nov-02 13:07
memberChristian Graus5-Nov-02 13:07 
GeneralRe: Something for Chris Pin
Jason Henderson5-Nov-02 17:15
memberJason Henderson5-Nov-02 17:15 
GeneralMFC for Palm? Now THERE'S an article! Pin
Codin' Carlos5-Nov-02 9:24
memberCodin' Carlos5-Nov-02 9:24 
GeneralRe: MFC for Palm? Now THERE'S an article! Pin
Jason Henderson5-Nov-02 10:03
memberJason Henderson5-Nov-02 10:03 
GeneralRe: MFC for Palm? Now THERE'S an article! Pin
Codin' Carlos5-Nov-02 10:26
memberCodin' Carlos5-Nov-02 10:26 
GeneralRe: MFC for Palm? Now THERE'S an article! Pin
Christian Graus5-Nov-02 11:05
memberChristian Graus5-Nov-02 11:05 
GeneralPalm Pilot Development Pin
Anonymous5-Nov-02 4:04
sussAnonymous5-Nov-02 4:04 
GeneralRe: Palm Pilot Development Pin
Jason Henderson5-Nov-02 4:07
memberJason Henderson5-Nov-02 4:07 
Generalsmashing! maybe regular column! Pin
Amit Dey4-Nov-02 23:29
memberAmit Dey4-Nov-02 23:29 
GeneralRe: smashing! maybe regular column! Pin
Jason Henderson5-Nov-02 3:53
memberJason Henderson5-Nov-02 3:53 
General*sigh* Pin
Christian Graus4-Nov-02 19:48
memberChristian Graus4-Nov-02 19:48 
GeneralRe: *sigh* Pin
Nishant S4-Nov-02 20:57
editorNishant S4-Nov-02 20:57 
GeneralRe: *sigh* Pin
Christian Graus4-Nov-02 21:03
memberChristian Graus4-Nov-02 21:03 
GeneralRe: *sigh* Pin
Christian Graus6-Nov-02 23:44
memberChristian Graus6-Nov-02 23:44 
GeneralRe: *sigh* Pin
Nishant S7-Nov-02 14:34
editorNishant S7-Nov-02 14:34 
GeneralRe: *sigh* Pin
Jason Henderson5-Nov-02 3:50
memberJason Henderson5-Nov-02 3:50 
GeneralRe: *sigh* Pin
Christian Graus5-Nov-02 13:06
memberChristian Graus5-Nov-02 13:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 6 Nov 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid