Smart Thinking

John Kennedy
Microsoft Corporation

February 12, 2002

Download this article in Microsoft Reader format.

In case you've had your head in a bucket for the past few months (not something I recommend by the way, but that's an entirely different story), you might be aware that Microsoft has been working away on something called .NET.

It's always easy to ignore this kind of thing as a marketing exercise, but it turns out that this time they're serious. .NET is important, and it's going to drastically change the way applications are developed on all platforms—including the precious Pocket PC.

As you are probably aware, the latest version of the Visual Studio® development suite is called Visual Studio .NET. Its purpose is to make it easy for you, the developer, to create great applications that make the most of the .NET Framework. As you know, I've been working on the User Education team for two years now, and no one is more pleased with the fact that we're finished. It's a remarkable product, and you're going to love it (and especially some of the online help, into which I've managed to hide most of my thoughts regarding Drew Barrymore. Or at least, I've been able to express those thoughts in terms of some pretty racy ATL library documentation).

Somehow Larry escaped with his life after talking about using Visual Basic® .NET on the Pocket PC back in October, before much official information was available. I'm more of a chicken than Larry, and I've waited until now before writing about this new way of developing. Sadly, at the time of writing, the tools have yet to be released but don't worry, they'll be here sooner rather than later.

Watch Your Language

So do you want the good news or the bad news? Well, the good news is that you still don't have to use Visual Basic to develop applications on the Pocket PC. The other news isn't really that bad either, although there is no Visual C++® support at the moment, there is support for Visual C#™. C# is a brand new language designed from the ground up for creating reliable software components. C# is equally at home with traditional GUI applications as it is with the latest XML Web services.

The Pocket PC implementations of both Visual Basic .NET and Visual C# are, as my editing friend likes to say, for all intensive purposes the same as the desktop versions. This speeds up cross-platform development tremendously. It also means that the Pocket PC has gained an incredibly rich library of classes, but more on that in a moment.

I won't lie to you. If you've been brought up on a strict diet of C and C++, it's going to take a little while to get your head around C#. It can be quite a culture shock, as C# is different in some subtle, and some not-so-subtle, ways. Even the way in which you design and implement a program is different—the C# environment looks a lot more like Visual Basic than Visual C++. That aside, I think you'll like developing in C#. It has familiar syntax, but is quicker to write and more difficult to make mistakes with—check out the references at the end of this article for some more information.

Managed Code and the .NET Framework

The .NET concept features several key issues that make it different from development platforms with which you may be familiar. Perhaps the most interesting is the concept of managed code versus unmanaged code. In the past, you've probably developed Pocket PC (or desktop) applications using unmanaged code. That is, languages such as Visual Basic, C or C++. When you are in the zone, churning out pages of this kind of code, you know the feeling of the wind in your hair, the warmth of the sun on your skin, and the dangers of an uninitialized variable or stack overflow.

Of course you have freedom, but with it comes much responsibility. You need to be ruthless in your error checking, and watch for memory leaks with eagle-eyed precision (not that eagles can actually see memory leaks, but I think the point is clear enough). On the desktop, you might get away with the occasional little lapse—forgetting to freeing an object here, using some unallocated memory there, but on a device with limited resources, such as a Pocket PC, you rarely get away with such things. As mobile devices become more pervasive (think of your application running on an always-on mobile phone, for example), users want to know nothing about software. A gadget that locks up or needs rebooting to reclaim memory is not a useful gadget.

Most sinister is the fact that we live in a world were some people like to get their kicks by writing malicious programs designed to screw things up for everyone else. Or worse, software designed to pinch credit card details. Again, as we're moving to an information-everywhere-all-the-time lifestyle, we all need to be able to trust our software.

This is what .NET and managed code is all about. Managed code is targeted at a special runtime that processes and executes it. The runtime can examine the code to make sure it's not doing anything naughty. It can look after memory allocation and release. It can handle errors that might otherwise bring the system to a blue-screened death. It'll do all this and it'll even help you write programs that can access XML Web services. Managed code is a Good Thing.

Managed code is there to help you, the programmer, and your users, by making applications quicker to develop, more reliable, and more secure. But that's only half the story. The .NET Framework provides the runtime magic, and it also gives you sets of classes designed to pamper your every programming whim.

Well, of course there are limits to how many of your whims can be pampered. And there are also size constraints that mean that compared to the complete, desktop .NET Framework, the so-called Compact Framework is a subset. The vital classes are present of course, but some less useful classes are missing, and some mobile device specific classes are present.

Be Smart

So that's the vague, non-Pocket PC specific introduction to what .NET is all about. The more you read about it, the more you will realize that .NET is actually pretty cool, and I'm not just saying that because of the name on my paychecks. Then again, I might just be saying that part too. I guess to know the truth you'll need to meet me in a bar and buy me a pint of two of Guinness.

Now we can get a little more specific and turn to the Pocket PC development side of things. Rather than go the way of the Visual eMbedded Toolkit and create a completely standalone mobile device version of Visual Studio, Microsoft has returned to the plug-in concept. The Smart Device Extensions is a plug-in that is installed into Visual Studio .NET. It provides new Project types for mobile devices, and looks after the entire edit-compile-download-test-swear-edit cycle. It comes complete with a Pocket PC emulator and the usual suite of remote tools, such as Spy, Registry Editor, and so on. Debugging is fully supported.

The downside is that suddenly the development tools aren't free anymore. You'll need to buy the desktop version of Visual Studio .NET, and there's just no way around that. The bottom line is that you can't use the Visual eMbedded Toolkit to develop using the Smart Device Extensions, sorry.

The upside is that you get a kick-butt piece of state-of-the-art development software (with simply the best online documentation available, ahem) that can be made to create the next generation of Internet-aware applications.

Practical Pocket PC Considerations

With all this talk of runtimes, you might get depressed thinking about how interpreted code will crawl on a Pocket PC. Fear not—the managed code is actually compiled before it is executed. I've been using the SDE to write Pocket PC applications and performance is certainly acceptable. OK, so I'm not going to use it to write a Quake 3D shoot-'em-up, but I know which development tool is going to make it easier to write an application for Consolidated Grommit Incorporated who want to pay me twenty grand for a program that accesses a Web service.

Running managed code applications on a Pocket PC requires the runtime to be present on the device, and unfortunately the runtime isn't built into the ROM of even the current Pocket PC 2002 devices. At the moment, this means a hefty 1MB to 2MB of extra data to be carried around in memory. Presumably the runtime will be built into the ROM of any forthcoming mobile devices, but I'm not privy to this kind of information which means it's only my guess, so don't quote me.

Should you consider moving to using Smart Device Extensions? The answer depends on what kind of applications you are writing. If you're working on rather low-level stuff like action games, Today screen components, and so on, it's probably a good idea to stick to eMbedded Visual Tools and C++.

If you are writing a more "serious" application, then it's definitely something you need to consider. Remember, if you get scared or lost, you can also mix managed and unmanaged code. It's possible to call any Windows® CE API functions that aren't wrapped in a Framework class, for example.

Walkthrough: Building a Pocket PC .NET Application

Here's a sample SDE application being constructed. These pictures were smuggled out of the building at great personal risk, so don't let on it was me who showed them to you. Also, remember that the final product will probably look a lot different from what you see here.

From within Visual Studio .NET, select the C# projects tree. With the Smart Device Extensions installed, there will be a new project type called Smart Device Application, as shown in Figure 1.

Figure 1. Smart Device Application project type

In turn, this leads to a wizard that allows you to select the device you are targeting, as well as a more selective project type. The wizard will create a new C# project for you, reminiscent of creating an MFC application in the old eMbedded Visual Toolkit. This time however, the language is C# and the classes used belong to the .NET Framework.

Figure 2. Example of C# project created by the wizard

Figure 3 is a real, live drag-and-drop form. It might look like Visual Basic, but don't be fooled—we haven't gone over the dark side.

Figure 3. Drag-and-drop form with a button and PictureBox control

From within the designer, I can double-click on the button to bring up the relevant code. Here I've added a few lines to change the color of the PictureBox control.

Figure 4. Designer view of the code

Hit F5 to compile your application, and Visual Studio .NET will deploy it to your device, which in this case is the emulator. All the required compact Framework files are also copied over if and when they are needed.

Figure 5 is the running application itself. One tap on the button and the PictureBox control changes color. OK, so it's not going to win any design awards, but you have to admit that it didn't take long to create and deploy. This is the future of Pocket PC application development!

Figure 5. The running application itself

Head in a Bucket

Oh yes, and back to that bucket story. Working with a team of technical writers is always interesting, because, frankly, we're the oddest bunch of people you're ever likely to meet. Believe me, what follows is a fairly typical event. At a recent team gathering over coffee in the office cafeteria, a dispute broke out over who was in the possession of the largest head. Specifically, a member of the team we'd taken to referring to as "Old Big Head" was denying his massive skull was the largest and demanded proof.

Always ready for a challenge, the entire team headed to the parking garage, where we took turns sticking our heads in a bucket of tepid water and measuring the displacement. It was indeed a cunning plan, and it did happen to prove that Old Big Head didn't actually have the largest head on the team. I don't really want to discuss who did as it turns out, but thought I'd furnish you with the tale in case you were wondering.

And that's all for this month!

In Other News

DirectPlay for Pocket PC 2002 has been released. The final beta for this great networking library includes support for both wired and 802.11b wireless network connections. Check it out at the official Web site: https://www.betaplace.com.

If you've been checking out the current happenings in the Pocket PC market, you might have noticed a growing number of add-on keyboards available. Some of these miniature keyboards have very clever designs and snap onto the bottom of the Pocket PC to make a very integrated unit. When designing your applications, or pitching your latest solution to a mega-corporation, you might want to take them into consideration.

C# References

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.