ch11_Storage.pdf

(61 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Chapter 11
Storage
In this chapter we examine .NET Compact Framework’s capabilities for accessing local
storage, concentrating on File I/O and Registry access.
File I/O........................................................................................................................................................... 6
The File and Directory Classes........................................................................................................ 6
Byte Level I/O................................................................................................................................... 7
Higher Level I/O ............................................................................................................................... 8
Encoding and Decoding Data ........................................................................................................ 10
Reader/Writer – Stream Relationship ............................................................................................ 11
Using the I/O Classes .................................................................................................................... 11
Text File I/O.................................................................................................................................... 13
Binary File I/O ................................................................................................................................ 15
Writing Binary Data ........................................................................................................................ 15
Reading Binary Data ...................................................................................................................... 17
Binary I/O and Structures............................................................................................................... 17
File I/O Summary ........................................................................................................................... 20
Registry Access .......................................................................................................................................... 20
Opening a Registry Key ................................................................................................................. 21
The Need for P/Invoke Wrappers ..................................................................................................21
Mapping Registry Types to Managed Types ................................................................................. 22
Opening and Accessing Registry Keys.......................................................................................... 23
Reading Registry Values................................................................................................................ 24
Writing Registry Values.................................................................................................................. 25
Using Function Overloading for Registry Access .......................................................................... 26
Registry Utility Class ...................................................................................................................... 27
Updating the NOTEPADCE Sample .............................................................................................. 31
Conclusion .................................................................................................................................................. 34
Storage
Storage is an area where Windows CE-powered devices and desktop systems differ
significantly. On desktop machines the primary storage media is disk, while on Windows CE-
powered systems the primary storage media is RAM. Windows CE can support rotating
Chapter 11 – Storage
Page 1
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
magnetic storage media, but this is the exception rather than the rule 1 . Battery-powered mobile
devices in particular, such as a Pocket PC, are not suited to support power-hungry rotating
media. Thus, Windows CE storage centers on using RAM instead of disk, and so mobile device
storage tends to be fast but small. Windows CE-powered devices use a combination of ROM
and RAM. Most PocketPCs support removable storage, primarily in the form of Compact Flash
(CF) cards or Secure Digital (SD) media. [Comment 11.1]
Another difference from desktop systems is that a Windows CE operating system image
resides in a non-volatile media. This might be ROM (Read-Only Memory), or it could be flash
memory 2 . The ROM contains operating system components, applications, device drivers, and
other supporting files needed for device operation. Microsoft provides the Platform Builder tool
that third-parties use to create specific operating system images. While Microsoft provides
guidelines for what a Pocket PC or SmartPhone device should include, the actual content is
determined by the device manufacturer who creates the actual operating system image. This is
the reason that smart devices from different manufacturers sometimes have a slightly different
set of applications and device drivers. [Comment 11.2]
In spite of these differences at the physical media, there are many common features at a
higher level of abstraction. Both device and device storage support a hierarchical file system.
And both device and desktop storage support a hierarchical registry. [Comment 11.3]
The Object Store
On a Windows CE-powered device, RAM is divided into two portions: program memory and
the object store. Program memory serves the purpose that most desktop programmers
associate with memory: providing the space for code to load and run, and the space for data to
be allocated, used, and reclaimed. [Comment 11.4]
The other portion of RAM is the object store . The object store serves as the primary storage
area for Windows CE. The object store contains three things: a file system, the system registry,
and CE property databases. The Compact Framework has built-in support for file I/O, but not for
the registry or property databases. To use the registry and CE property databases, a program
calls native functions using P/Invoke. [Comment 11.5]
The maximum allowable size for the object store is 256MB. This maximum size is exactly
one-half the total RAM that can be supported by Windows CE, which is 512MB 3 . The maximum
size for individual files in the object store is 32MB. RAM not used for the object store is available
for use as program memory. [Comment 11.6]
This chapter covers two of the three types of objects in the object store. We cover the use of
Compact Framework classes to access the file system directories and files. In addition, we
cover using P/Invoke to access native functions to access the system registry. What we do not
cover are Windows CE property databases, although they are described in the accompanying
boxed discussion. [Comment 11.7]
Windows CE Property Databases
Most Windows CE features are ported from desktop Windows. There are,
however, certain features which were created to meet a need not available from
existing desktop technologies. Windows CE property databases are one of those
technologies. [Comment 11.8]
1 One exception is the IBM Microdrive; it uses rotating media in a Compact Flash (CF) form factor.
2 The HP website uses the term "Flash ROM" for the flash memory used in their iPaq Pocket PCs. The
benefit of flash memory is that it enables field upgrades.
3 That memory is factory-installed. Windows CE does not support adding additional RAM.
Chapter 11 – Storage
Page 2
Copyright © 2003 Paul Yao & David Durant
870590597.001.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Property databases provide a simple, low-overhead mechanism to store record-
based data. It provides, in essence, a very flexible flat-file manager. A fairly
simple set of C-callable functions are provided to create records. The names of
the functions reflect the Windows CE-centric nature of these functions, since
each has a prefix of 'Ce.'Included among the property database functions are
CeCreateDatabaseEx to create a new database, CeOpenDatabaseEx to open an
existing database, CeSeekDatabaseEx to search for a record,
CeReadRecordPropsEx to read a record and CeWriteRecordProps to write a
The Compact Framework does not support accessing CE property databases,
except through P/Invoke wrappers for the above named functions. We do not
cover that topic in this book. If you find that you need to access CE property
databases from a Compact Framework, you have two choices. One is to write a
set of Win32 functions that do all the main database work, and then write a set of
custom wrapper functions to pass your database records up to your managed
A second choice involves writing P/Invoke wrappers for direct calls to the CE
property database functions. The P/Invoke Wizard described in chapter 4 can
help. We provide a set of wrappers for a related set of functions, the desktop
RAPI functions that let a desktop .NET Framework program access the object
store of a Windows CE device using ActiveSync. That topic is covered in chapter
15 ( The Remote API – RAPI ). [Comment 11.11]
System Power States
RAM is volatile and requires a power source to maintain its integrity. Device manufacturers
normally provide a backup battery, whose sole function is keeping the object store intact when
the main battery is empty. When the main battery is discharged, a warning message appears
briefly and then a device enters a power critical state. At that point, a device can only start up
when the main battery is replaced or recharged. [Comment 11.12]
When both main battery and backup battery are removed, the contents of RAM – the object
store and program memory – are lost. When battery power is again available, a cold boot is run
by a bootloader in ROM. Among its other tasks, the bootloader initializes the object store to its
startup state. [Comment 11.13]
A Pocket PC has a power switch, but turning a device off is different from turning a desktop
system off. The RAM remains powered, which maintains the viability of the object store and also
the viability of program memory. For this reason, applications are not shutdown but wait in a
frozen, hibernating state. When the user wakes a Pocket PC, the user encounters a device in
the same state as when the power was turned 'off.' The 'instant-on' support makes Windows CE
suited for consumer devices. [Comment 11.14]
Most Pocket PCs also have a reset switch, which can be necessary when a device gets
hung. This switch causes a warm boot , which resets program memory but leaves the object store
Installable File Systems
In addition to the RAM-based object store, Windows CE supports installable file systems which
provide non-volatile data storage. The most common storage devices are two forms of flash
memory, Compact Flash and Secure Digital. The IBM MicroDrive provides a rotating hard drive
4 More details about power management are available in an article written by Paul Yao:
http://msdn.microsoft.com/library/en-us/dncenet/html/power_management_features.asp
Chapter 11 – Storage
Page 3
Copyright © 2003 Paul Yao & David Durant
870590597.002.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
packaged in the form factor of a Type II Compact Flash card, available in 340MB and 1GB
storage capacities. [Comment 11.16]
A maximum of 256 installable file systems can be made available on a single Pocket PC or
Windows CE device (the limit is 10 on Windows CE 3.0 devices such as the Pocket PC and
Pocket PC 2002). Many installable file systems are FAT-based, including FAT12, FAT16, and
FAT32; as the legacy of MS-DOS/Windows lives on. [Comment 11.17]
Files in an installable file system have a maximum size of 4GB, considerably more than the
32MB maximum for object-store files. On a Pocket PC, a storage card file system might appear
in the \Storage Card directory. At present, however, there are not standard names. Because
this is a localizable name, you should avoid hard-coding directory names. [Comment 11.18]
Pocket PC programmers can address this issue with a pair of native Win32 API functions:
FindFirstFlashCard / FindNextFlashCard enumerate installed file systems. (See the
FindMemoryCard sample application in chapter 4, Platform Invoke , for an example of calling
these functions). [Comment 11.19]
Thus, the files accessible to your application can be located in ROM, RAM, or the additional
devices mentioned above. They can be part of the native Windows CE file system or in an
installable file system. [Comment 11.20]
The Windows CE File System
The Windows CE file system provides a unified view of all available storage devices,
Files and directories from the operating system image (ROM-based) [Comment 11.22]
Files and directories in the object store (RAM-based) [Comment 11.23]
Files and directories on installable file systems [Comment 11.24]
The term used in the Windows CE Platform Builder documentation is “seamless integration” of
each of these different files systems. [Comment 11.25]
The architects of Windows CE simplified the file system structure from the legacy MS-
DOS/Windows disk storage architecture. They eliminated drive letters so that Windows CE uses
a simpler single-tree file system hierarchy. Instead of 26 starting possible starting points, there
is only one: the root directory ('\'). [Comment 11.26]
Because Windows CE does not support a “current directory” for a process, you must fully
qualify all path names in your application. Aside from these differences, the file system
directory structure has similarities to desktop filesystems: a hierarchical filesystem with support
for long file names. The maximum file path on Windows CE, just like on desktop file systems, is
260 characters. [Comment 11.27]
The Windows CE file system does support memory mapped files, thus providing a
mechanism for inter-process memory sharing. Like other capabilities that we discuss in this
chapter, the Compact Framework does not support memory mapped files. Chapter 4, (Platform
Invoke) , provides details on accessing native Windows CE functionality. [Comment 11.28]
ROM based files
Files located in ROM reside in the \windows directory, and are read-only by definition. Many
have the system or hidden attribute, or both. At system boot time, the object store is initialized
with whatever directories may be required. At that time, ROM-based files can be mapped to
other directories besides the \windows directory. [Comment 11.29]
The Platform Builder packages the Windows CE operating system as a ROM image,
compressing some of these files and leaving others uncompressed, as determined by the
Chapter 11 – Storage
Page 4
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
platform developer. Uncompressed ROM files run in place, in what is called an XIP ( eXecute In
Place ) region. An advantage of XIP regions is that ROM images can be mapped directly into
virtual memory, allowing system files to be available without using precious program memory.
On the down side, some ROM is slower than RAM, so code running in ROM may executes a
little slower than code that is located elsewhere. [Comment 11.30]
The \windows folder can also include files from the RAM-based object store. You can copy
files and even overwrite some of the ROM-based files that are in the \windows folder. Of
course, the file system does not actually copy to / overwrite ROM files, but it does note that you
have newer version of a ROM file in RAM, and uses the newer version whenever the file is
Another difference between Windows CE-powered systems and desktop systems is that
Windows CE does not support a PATH environment variable that determines the order in which
directories are searched when resolving requests to locate programs or shared libraries. The
hard-coded search sequence is as follows: [Comment 11.32]
The absolute path (when specified) [Comment 11.33]
The .exe launch directory (when searching for libraries) [Comment 11.34]
The \windows directory [Comment 11.35]
ROM DLL files [Comment 11.36]
While there is no PATH variable, starting with Windows CE 4.1 there is support for a registry-key
that provides a search path: HKEY_LOCAL_MACHINE\Loader , with the SystemPath property. The
best resource for details on this subject is the Win32 documentation for Windows CE,
specifically the remarks that accompany the Win32 LoadLibrary function. [Comment 11.37]
RAM based files
At system boot time, the amount of RAM available for the object store is specified during
platform creation. It can be modified on some platforms (including the Pocket PC) by a user in
the Memory tab of the Control Panel. This change can also be made programmatically through
the native SetSystemMemoryDivision Windows CE functions. (A companion function,
GetSystemMemoryDivision , lets you query the current setting.) These functions are available
to application programs but, as of the writing of this book, are only documented in the platform
Because the object store has a limited size, a compression algorithm is applied to data in
the object store. It is a simple, fast algorithm that achieves approximately 2:1 compression. The
maximum size of a single object store file is 32MB. The maximum size of a CE database, which
are also located in the object store, is 16MG. [Comment 11.39]
On the Pocket PC, user files are meant to be stored in a directory named My Documents . This
is the only directory available through the system-provided file-open and file-save dialog boxes,
although a user can see the contents of other directories using the File Explorer application.
This completes our introduction to storage. You now know that there a unified view of a
physically diverse file system, and that there is a RAM-based object store which contains a file
system, a registry, and CE databases. The focus for the remainder of this chapter is file I/O and
registry access. [Comment 11.41]
Chapter 11 – Storage
Page 5
Copyright © 2003 Paul Yao & David Durant
Zgłoś jeśli naruszono regulamin