Hardware Reference
In-Depth Information
The next six API functions in Fig. 6-39 are fairly similar to the corresponding
UNIX system calls. Note, however, that Windows 7 I/O is, in principle, asynchro-
nous, although it is possible for a process to wait for completion. The last two
functions allow a region of a file to be locked and unlocked to permit a process to
get guaranteed mutual exclusion to it.
Using these API functions, it is possible to write a procedure to copy a file,
analogous to the UNIX version of Figure 6-36. Such a procedure (without any
error checking) is shown in Fig. 6-40. It has been designed to mimic the structure
of Fig. 6-36. In practice, one would not have to program a copy file function since
CopyFile is an API function (which executes something close to this program as a
library procedure).
/* Open files for input and output. */
inhandle = CreateFile(
, GENERIC READ, 0, NULL, OPEN EXISTING, 0, NULL);
outhandle = CreateFile( ′′ newf ′′ , GENERIC WRITE, 0, NULL, CREATE ALWAYS,
FILE ATTRIBUTE NORMAL, NULL);
′′
data
′′
/* Copy the file. */
do {
s = ReadFile(inhandle, buffer, BUF SIZE, &count, NULL);
if(s>0&&count > 0) WriteFile(outhandle, buffer, count, &ocnt, NULL);
} while (s>0&&count > 0);
/* Close the files. */
CloseHandle(inhandle);
CloseHandle(outhandle);
Figure 6-40. A program fragment for copying a file using the Windows 7 API
functions. This fragment is in C because Java hides the low-level system calls
and we are trying to expose them.
Windows 7 supports a hierarchical file system, similar to the UNIX file system.
The separator between component names is \ however, instead of /, a fossil inher-
ited from MS-DOS . There is a concept of a current working directory and path
names can be relative or absolute. One significant difference, however, is that
UNIX allows the file systems on different disks and machines to be mounted toget-
her in a single naming tree starting at a unique root, thus hiding the disk structure
from all software. Early versions of Windows (pre Windows 2000) did not have
this property, so absolute file names had to begin with a drive letter indicating
which logical disk was meant, as in C:\windows\system\foo.dll . Starting with Win-
dows 2000 UNIX -style mounting of file systems was added.
The major directory management API functions are given in Fig. 6-41, again
along with their nearest UNIX equivalents. The functions should be self-explana-
tory.
Windows 7 has a much more elaborate security mechanism than most UNIX
systems. Although there are hundreds of API functions relating to security, the
 
Search WWH ::




Custom Search