Windows CE: The Next Big Wave?

by Ron Schwarz

Reprinted with permission from Visual Basic Programmer's Journal, 3/98, Volume 8, Issue 3, Copyright 1998, Fawcette Technical Publications, Palo Alto, CA, USA. To subscribe, call 1-800-848-5523, 650-833-7100, visit www.vbpj.com, or visit The Development Exchange

Use Visual Basic 5.0 with the CE Toolkit to create Windows CE 2.0 applications.

Every so often, events conspire to create chaos, opportunity, and disaster. Over time, we're hit with wave after wave of new technologies, and judging from past events, it seems that a lot of people are inclined to miss the right wave. When CP/M took off, the mini and mainframe priesthood sneered. When MS-DOS showed up, the CP/M adherents smugly assured us it was a passing fad. When Windows 3.0 hit the shore, the DOS diehards laughed at it. When NT started flexing its muscles, the Unixers looked down from their ivory towers and harumphed. In the same vein, many "serious" programmers scorned Visual Basic when it first appeared.

After spending the past two decades watching people who should have known better fall off the cliffs, I've resolved to keep an ear to the ground. We're now at the brink of another monumental change, and as always, it's rife with opportunity. Admittedly, it's hard to tell what innovation is going to be worth the effort. Let's face it: many of the would-be revolutions never did take off. This time, however, there's no doubt about what's happening and where it's headed. Anyone who chooses to disregard this wave is a prime candidate for the Darwin Award.

What's happening now is this: Windows has hit a fork in the road. Microsoft has made it abundantly clear that the end of the line for the Windows 95 platform is in sight; no upgrades beyond Windows 98 are planned. Every new Microsoft OS after that will be an advancement of either NT or CE. Those who know at Microsoft, from chairman Bill Gates to Windows product managers, have been saying for months that Windows NT will be the company's focus in the big-business arena. Recently the two-pronged NT/CE strategy for both home and business has become evident in their public statements.

Windows CE is a modularized, ROMable, 32-bit version of Windows that supports a variety of CPU architectures. It's lightweight and compartmentalized. CE made its debut in handheld Personal Digital Assistant (PDA)-type Handheld Personal Computers (H/PCs), such as the HP 300 series. Version 1.0 looked "95-ish" and came with a suite of lightweight counterparts to popular Office applications. You could write software only in a special version of C++.

Figure 1: Target Apps for CE. The H/PC emulator provides a virtual H/PC on your NT 4.0 desktop. You can target VB CE applications to it, and test them without having to connect to a physical H/PC.

The first of the machines loaded with CE 2.0 are based on the same kind of hardware, and some of the original CE 1.0 machines can even be upgraded with CE 2.0 ROMs. That doesn't sound like a big deal, but it's just the tip of the iceberg. In addition to a series of incrementally improved PDAs, CE is aimed at a plethora of new platforms. It will appear in game controllers, car radios, WebTV boxes, shirt-pocket computers, cell phones, and a new breed of ultra-mini laptop computers. It's also aimed at the embedded controller market, where it can provide the logic for everything from home appliances to industrial machinery.

At the moment, Microsoft has designated three target platforms—H/PC, Palm-Size PC, and Auto-PC. The CE 2.0 SDK, which you can download from Microsoft's Web site (http://www.microsoft.com/windowsce/default.asp), contains emulators for all these platforms, which run under Windows NT 4.0. The H/PC emulator creates an H/PC environment on your desktop (see Figure 1). It looks and acts like a tiny version of Windows 95. The Palm-Size PC provides a shirt-pocket computer emulation. Although no Palm-Size PC machines were available at press time, they will be soon, possibly by the time you read this. They won't have keyboards, but they will offer handwriting recognition on the touch-screen. H/PC machines have touch-screens, but no built-in handwriting recognition, although third-party utilities are available. The Auto-PC environment is the most radical. It's a car radio, controlled by a Windows CE computer. In addition to controlling audio, tuning, and CD functionality, the computer will also be able to accommodate voice I/O, e-mail, GPS navigation, cell phone control, and infrared communication with your H/PC or laptop. Auto-PCs are scheduled to become available in the 1999 model year. Because CE is compartmentalized, hardware vendors can include the specific pieces that meet their needs.

Even though Microsoft is investing huge resources in CE, it won't count for much if the hardware vendors don't run with it. Remember, for all that CE is, one big thing it isn't is a shrink-wrapped operating system that Joe User can buy off the shelf and install on his computer. That's what makes this all a no-brainer for the developer: the hardware manufacturers are jumping all over each other to get on the CE bandwagon, and even the media seems to know that it's for real.

The big question, of course, is "What's in it for me?" The short answer is "VB." Unlike the situation under CE 1.0, it's now possible to write CE 2.0 applications in VB. But because of the radically different hardware, you'll have to approach VB CE development differently from traditional VB.

how am i supposed to write code on that tiny thing?

Although the C++ CE tools have been around for awhile, their VB counterparts are just starting out. It shouldn't come as any great surprise that VB CE is still a little rough around the edges. At the time I wrote this article, VB CE was in beta; however, it should be available by now. Check Microsoft's Web site for details at http://www.microsoft.com/windowsce/developer.

You need a copy of VB5 to use VB CE because CE attaches itself—using a sophisticated add-in—to the VB5 IDE. When you work on a CE project, you have several new menu items, and several existing menu items are missing. One of the biggest changes pertains to running and debugging applications. CE projects don't execute within the IDE; the familiar toolbar buttons that stop and step a project are gone, as are the IDE's numerous debugging facilities. The IDE serves as a shell for editing and building projects, but actual execution takes place on the emulator or an H/PC connected to your development machine. You should know that VB projects are currently limited to the H/PC platform.

Figure 2: Run the Install Wizard. The CE Application Install Wizard creates an install program for your VB CE application. Run it from the Windows CE menu in the VB IDE.

In some ways, VB for CE is like a cross between Visual Basic for Applications (VBA) and VBScript. Its form handling is similar to VBA, and its syntax is closely related to VBScript. Unlike either, it uses the VB IDE as its development environment.

CE machines are unique in many ways, but from a developer's standpoint, one aspect stands out: you don't write code on them. An H/PC is great for running programs, but would be a horrible platform for writing them. In addition to the tiny keyboards and half-VGA displays, you have limited RAM and no disk drives. In short, you'd go nuts in a hurry. Although the term isn't officially used here, it wouldn't hurt to think of VB as a kind of cross-compiler for CE development.

The changes to the IDE affect five areas: menus/toolbars, options, forms, controls, and compilation. A bit more work is involved than with traditional VB projects, but that's not really surprising considering you're running the apps on another machine, either real or virtual. I'll first outline the major changes to the IDE and the language syntax, then I'll take you on a quick walk-through of the steps required to create, build, and run a project. At that point, you'll be prepared to explore on your own.

Keep a few things in mind before embarking on any VB CE development. First, and most important, the emulators can only run on NT 4.0, with SP3 applied. Second, you need to have VB5 Professional Edition, with the VB5 SP2 applied. You can create projects under Windows 95, but you can target them only to a hardware platform, which means you need to have an H/PC running CE 2.0 attached to your desktop computer.

The Project and Tools menus have significant changes. The various "Add…" items in Project are trimmed down to "Add Form," "Add Module," and "Add File." The reason is simple: VB CE does not support classes, user controls, and MDI forms. Likewise, References has been removed due to the restricted CE environment. The Project Properties dialog has been completely redesigned. It has three tabs: General, Make, and About. The General tab bears a superficial relationship to the standard VB General tab, but much of the content—threading, execution, control upgrade, license key, and help context ID—has been removed. In place of those options is a frame labeled "Run on Target:," which offers a choice of Emulator and Remote Device. One possible caveat: if you downloaded the CE SDK separately from the CE VB kit, you might have installed emulators other than H/PC. I recommend not installing that SDK, and if you already have it installed, uninstalling it. I could not run the VB kit unless I remove the other kit and reinstall the VB kit. The VB kit includes the H/PC emulator.

Figure 3: Invoke the Debugger. Because VB CE isn't executed from within the IDE, an outboard debugger is necessary. You invoke the debugger from the Debug entry of the Project menu.

The Project Type entry is also gone. New properties include "Form Width," "Form Height," "Local Path," and "Remote Path." Use the Form Width and Height properties to set the default size of your forms to the screen size of the target H/PC. The Format menu contains a new "Reset Form Dimensions" entry, which resets a form to the defaults set in Project Properties. The Local Path setting points at the target dir for emulation builds, and the Remote Path setting tells VB where to place the executable when building directly to a hardware H/PC.

Icon, Command Line, and Conditional Compiliation options have been removed from the Make tab. In their place is a "Build Debug" check box. If you plan to run your project through the debugger, check this box; otherwise, leave it blank for distribution builds. The About tab simply displays version information for the CE add-in.

The new Windows CE menu appears when you're working on a CE project. It contains entries for a series of CE-oriented utilities, but when you're starting out with VB for CE, you most likely want to access the "Windows CE Books Online" and "Application Install Wizard" entries (see Figure 2). The "Download Runtime Files" entry places copies of the necessary run times into the appropriate directories in either the emulation or hardware path, depending on which option you target.

not your standard display

As you might imagine, the tiny H/PC displays are not suited to the same style of window layout techniques you're accustomed to using on your high-resolution desktop monitor. Nonetheless, the typical half-VGA display is sufficient for serious work, if you manage it carefully. As a result, most CE applications run in full-screen mode. In addition, the "mouse" is a stylus that you tap, double-tap, or drag over the touch-sensitive screen. To simulate a right-click, you hold down the Alt key while tapping. Because of the nature of the CE environment, many of the standard form properties are no longer available. You can't resize forms, nor can you use MDI forms.

As in VBA, you can't use control arrays. And standard OCX controls won't work either, because they're not compiled for CE CPU architectures. Most of the intrinsic VB controls work, and custom controls designed for CE can also be included. You might have noticed that when I was comparing the Project Properties dialogs, I didn't mention the "Compile" or "Component" tabs. That's because they're gone. In the case of Component, it's simple: a VB CE project can be only a standalone program, because it doesn't support Class modules, UserForms, or UserDocuments. The Compile tab options are missing because the project isn't truly compiled—it's a sort of p-code that is interpreted by a runtime engine, so the compilation options are moot.

The Debug and Run menus are missing too, because their functionality is provided by external means—emulator, H/PC, or debugger.

Although form and control handling is similar to VBA, the language is close to VBScript. Unlike VBScript, though, VB for CE supports forms and code modules, limited to one code module per project. However, the semantics are quite similar. The main limitations are the absence of data access and traditional file I/O, although I was told the final shipping product would include file system and data access ActiveX controls. Variant is the only data type, and VB CE has no access to the Printer object. Like server-side VBScript, you have a set of simple functions for handling text files, although the first beta version hasn't implemented them. For an exhaustive comparison of the minutia, you should consult the Books Online included with the VB CE package. If you've got a recent SDK that lists the current VBScript syntax differences—Internet or ActiveX—you're pretty up-to-date.

The changes are not all restrictive. The new string-handling functions found in VBScript are available in CE. They can save a considerable amount of coding effort.

Once you've installed the VB CE kit, you're ready to create your first CE project. The example project that accompanies this article—HELLO.VBP, included in the free, Registered Level of The Development Exchange (see the Code Online box at the end of this article for details)—is, like any decent "hello world" program, pretty darned simple. It has one form, one text box, one command button, and, naturally, one line of executable code:

Private Sub cmdReverse_Click()
   txtInput = StrReverse(txtInput)
End Sub

I used the StrReverse function to show off one of the new string-handling features of VB for CE. Anything you type into the text box is automatically reversed when you click on the button. Click on it again, and it re-reverses.

The purpose of a Hello app isn't to show off the language, and it certainly isn't to perform any useful function. Doing the "hello thing" in traditional VB takes a couple of mouse clicks and a deft flick of the F5 key, but CE mode is a little different.

The first thing you have to do is start VB, and select "Windows CE Project" as the type of new project to create. The Project Properties dialog appears, giving you a chance to change the defaults. Next, create your application. Now you're ready to run it. This is where things are a little different. With a standard VB project, when you either press F5 or click on the "Start" button on the toolbar, your program starts executing within the VB IDE. When coding for CE, VB needs to "make" a PVB file. That's because you can't run a project inside the IDE, so you have to create an executable to run on the emulator or H/PC. Although you can manually select the "Make" entry from the File menu, and the "Download Runtime Files" entry from the Windows CE menu, you can press F5—or click on the "Start" button—and let VB invoke the Make and Download functions automatically.

DebugGING and Installing Applications

The debugger is used to insert breakpoints, trace execution, step through code, and watch expressions (see Figure 3). To set a breakpoint, wait for the debugger to finish linking up with the program you're debugging, then double-click on the form name from the "Visual Basic Forms" window. Then either press F9, click on the hand "Toggle Breakpoint" icon, or click in the margin to the left of the desired line of code in the code window.

What do you give your users to install software on computers without disk drives? Disks, of course! H/PCs are generally used in conjunction with desktop computers. Windows CE comes with utilities that copy data between the H/PC and the desktop computer, as well as synchronize PIM data between the two machines. The VB CE kit comes with the source code for a setup program to help you create an install package to run over the desktop-H/PC link. When you install the VB CE kit, it creates a directory in your VB directory named "vbce." Look in VB\vbce\samples\setup for the SETUP.VBP example. In addition, you can use the "Application Install Wizard" available from the Windows CE menu.

I've only scratched the surface of developing for CE. The VB CE download is about 96 MB—a good reason to order it on CD. This is definitely emerging technology, and I'll be the first to admit it's a tad rough around the edges. But remember, it's in its infancy, and Microsoft has opened up an early beta to anyone who wants to experiment with it. This is an opportunity to work with a platform that, like VB 1.0, is poised to stake a serious claim in the marketplace.

Download the code for this article here