`
`CD-ROM
`Included
`
`Program ming
`
`"DOUG'S CODE
`DEMONSTRATES
`A PERFECT GRASP
`OF WINDOWS CE(cid:173)
`CRAFTY AND ELEGANT. "
`-Charles Petzold, author,
`Programming Windows
`
`The
`definitive
`guide to
`programming
`the Windows CE
`API
`
`Douglas Boling
`
`Apple Inc. Exhibit 1030
`
`
`
`Programming
`
`Port your Win32 skills to the
`expanding realm of portable
`computing technology.
`
`Design sleek, high-performance applications for the newest
`generation of smart devices with PROGRAMMING MICROSOFT
`WINDOWS CE. This practical, authoritative reference explains
`how to extend your Windows or embedded programming skills
`to the Windows CE environment. You'll review the basics of
`event-driven development, then tackle the intricacies and
`idiosyncrasies of this modular, compact architecture:
`
`• Get under the hood of the streamlined Win32® API
`•
`Investigate platform-specific programming considerations
`• Use specialized techniques for handling memory, storage,
`and power constraints
`• Delve into serial , network, and device-to-desktop
`communications
`• Advance your skill with modules, processes, and threads
`• Build or modify code to meet the requirements of different
`Windows CE shells
`
`Use Doug's expert guidance and the software development
`tools on CD-ROM to mobilize your Win32 programming efforts
`for exciting new markets!
`
`ISB N 1 57231 856 - 2
`
`90000
`
`90145 18562
`
`9 781572 318564
`
`Apple Inc. Exhibit 1030
`
`
`
`I
`
`®
`
`I
`
`Douglas Boling
`
`Microsoft· Press
`
`Apple Inc. Exhibit 1030
`
`
`
`pUBLISHED BY
`.
`.rvJicros~ft Press .
`A Diviston of Mtcrosoft Corporation
`one Microsoft Way
`Redmond, Washington 98052-6399
`
`copyright © 1998 by Douglas McConnaughey Boling
`
`Ail rights reserved. No part of the contents of this book may be reproduced or
`transmitted in any form or by any means without the written permission of the publisher.
`
`Library of Congress Cataloging-in-Publication Data
`Boling, Douglas McConnaughey, 1960-
`programming Microsoft Windows CE I Douglas McConnaughey Boling.
`p.
`em.
`Includes index.
`ISBN 1-57231-856-2
`1. Microsoft Windows (Computer file)
`I. Title.
`(Computers)
`QA76.76.063B623 1998
`005.4'469--dc21
`
`2. Operating Systems
`
`98-39279
`CIP
`
`Printed and bound in the United States of America.
`
`123456789 QMQM 321098
`
`Distributed in Canada by ITP Nelson, a division of Thomson Canada Limited.
`
`A CIP catalogue record for this book is available from the British Library.
`
`Microsoft Press books are available through booksellers and distributors worldwide. For further
`information about international editions, contact your local Microsoft Corporation office. Or
`contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at
`mspress.microsoft.com.
`
`Active Desktop, Developer Studio, Microsoft, Microsoft Press, MS-DOS, Visual C++, Win32, Win(cid:173)
`dows, the Windows CE logo, and Windows NT are either registered trademarks or trademarks of
`Microsoft Corporation in the United States and/or other countries. Other product and company
`names mentioned herein may be the trademarks of their respective owners.
`
`Acquisitions Editor: Eric Stroo
`Project Editor: Kathleen Atkins
`Technical Editor: Jim Fuchs
`
`Apple Inc. Exhibit 1030
`
`
`
`I was introduced to Microsoft Windows CE right before it was released in the fall of
`1996. A Windows programmer for many years, I was intrigued by an operating sys(cid:173)
`tem that applied the well-known Windows API to a smaller, more power-conserving
`operating system. The distillation of the API for smaller machines enables tens of
`thousands of Windows programmers to write applications for an entirely new class
`of systems. The subde differences, however, make writing Windows CE code some(cid:173)
`what different from writing for Windows 98 or Windows NT. It's those differences
`that I'll address in this book.
`
`JUST WHAT IS WINDOWS CE?
`Windows CE is the newest, smallest, and arguably the most interesting of the Micro(cid:173)
`soft Windows operating systems. Windows CE was designed from the ground up to
`be a small, ROM-based operating system with a Win32 subset API. Windows CE ex(cid:173)
`tends the Windows API into the markets and machines that can't support the larger
`footprints of Windows 98 and Windows NT.
`Windows 98 is a great operating system for users who need backward compati(cid:173)
`bility with DOS and Windows 2.x and 3.x programs. While it has shortcomings, Win(cid:173)
`dows 98 succeeds amazingly well at this difficult task. Windows NT, on the other hand,
`is written for the enterprise. It sacrifices compatibility and size to achieve its high level
`of reliability and robustness.
`Windows CE isn't backward compatible with MS-DOS or Windows. Nor is it
`an all-powerful operating system designed for enterprise computing. Instead, Win(cid:173)
`dows CE is a lightweight, multithreaded operating system with an optional graphi(cid:173)
`cal user interface. Its strength lies in its small size, its Win32 subset API, and its
`multiplatform support.
`
`PRODUCTS BASED ON WINDOWS CE
`The first products designed for Windows CE were handheld "organizer" type devices
`with 480-by-240 or 640-by-240 screens and chiclets keyboards. These devices, dubbed
`Handheld PCs, were first introduced at Fall Comdex 96. Fall Comdex 97 saw the re(cid:173)
`lease of a dramatically upgraded version of the operating system, Windows CE 2.0,
`
`xiii
`
`Apple Inc. Exhibit 1030
`
`
`
`Introduction
`
`with newer hardware in a familiar form-this time the box came with a 640-by-240
`landscape screen and a somewhat larger keyboard.
`In January 1998 at the Consumer Electronics Show, Microsoft announced two
`new platforms, the Palm-size PC and the Auto PC. The Palm-size PC was aimed di(cid:173)
`rectly at the pen-based organizer market currently dominated by the Palm Pilot. The
`Palm-size PC sports a portrait mode, 240-by-320 screen and uses stylus-based input.
`A number of Palm-size PCs are on the market today.
`Figure I-1 shows both a Palm-size PC, in this case a Casio E-10, and a Handheld
`PC, in this case a Casio A-20.
`
`Figure 1-1. Tbe Casio E-10 Palm-size PC and the Casio A-20 Handheld PC.
`
`Just as this book is being released, Microsoft has introduced the Handheld PC
`Professional, which is a greatly enhanced H/ PC with new applications and which uses
`the latest version of the operating system, Windows CE 2.11.1 This device brings the
`compact nature of Windows CE to devices of laptop size. The advantages of apply(cid:173)
`ing Windows CE to a laptop device are many. First, the battery life of a Handheld PC
`Pro is at least 10 hours, far better than the 2-to 3-hour average of a PC-compatible
`laptop. Second, the size and weight of the Windows CE devices are far more user
`friendly, with systems as thin as 1 inch weighing less than 3 pounds. Even with the
`diminutive size, a Handheld PC Pro still sports a large VGA screen and a keyboard
`that a normal human can use. The Vadem Clio Handheld PC Pro, shown in Figure 1-2,
`is an example of how Windows CE is being used in newer platforms. The system
`
`1. Windows CE 2.11 is Windows CE 2.10 with a few minor changes.
`
`xiv
`
`Apple Inc. Exhibit 1030
`
`
`
`can be used as a standard laptop or "flipped" into a tablet-mode device. 1bis de(cid:173)
`vice is just one example of how Windows CE is expanding into new system types.
`
`Introduction
`
`Figure 1-2. Tbe Vadem Clio Handheld PC Pro.
`
`I refer to the Handheld PC Pro throughout this book under its operating system
`version, Windows CE 2.1, because the platform name, Handheld PC Pro, was deter(cid:173)
`mined very late in the process. I knew of, and in fact, had a hand in the development
`of a Handheld PC Pro under its code name Jupiter. However, you can't use code names
`in a book, so its operating system version had to suffice.
`Other platforms-Auto PC, Web TV set-top boxes, and embedded platforms
`designed for specific tasks-are also appearing or will appear in the coming months.
`What's amazing about Windows CE is that the flexibility of the operating system al(cid:173)
`lows it to be used in all these diverse designs while all the time retaining the same
`basic, well-known Win32 API.
`
`WHY YOU SHOULD READ THIS BOOK
`Programming Microsoft Windows CE is written for anyone who will be writing appli(cid:173)
`cations for Windows CE. Both the embedded systems programmer using Windows CE
`for a specific application and the Windows programmer interested in porting an ex(cid:173)
`isting Windows application or writing an entirely new one can use the information
`in this book to make their tasks easier.
`The embedded systems programmer, who might not be as familiar with the
`Win32 API as the Windows programmer, can read the first section of the book to
`
`X V
`
`Apple Inc. Exhibit 1030
`
`
`
`,.-- - - - -
`
`Introduction
`
`become familiar with Windows programming. While this section isn't the compre(cid:173)
`hensive tutorial that can be found in books such as Programming Windows by Charles
`Petzold, it does provide a base that will carry the reader through the other chapters
`in the book. It also can help the embedded systems programmer develop fairly com(cid:173)
`plex and quite useful Windows CE programs.
`The experienced Windows programmer can use the book to learn about the
`differences among the Win32 APis used by Windows CE, Windows NT, and Windows
`98. Programmers who are familiar with Win32 programming recognize subtle differ(cid:173)
`ences between the Windows 98 and Windows NT APis. The differences between
`Windows CE and its two cousins are even greater. The small footprint of Windows CE
`means that many of the overlapping APis in the Win32 model aren't supported. Some
`sections of the Win32 API aren't supported at all. On the other hand, because of its
`unique setting, Windows CE extends the Win32 API in a number of areas that are
`covered in this text.
`The method used by Programming Windows CE is to teach by example. I wrote
`numerous Windows CE example programs specifically for this book. The source for
`each of these examples is printed in the text. Both the source and the final compiled
`programs for a number of the processors supported by Windows CE are also pro(cid:173)
`vided on the accompanying CD.
`The examples in this book are all written directly to the API, the so-called
`"Petzold" method of programming. Since the goal of this book is to teach you how to
`write programs for Windows CE, the examples avoid using a class library such as MFC,
`which obfuscates the unique nature of writing applications for Windows CE. Some
`people would say that the availability of MFC on Windows CE eliminates the need for
`direct knowledge of the Windows CE API. I believe the opposite is true. Knowledge
`of the Windows CE API enables more efficient use of MFC. I also believe that truly know(cid:173)
`ing the operating system also dramatically simplifies the debugging of applications.
`
`WHAT ABOUT MFC?
`The simple fact is that Windows CE systems aren't the best platform for a general(cid:173)
`purpose class library like MFC. The slower processors and the significantly lower
`memory capacity of Windows CE devices make using MFC problematic. Most Win(cid:173)
`dows CE systems don't include the MFC library in their ROM. This means that the
`MFC and OLE32 DLLs required by MFC applications must be downloaded into the
`systems. The first versions of the Palm-size PCs don't even support MFC.
`That said, there's a place for MFC on Windows CE devices. One such place might
`be if you're designing a custom application for a system you know will have the MFC
`and OLE32 DLLs in ROM. For those specific applications, you might want to use MFC,
`but only if you know the target environment and have configured the system with
`the proper amount of RAM to do the job.
`
`xvi
`
`Apple Inc. Exhibit 1030
`
`
`
`Introduction
`
`WINDOWS CE DEVELOPMENT TOOLS
`This book is written with the assumption that the reader knows C and is at least fa(cid:173)
`miliar with Microsoft Windows. All code development was done with Microsoft Vi(cid:173)
`sual C++ 5.0 and Windows CE Visual C++ for Windows CE under Windows NT 4.0.
`To compile the example programs in this book, you need Microsoft Visual C++ 5.0,
`which is part of the integrated development environment (IDE), DevStudio, run(cid:173)
`ning on a standard IBM-compatible PC. You also need Microsoft Visual C++ for
`Windows CE, which isn't a stand-alone product. It's an add-in to Visual C++ 5.0 that
`incorporates components to the compiler that produce code for the different CPUs
`supported by Windows CE. Visual C++ for Windows CE isn't currently available through
`standard retail channels, but information on ordering it directly from Microsoft can
`be found on the Microsoft Web site. Finally, you need one of the platform SDKs for
`Windows CE. These SDKs provide the custom include files for each of the Windows
`CE platforms. These platform SDKs are available for free on the Microsoft Web site.
`As a convenience, I've also included the platform SDKs available at the time of the
`writing of this book on the accompanying CD.
`While not absolutely required for developing applications for Windows CE,
`Windows NT 4.0 is strongly recommended for the development environment. It's
`possible to compile and download Windows CE programs under Windows 98, but
`many of the features of the integrated development environment (IDE), such as Win(cid:173)
`dows CE emulation and remote debugging, aren't supported.
`Visual C++ for Windows CE won't change the outward appearance of Visual C++,
`with the exception of a few new tools listed under the tools menu. Nor will the in(cid:173)
`stallation of Visual C++ for Windows CE prevent you from developing applications
`for other Windows operating systems. The installation of Visual C++ for Windows CE
`will result in new Windows CE targets such as WCE MIPS and WCE SH and WCE x86Em
`being added to the platforms listing when you're creating a new Win32 application.
`Also, a Windows CE MFC AppWizard will be added to the new projects listing to assist
`in creating MFC programs for Windows CE.
`
`TARGET SYSTEMS
`You don't need to have a Windows CE target device to experience the sample pro(cid:173)
`grams provided by this book. The various platform SDKs come with a Windows CE
`emulator that lets you perform basic testing of a Windows CE program under Win(cid:173)
`dows NT. This emulator comes in handy when you want to perform initial debugging
`to ensure that the program starts, creates the proper windows, reacts to menu selec(cid:173)
`tions, and so on. However, the emulator has some limitations and there simply is no
`replacement for having a target Windows CE system to perform final debugging and
`testing for applications.
`
`xvll
`
`Apple Inc. Exhibit 1030
`
`
`
`Introduction
`
`You should consider a number of factors when deciding what Windows CE
`hardware to use for testing. First, if the application is to be a commercial product,
`you should buy at least one system for each type of target CPU. You need to test against
`all of the target CPUs because, while the source code will probably be identical, the
`resulting executable will be different in size and so will the memory allocation foot(cid:173)
`print for each target CPU.
`Most applications will also be written specifically for the Handheld PC or Palm(cid:173)
`size PC, not both. Although the base operating system for both the Handheld PC and
`Palm-size PC is Windows CE, the hardware underneath is vastly different. The strict
`memory constraints of the Palm-size PC, as well as its much smaller screen, its differ(cid:173)
`ent orientation, and its lack of a keyboard, force compromises that aren't acceptable
`on a Handheld PC or its larger relative, the Handheld PC Pro. Other constraints on
`Palm-size PC systems, such as the lack of printing and TrueType support, differenti(cid:173)
`ate its environment from the Handheld PC's.
`In this book, I demonstrate programs that can run on the Handheld PC,
`Handheld PC Pro, or Palm-size PC. The goal is to allow the lessons to be applied to
`all platforms. For some examples, however, the different screen dimensions mean
`that the example will run better on one particular system. I point out the differences
`and the reasons they exist. For example, some controls might exist on only one plat(cid:173)
`form or the other. The shells for the two platforms-Handheld or Palm-size-are also
`different and need separate coverage. Finally, a small set of features in Windows CE
`are simply not supported on the smaller Palm-size PC platform.
`
`WHAT'S ON THE CD
`The accompanying CD contains the source code for all the examples in the book.
`I've also provided project files for Microsoft DevStudio so that you can open
`preconfigured projects. Unless otherwise noted, the examples are Windows CE 2.0
`compatible so that they can run on most Windows CE systems available today. Chap(cid:173)
`ter 13, "Shell Programming-Part 2" contains examples that are compiled for
`Windows CE 2.01, so they won't run on current Handheld PCs. There are some ex(cid:173)
`amples, such as the console applications in Chapter 12, that are specific to the
`Handheld PC Pro and other devices running Windows CE 2.10.
`When you build for a specific platform, remember that it might not be back(cid:173)
`ward compatible with earlier versions of Windows CE. For example, Microsoft moved
`some of the C library support from statically linked libraries in Windows CE 2.0 into
`the operating system for Windows CE 2.01, the Palm-size PC release. This reduces
`the size of an executable, but prevents code built for the Palm-size PC from running
`on a Handheld PC running Windows CE 2.0. You can, however, compile code for a
`Handheld PC running Windows CE 2.0 and have it run on a Palm-size PC.
`
`xviii
`
`Apple Inc. Exhibit 1030
`
`
`
`Introduction
`
`In addition to the examples, the CD contains a number of folders of interest to
`the Windows CE programmer. I've included the platform SDKs for the Handheld PC
`as well as for the Palm-size PC. Unfortunately, the Handheld PC Pro SDK wasn't avail(cid:173)
`able in time for this release. Like the other platform SDKs, that one is available for
`free on the Microsoft Web site. Check out the readme file on the CD for late-breaking
`information about what else is included on the CD.
`
`OTHER SOURCES
`While I have attempted to make Programming Microsoft Windows CE a one-stop shop
`for Windows CE programming, no one book can cover everything. A nice comple(cid:173)
`ment to this book is Inside Windows CE by John Murray. It documents the "oral his(cid:173)
`tory" of Windows CE. Knowing this kind of information is crucial to understanding
`just why Windows CE is designed the way it is. Once you know the why, it's easy to
`extrapolate the what, when trying to solve problems. Murray's book is great, not just
`because of the information you'll learn about Windows CE but also because it's an
`entertaining read.
`For learning more about Windows programming in general, I suggest the clas(cid:173)
`sic text Programming Windows by Charles Petzold. This is, by far, the best book for
`learning Windows programming. Charles presents examples that show how to tackle
`difficult but common Windows problems. For learning more about the Win32 kernel
`API, I suggest Jeff Richter's Advanced Windows. Jeff covers the techniques of pro(cid:173)
`cess, thread, and memory management down to the most minute detail. For learning
`more about MFC programming, there's no better text than Jeff Prosise's Programming
`Windows 95 with MFG. This book is the "Petzold" of MFC programming and simply
`a required read for MFC programmers.
`
`FEEDBACK
`While I have striven to make the information in this book as accurate as possible,
`you'll undoubtedly find errors. If you find a problem with the text or just have ideas
`about how to make the next version of the book better, please drop me a note at
`CEBook@DelValle.com. I can't promise you that I'll answer all your notes, but I will
`read every one.
`
`Doug Boling
`Tahoe City, California
`August 1998
`
`xlx
`
`Apple Inc. Exhibit 1030