Get Going with Microsoft eMbedded Visual C++

John Kennedy
Microsoft Corporation

May 23, 2001

Welcome back to the extremely hip Pocket PC development corner of MSDN, where Larry Roof and myself take turns attuning your programming skills to the handheld devices that are increasing in popularity every second.

While Larry is walking the streets with his iPAQ in search of wireless Internet access, the sensible dudes like myself are checking e-mail while sitting in a comfortable chair the size of a Volkswagen Beetle in the local Starbucks. It's no joke—Seattle's favorite coffee emporium is busy installing 11Mbps wireless Internet points, which means I can write this particular column with a double-tall latte in one hand, a donut in the other, and well, thank goodness for the voice recognition features in Office XP.

Last month, good old Larry set about demonstrating development using that wannabe language of Visual BASIC. You know the one—it's more a graphic adventure program than a real programming language, involving nice little pictures and dialog boxes that you can drag over the screen. This time we'll use a REAL programming language—C++. And don't worry, we won't stop at a simple "Hello World." If you are an experienced C++ coder, you'll be amazed at how easy it is to apply your existing knowledge to this exciting new platform. If you are just starting out, or merely dabbling, you might also be impressed with how straightforward it can be to write applications for your Pocket PC.

If you have Windows® developing experience, you might be wondering if this version of C++ is some kind of "toy" language. Will it offer Win32® compatible APIs? What about MFC? Or ATL? What kind of debugging support is there?

Well, you're in for a treat. eVisual C++® is not a cut-down version of C++, it's the real McCoy. And the Pocket PC operating system isn't a toy either—it's a real, professional operating system, so you can expect many of your Win32 APIs to be present. MFC? No problem. It's supported, and that goes for ATL as well. While some features are missing or reduced in complexity, you'll recognize enough to feel right at home.

Getting Ready for eVC

Back in the Dark Ages, or so it might seem now, the Windows CE development suite consisted of a "plug-in" for the vast Visual Studio® 6 development suite. In fact, multiple plug-ins were required—the Windows CE toolkit component, and then a Software Development Kit (SDK) for each device for which you wanted to develop. The SDKs were free, but the Windows CE toolkit cost money. That, combined with the requirement of the pricey professional version of Visual Studio 6, made developing for CE an expensive business.

When the Pocket PC device was released, it was apparent that a lot of things had been rethought. Happily for us, that included the development tools. Rather than the expensive system of the past, there is now a completely separate, standalone version of the Visual Studio tools designed solely for Windows CE development, called the Microsoft eMbedded Visual Tools 3.0. The SDKs are still there, one required for each device you want to develop for, that is, Pocket PC, Handheld PC, and whatever is next. The eMbedded Visual Tools is now a free download from https://msdn.microsoft.com/library/default.asp?url=/downloads/list/pocket2002.asp and comes complete with versions of both C++ (eVC++) and Visual Basic® (eVB). At over 300 MB, it's a hefty download, so consider requesting the toolkit on CD-ROM. Full details are available on the Microsoft Evaluation & Resource Center Web site.

You might be thinking, "Well, I don't actually have a Pocket PC, but I'd still like to try it. Will you lend me your Pocket PC?" And, of course, I'd love to lend you my device, but there's a quicker way—the emulator. The emulator is a software version of the Pocket PC that runs on your desktop. It looks and acts like the real thing—it even has many of the standard Pocket PC applications, and you and write software and see it running. Okay, so it's not perfect, but you'll find it very useful—especially if you don't have a Pocket PC in your pocket.

Figure 1. The Pocket PC emulator makes it easy to get developing right away.

If you are setting up a PC to act as your Pocket PC development device, I strongly recommend that you use Windows 2000 as your operating system, and that you don't install any previous versions of Visual Studio (Windows CE or for any other version of Windows). Don't be tempted to use Windows XP, or even Windows 98 or 95. While it is possible to use Windows 98, there are several "gotchas"—you need to use Windows 98 SE, and even then, important tools, such as the emulator won't function. I use an IBM Thinkpad 390 laptop as my development machine. While it might not be state of the art (feel free to bribe me with a new Thinkpad, IBM!), it works very well with Windows 2000 and of course, it's mobile for those trips to Starbucks for caffeine upgrades.

Speaking of coffee, I'll duck out now for a quick beverage, while you sort out your PC and get the development tools installed. Remember, it's perfectly possible to have both the eVB Visual BASIC tools and the eVC Visual C++ tools on the same machine, so if you followed Larry's advice last time, your machine is still good to go.

My First Project

Firing up eMbedded Visual C++ will present you with a display that looks very familiar to Visual C++ version 6 veterans. There are a few changes, but by and large, if you have used Visual C++ 6.0, you'll already be feeling like you've just bumped into an old friend.

Now is an excellent time to introduce you to the Wizards—a set of features that is going to make your life much easier. The Wizards walk you through creating your project, even getting you to the stage were you can create a complete, stand-alone program that says "Hello world," believe it or not. It gets better, because thanks to the emulation system, you don't even need to own a Pocket PC in order to see all this working. Microsoft really has gone out of their way to make this as painless as possible for you. So let's give it a go!

Creating a Project in Six Easy Steps

  1. Start eVC.
  2. From the File menu, select New.
  3. You'll see the following dialog box—or something similar—depending on the SDKs you have installed. The dialog box lists all the different possible program types the Wizards know how to make for you.
  4. Select WCE Pocket PC Application.
  5. Enter a name for your project, for example, Helloworld, in the Project name: box.
  6. Click OK.

Figure 2. Available Project Wizards

Creating a Project with Just One More Easy Step

Once you have decided on the name and type of the your project, you'll also have the ability to decide how much of the work is done for you. At this point, let's be extra lazy.

Programming Lazily

  1. Select A typical "Hello World" application.
  2. Click Finish.
  3. Click OK.
  4. Wait a bit as the code is created for you.

Figure 3. Programming the lazy way

Amazingly, that's all there is to writing a Hello World application. If you don't believe me, just run it. Let's try the emulator to start with, as it's a useful little utility. It's not perfect, but it's free.

Building Your Program

  1. Select Win32 (WCE x86em) Release or Win32 (WCE x86em) Debug as the Active Configuration. The pull-down is marked in the screenshot below.
  2. From the Build menu, select Execute Helloworld.exe.
  3. The emulator will be loaded and appear on the screen. Cool!
  4. You'll be asked if you want to build your program. Select Yes.
  5. Your new program will then be built, copied over, and started.
  6. If your program doesn't start, tap Start on the emulator and find Helloworld.exe.

Figure 4: Selecting the Active Configuration.

Figure 5: Your amazing application

Of course, not everyone really needs such a powerful and earth-changing program, so the Wizards offer you several other options, such as creating an empty project or creating an MFC application. We'll look at these options, don't worry.

The Same but Different

If you look through your new project's source code, and you know what you're looking for, you'll see all the familiar bits and pieces that make up any Windows program. So what's the fuss all about? Is a Pocket PC simply an ordinary PC that has been passed through a Shrink-o-Matic ray? Nope,it's different. Perhaps the most fundamental difference is that there's no hard drive, and so no lengthy boot up process. When you press the "On" button on a Pocket PC, it powers up within a second, so it's right there when you need it.

When you press "Off," the device drops into a special low-power state. The screen is turned off, the CPU goes to sleep, and only a tiny amount of battery current is used to keep memory and some internal clocks ticking over.

If you press the reset button, the device clears its program memory, stops all running programs, and restarts. If you remove the power (or trigger a full reset with a device-specific key combination), then the device will perform a complete reset and revert to its factory settings. You don't want this to happen very often, if at all, as it removes your installed applications (but not the ones built into ROM, such as Pocket Word) and your data.

So, why am I telling you all this? "Are you drunk?" as my manager asks me with alarming frequency. No—this is one of the situations where the Pocket PC is different from a "real" PC, and when we can take advantage of it. Here's the deal—we're going to develop an application that makes a nice sound when you switch the Pocket PC on. My plan is to allow you add a nice sound effect—such as the sound the Hitchhiker's Guide to the Galaxy book makes (in memory of Douglas Adams)—when you switch on your iPAQ, Jornada, or Cassiopeia.

Now, if you've used a Pocket PC, you might have discovered the directory called Startup under Windows. You might have assumed that this is where you put an application when you want it to be launched when the device starts up. Well, that's the trick—it doesn't work like that. Any programs in the Startup directory are only executed after a reset,which means we can't play our sound effect simply by placing a sound effect program into Startup. Or we could, but it would only play every time you perform a reset, which is pretty lame.

Here's the cunning part. We're going to use a Windows CE specific function that launches a program on power up. The program we'll launch will be a program that plays a sound effect. With me so far? Great, let's get started.

Making a Sound

First, we'll write a program that plays a sound sample. Pocket PC's can replay standard WAV files using a simple API function called sndPlaySound(). Keeping with our lazy style, we'll use the Wizard to create an empty project, and then simply drop in the API. Here we go.

Creating Your PlaySound Program

  1. Close any existing project in the embedded Visual Tools.
  2. From the File menu, select New.
  3. Select WCE Pocket PC Application.
  4. Enter a program name, such as PlaySound.
  5. Select OK.
  6. Select A simple Windows CE application as the project type.
  7. Select OK to create your project.

Use the File View to browse through the source files that make up your project, and select PlaySound.cpp. This is the core of the program, and the location where we will add the code that triggers the sound effect. The Wizard even adds a comment that says "Place code here," in case there was any doubt.

So, replace that line with this one:

sndPlaySound(L"Asterisk",SND_ASYNC);

The sndPlaySound function takes two arguments—the first is the name of the file to play, the second is a flag that tells the system how to play the sound. You can get more information on this function from online help. We can count on the sample Asterisk being there, as it's built into every device as a default sound. We can actually leave off the ".wav" part of the filename, and it will be assumed. Check out the letter L too—it's not a typo, it's a request to the compiler to treat the text that follows as Unicode, and we'll deal with Unicode in later editions.

If you have a real Pocket PC, this is a good time to connect it up and run ActiveSync®. If you don't, you can still persevere with the emulator, but be honest, you really want to buy one now, don't you? Go on, we'll wait for you while you visit your local computer store.

OK, back yet? Good. Unpack your Pocket PC and get it connected. Now you'll need to determine the processor your device uses so you can pick the correct Active Configuration. Your device will tell you if select Start/Settings/System/About, but here's a handy table for the most common devices:

Device Processor
HP Jornada SH3
Compaq iPAQ ARM
Casio Cassiopeia MIPS

Use this information to select the correct configuration. Don't worry about selecting Debug or Release at this point—either one will do.

Build the program, and it will be copied to your device automatically.. When you run it, you should hear the sample. If you don't, check that you have the volume control on your device turned up and that you have spelled "Asterisk" correctly. (Don't scoff—my boss regularly insults my spelling, which as a technical writer can be damaging to my self-confidence. Is it any wonder I'm the emotional wreck I am?)

Now we'll write the program that executes this PlaySound program on power-up. Close the PlaySound project and create another new "A simple Windows CE application" type project. Call this one "PlayOn," and this time add the following lines to the PlayOn.cpp file:

CeRunAppAtEvent(L"Playsound",NOTIFICATION_EVENT_WAKEUP);

This function is Windows CE specific (the Ce at the start of the name should tell you that), and it essentially runs a given application when it receives a given notification message. The message in question is NOTIFICATION_EVENT_WAKEUP and that is the message that is sent out when the device is powered-up.

When you run this program, which you only need to do once, the system will be ready—power off and then on, and… and… you'll see an error message. The Playsound.exe application needs to be in the Windows/ directory, rather than in the Windows/Start Menu directory where it was placed by default. From the Pocket PC's File Explorer, locate Playsound.exe and then cut and paste it into the main Windows directory. Now power off and then back on, and voila!—the sound sample is triggered.

Two questions are probably occurring to you now. Firstly, how can you use your own sample, and secondly, how can you stop this from happening every single time you switch on your device.

Using your own sample is easy. Find any WAV file and copy it to the Pocket PC. Try not to use any truly bizarre sample rates, and then change the Playsound.exe program to reflect the new filename.

Stopping the system from running the PlaySound.exe application every time is slightly trickier. The notification system survives a warm reset, and so you'll need to write another application. Create this one and call it PlayOff, and add the following single line of code:

CeRunAppAtEvent(L"Playsound",NOTIFICATION_EVENT_NONE);

This line cancels out the existing notification. Run it once, and the auto-startup feature will be disabled.

That's It for Now

I hope this introduction to eMbedded Visual C++ wasn't too much or too little for you. It's always difficult to strike a balance between being trivial and being pointlessly complicated. Feel free to e-mail me if you think I'm straying in one direction more than another, and you can help shape forthcoming columns. Thanks for reading!

 

John Kennedy is a Technical Writer/Programmer in the Visual C++ group by day and leads a secret life as a Pocket PC developer by night.

Larry Roof is a partner at tonked, a firm that specializes in the development of mobile solutions and training. He's the author of Professional Visual Basic Windows CE, available from Wrox Press.