MobileWindows Mobile Development with MFC

Windows Mobile Development with MFC content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Building Windows Mobile applications opens new development and business opportunities for a C++ developer, and given the issues with managed development on mobile devices that you covered in last month’s article, C++ remains by far the best tool to take advantage over the wide range of development possibilities that Windows Mobile offers. This article examines how the frameworks and supported platforms for a Mobile Application (also referred to as a Smart Device application in the Visual C++ documentation) should be chosen, and then looks at the development process for a MFC Application.

With mobile development in C++, there are two important decisions to make before creating the project—which application framework (if any) will be used to build the application, and which Mobile SDKs will the application target. The type of functionality that the application offers will go a long way to deciding the appropriate application framework to use and SDK to target—for generic line-of-business applications such as an accounting application, the use of the MFC Smart Device Application that supports Pocket PC 2003 and above will be the best option. In contrast, the development of a plug-in for the Today screen of a Windows Mobile 6 Professional device obviously requires the corresponding SDK, and given that the plug-in will be loaded all of the time, the minimalist Win32 Smart Device Project option makes the most sense. In general, targeting the oldest SDK that contains the feature-set that will be used in a given application with the lightest application framework (Win32 followed by ATL followed by MFC) will be the best option. ATL sits between the more heavy-weight MFC option and the minimalistic Win32 option, and provides a wealth of support for COM development.

As with desktop development, it is difficult to anticipate all the features that an application will require when the application skeleton is first being developed, and as with development on the desktop and server, maintaining loose coupling at a source-code level between the various functional areas of an application is the best defence against the possibility that the targeted SDK or chosen application framework needs to change. In addition, rather than take a dependency on MFC just to use a few classes, it is preferable to use features in the C Runtime Library (CRT) and Standard C++ Library for utility types like strings and collections.

The final point worth making on choosing the appropriate starting point for a project is on the best technique for starting specialised applications that have a high degree of interaction with specific features of Windows Mobile such as the Today/Home screen, Internet Explorer Mobile, or a specific piece of hardware. For these specialized applications, it is often easier to start with a sample for the SDK that shows the basics of the task, strip the sample down so it is essentially a skeleton application, and then build on top of this skeleton. The alternative of starting with an App Wizard-generated project and then trying to add the configuration and set-up items to integrate the project with the device typically will be much harder. If it is likely that the skeleton application will need to be re-used as the basis for a number of applications, it is worth the effort to strip out all project-specific strings and make the skeleton into a Visual Studio project-type. It is a relatively easy undertaking, and will generate a positive return on investment with only a few uses.

MFC Smart Device Applications

Rather than provide a dry description of the application frameworks for Mobile Devices, the remainder of the article will follow the development of a MFC Smart Device Application that allows a user to check the number of hours that are booked on a particular date with appointments. All personal information manager (PIM) functionality provided on a Mobile device should be built on top of Pocket Outlook (see the Designed for Windows Mobile Software Application Handbook for other development guidance of a similar nature), and this sample will show how that can be achieved. The application offers reasonably rich functionality (at least with the scope of a demonstration application), so the use of MFC makes sense because it greatly simplifies the task of hooking together UI elements.

After the decision to use MFC has been made, the next decision is the range of devices the application will support. An inspection of the documentation related to the PIM functionality on Windows Mobile reveals that information on the calendar is available through the IPOutlookApp COM interface that has been part of Windows Mobile for a very long time (Pocket PC 2000 and above, according to the MSDN documentation for the interface), so targeting an old SDK like Pocket PC 2003 make sense. Most applications developed against older SDKs will work without modification on newer devices, and the range of emulators that are available with the various Windows Mobile SDK versions makes forward-compatibility checks simple to complete.

The code to implement the application is stock-standard MFC, with very little differences from standard MFC desktop development. The sample application is dialog- rather than document-based, so the Dialog option needs to be selected when the project is created, as shown in Figure 1. As with the desktop version of MFC, a dialog editor is present in Visual Studio to allow controls to be positioned with a WYSIWYG editor, and these controls then can be bound to a member variable in a particular C++ class. Data exchange between the control and member variable is achieved using the same Dialog Data Exchange (DDX) architecture as the desktop version of MFC.

Figure 1: Creating the MFC Application

For the sample application, two controls are required—a date picker to allow the user to select the date for which Outlook information will be retrieved, and a button to begin the search. Once the results have been retrieved, a message box to show the total number of appointments on the nominated day will be displayed.

The actual code to retrieve data from Pocket Outlook is relatively straightforward. The first task is to initialise COM and create the root Outlook COM object (error handling has been stripped for readability, but is present in the sample code that accompanies the article):

#include <initguid.h>
#include <pimstore.h>

//inside button click handler
CoInitializeEx(NULL, 0);
IPOutlookApp * polApp;
CoCreateInstance(CLSID_Application, NULL,

Once Outlook has been logged in, the items in the calendar folder can be retrieved and searched:

IPOutlookItemCollection* pItems;
IFolder* pFolder;
polApp->GetDefaultFolder(olFolderCalendar, &pFolder);

IAppointment* pAppt;
CComBSTR query;
//populate query using date selected by user –- see sample for
//this code
//Sample query string:
//[Start] >=11/30/2007 00:00 AND [End] < 12/ 1/2007 00:00
pItems->Find(query, (IDispatch**)&pAppt);

The Find function is a general-purpose PIM querying tool, and works across all the different folder types such as Calendar, Contacts, and Tasks. If Find brings back any appointments for the selected date, the code simply loops through them and keeps a count of the total of minutes in appointments for display to the user:

long totalApptLength = 0;
long apptLength      = 0;

if (pAppt){
   totalApptLength += apptLength;
   while(S_OK == pItems->FindNext((IDispatch**)&pAppt)){
      totalApptLength += apptLength;

Figure 2 shows the end form of the sample, with a Message Box displaying the total booked minutes for a particular day.

Figure 2: Final version of the sample running in PocketPC 2003 Emulator

Although displaying the total number of minutes booked for a particular day isn’t overly useful, it is easy to imagine the sample application extended to a more graphical presentation that could display some type of heat-map of the busyness of a particular day or week within the calendar to allow end-users to identify when they are heavily booked.


The developer experience in C++ with Windows Mobile is excellent, and moving from the desktop to the mobile device doesn’t involve giving up a heap of functionality. The debug experience for an application is excellent—hitting F5 launches the emulator, copies the application across, and starts the application ready to debug. All the normal debug features work—breakpoints, tracing, and variable inspection all work fine. As the sample application shows, the MFC features such as the WYSIWYG editor and DDX that make development much quicker compared to the tedious construction of Win32 GUI apps are all present. C++ has the added advantage over its managed contemporaries with the richness of the libraries available for mobile development. Although C# and VB.NET rely heavily on the .NET Framework libraries to provide the helper classes needed to build an application, C++ developers have the freedom to bring in header and source code files from any location and compile them as part of their application.

Next month, you’ll round out the tour of Windows Mobile with a look at ATL and COM development in the embedded world.

Download the Code

You can download the code that accompanies this article here.

About the Author

Nick Wienholt is a Windows and .NET consultant based in Sydney, Australia. He has worked on a variety of IT projects over the last decade and continues to stay involved in the developer community. Nick is the co-founder and president of the Sydney Deep .NET User group, writes technical articles for Pinnacle Publishing and the Microsoft Developer Network, and is a participant in many .NET-related newsgroups. Nick’s most recent book is Maximizing .NET Performance.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories