Building a WinCE Application. Code Example
 return to main page


This section describes how to create, build and run a Windows CE application using eMbedded Visual C++. Moreover, it provides the complete source code for HelloPurdue, a version of the typical "Hello World!" program.

Note: If you have not yet modified the registry of the Jornada, please read this section.

To start building a Windows CE Application, follow these steps:

  • Start MS eMbedded Visuals Tools C++. Select File, then New.
  • Now you must select the CPUs.
    • If building for the Jornada 720 Handheld PC or Jornada 568 Pocket PC: Under the CPUs menu check Win32 (WCE ARM), Win32 (WCE x86em) and Win32 (WCE x86) target platforms. The lattest allows you to run the sample program in the emulator.
    • If building for the Jornada 548 Pocket PC: Under the CPUs menu check Win32 (WCE SH3) and Win32 (WCE x86em) target platforms. The lattest allows you to run the sample program in the emulator.
  • Select WCE Application. Enter a name for the Project, ie. HelloPurdue, and select OK.
  • Next you'll be asked to create either "an empty project", a "simple WCE application" or a "typical Hello World! Application". Pick empty project.
  • Now you must create the source files. Select File > New > C/C++ Header File. Enter the file name, ie HelloPurdue.h and write the code. Next, select File > New > C++ Source File. Enter the file name, ie HelloPurdue.c and copy the code you can find below.
  • After you have created the proper source files for HelloPurdue you need to select the active patform and target.
    • To build the application and run it/test it on the emulator:
      • If building for the Jornada 720 Handheld PC: set the active platform to H/PC Pro 2.11, select the target Win32 (WCE x86em) Debug and Handheld PC Pro Emulation. Then select Build > build HelloPurdue.exe to build the program.
      • If building for the Jornada 568 Pocket PC: set the active platform to Pocket PC 2002, select the target Win32 (WCE x86) Debug and Pocket PC 2002 Emulation. Then select Build > build HelloPurdue.exe to build the program.
      • If building for the Jornada 548 Pocket PC: set the active platform to Pocket PC, select the target Win32 (WCE x86em) Debug and Pocket PC Emulation. Then select Build > build HelloPurdue.exe to build the program.
      This step compiles the source and, assuming you have no compile errors, automatically launches the emulator and inserts the EXE file into the emulator file system; you can then launch HelloPurdue by double-clicking the exe file located in the My Handheld PC directory if using the Jornada 720, or in the Start menu, if using a Pocket PC.
    • To build the application and download it to the device: If you have your Handheld PC or Pocket PC available, attach it to your desktop PC the same way you would to sync the contents to the Jornada with the PC. Open ActiveSync and establish a connection between the Jornada and the desktop PC. Next, switch back to eMbedded Visuals C++.
      • If building for the Jornada 720 Handheld PC: set the active platform to HPC 2000, select the compile target Win32 (WCE ARM) Debug and HPC2000 (Default Device) to debug for the HP Jornada 720. Then select Build > build HelloPurdue.exe to build the program.
      • If building for the Jornada 568 Pocket PC: set the active platform to Pocket PC 2002, select the target Win32 (WCE ARM) Debug and Pocket PC 2002 (Default Device). Then select Build > build HelloPurdue.exe to build the program.
      • If building for the Jornada 548 Pocket PC: set the active platform to Pocket PC, select the target Win32 (WCE SHE) Debug and Pocket PC (Default Device). Then select Build > build HelloPurdue.exe to build the program.
      If there are no errors, Visual C++ automatically downloads the compiled program to the remote device. The program is also placed in the My Handheld PC directory if using a Jornada 720, or in the Start Menu if using a Pocket PC.

     

  • Note: Anytime you modify the source code, you must select Build > Rebuild All when debugging, if you want the changes to take effect.
Hello Purdue! Application viewed on the Pocket PC 2002 Emulator
Hello Purdue! Application viewed on the Handheld PC 2000 Emulator

 

 

 

Source Files

 

HelloPurdue.h
//======================================================================
// Header file
//
//
//================================================================
// Returns number of elements

#define dim(x) (sizeof(x) / sizeof(x[0]))

//----------------------------------------------------------------------
// Generic defines and data types
//

struct decodeUINT {                                                   // Structure associates
UINT Code;                                                               // messages
                                                                               // with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
struct decodeCMD {                                                  // Structure associates
UINT Code;                                                              // menu IDs with a
LRESULT (*Fxn)(HWND, WORD, HWND, WORD);     // function
};

//----------------------------------------------------------------------
// Generic defines used by application

#define IDC_CMDBAR 1                                           // Command bar ID

//----------------------------------------------------------------------
// Function prototypes
//

int InitApp (HINSTANCE);
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);

// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

// Message handlers
LRESULT DoCreateMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoHibernateMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoActivateMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

 

 

HelloPurdue.c
//======================================================================
// HelloPurdue - A simple application for Windows CE
//
//
//======================================================================

#include <windows.h>      // For all that Windows stuff
#include <commctrl.h>    // command bar includes
#include "HelloPurdue.h" // Program-specific stuff

//----------------------------------------------------------------------
// Global data
//

const TCHAR szAppName[] = TEXT("HelloCE");
HINSTANCE hInst;          // Program instance handle

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
         WM_CREATE, DoCreateMain,
         WM_PAINT, DoPaintMain,
         WM_HIBERNATE, DoHibernateMain,
         WM_ACTIVATE, DoActivateMain,
         WM_DESTROY, DoDestroyMain,
};

//======================================================================
// Program entry point
//

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                LPWSTR lpCmdLine, int nCmdShow) {
         MSG msg;
         int rc = 0;
         HWND hwndMain;

         // Initialize application.
         rc = InitApp (hInstance);
         if (rc) return rc;

         // Initialize this instance.
         hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
         if (hwndMain == 0) return 0x10;

         // Application message loop
         while (GetMessage (&msg, NULL, 0, 0)) {
                  TranslateMessage (&msg);
                  DispatchMessage (&msg);
         }
         // Instance cleanup
         return TermInstance (hInstance, msg.wParam);
}
//---------------------------------------------------------------
// InitApp - Application initialization
//

int InitApp (HINSTANCE hInstance) {
         WNDCLASS wc;

#if defined(WIN32_PLATFORM_PSPC)
         // If Pocket PC, only allow one instance of the application
         HWND hWnd = FindWindow (szAppName, NULL);
         if (hWnd) {
         SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
         return -1;
         }
#endif
         // Register application main window class.
         wc.style = 0; // Window style
         wc.lpfnWndProc = MainWndProc; // Callback function
         wc.cbClsExtra = 0; // Extra class data
         wc.cbWndExtra = 0; // Extra window data
         wc.hInstance = hInstance; // Owner handle
         wc.hIcon = NULL, // Application icon
         wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Default cursor
         wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
         wc.lpszMenuName = NULL; // Menu name
         wc.lpszClassName = szAppName; // Window class name

         if (RegisterClass (&wc) == 0) return 1;
         return 0;
}
//------------------------------------------------------------------
// InitInstance - Instance initialization
//

HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
         HWND hWnd;

         // Save program instance handle in global variable.
         hInst = hInstance;

         // Create main window.
         hWnd = CreateWindow (szAppName, // Window class
                                             TEXT("Hello"), // Window title
                                             WS_VISIBLE, // Style flags
                                             CW_USEDEFAULT, // x position
                                             CW_USEDEFAULT, // y position
                                             CW_USEDEFAULT, // Initial width
                                             CW_USEDEFAULT, // Initial height
                                             NULL, // Parent
                                             NULL, // Menu, must be null
                                             hInstance, // Application instance
                                             NULL); // Pointer to create
         // parameters
         if (!IsWindow (hWnd)) return 0; // Fail if not created

         // Standard show and update calls
         ShowWindow (hWnd, nCmdShow);
         UpdateWindow (hWnd);
         return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//

int TermInstance (HINSTANCE hInstance, int nDefRC) {
     return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//

LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
                                                        LPARAM lParam) {
         INT i;
         //
         // Search message list to see if we need to handle this
         // message. If in list, call procedure.
         //
         for (i = 0; i < dim(MainMessages); i++) {
                  if (wMsg == MainMessages[i].Code)
                           return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
         }
         return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//

LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                                      LPARAM lParam) {
         HWND hwndCB;

         // Create a command bar.
         hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
         // Add exit button to command bar.
         CommandBar_AddAdornments (hwndCB, 0, 0);
         return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//

LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                                    LPARAM lParam) {
         PAINTSTRUCT ps;
         RECT rect;
         HDC hdc;

         // Adjust the size of the client rectangle to take into account
         // the command bar height.
         GetClientRect (hWnd, &rect);
         rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));

         hdc = BeginPaint (hWnd, &ps);
         DrawText (hdc, TEXT ("Hello Purdue!"), -1, &rect,
         DT_CENTER | DT_VCENTER | DT_SINGLELINE);
         EndPaint (hWnd, &ps);
         return 0;
}
//----------------------------------------------------------------------
// DoHibernateMain - Process WM_HIBERNATE message for window.
//

LRESULT DoHibernateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                                           LPARAM lParam) {

         // If not the active window, nuke the command bar to save memory.
         if (GetActiveWindow() != hWnd)
                  CommandBar_Destroy (GetDlgItem (hWnd, IDC_CMDBAR));
         return 0;
}
//----------------------------------------------------------------------
// DoActivateMain - Process WM_ACTIVATE message for window.
//

LRESULT DoActivateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                                        LPARAM lParam) {
         HWND hwndCB;
         // If activating and no command bar, create it.
         if ((LOWORD (wParam) != WA_INACTIVE) &&
            (GetDlgItem (hWnd, IDC_CMDBAR) == 0)) {

                  // Create a command bar.
                  hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);

                  // Add exit button to command bar.
                  CommandBar_AddAdornments (hwndCB, 0, 0);
         }
         return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//

LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                                        LPARAM lParam) {
         PostQuitMessage (0);
         return 0;
}

 

 

 Back to top