Unit OS2: Operating System Principles
Unit OS2: Operating System Principles
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze
Copyright Notice
© 2000-2005 David A. Solomon and Mark Russinovich
2
Roadmap for Section 2.3.
3
Windows API - Overview
4
Windows API - major functionality
5
Windows API Principles
6
Windows API principles (contd.)
7
Windows API Naming Conventions
Predefined data types are in uppercase
BOOL (32 bit object to store single logical value)
HANDLE
DWORD (32 bit unsigned integer)
LPTSTR
LPSECURITY_ATTRIBUTE
Prefix to identify pointer & const pointer
LPTSTR (defined as TCHAR *)
LPCTSTR (defined as const TCHAR *)
(Unicode: TCHAR may be 1-byte char or 2-byte wchar_t)
See \$MSDEV\INCLUDE\WINDOWS.H, WINNT.H, WINBASE.H
(MSDEV=C:\Program Files\Microsoft Visual Studio\VC98\)
8
64-bit vs. 32-bit Windows APIs
Pointers and types derived from pointer, e.g. handles,
are 64-bit long
A few others go 64, e.g. WPARAM, LPARAM, LRESULT, SIZE_T
Rest are the same, e.g., 32-bit INT, DWORD, LONG
Only five replacement APIs!
Four for Window/Class Data Win32 and Win64
are referred to as
Replaced by Polymorphic (_ptr) versions
the Windows API
Updated constants used by these APIs
One (_ptr) version for flat scroll bars properties
9
Differences from UNIX
10
Portability: The Standard C Library
11
Example Application
12
Sequential File Copy
UNIX:
13
Basic cp file copy program. UNIX
Implementation
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#define BUF_SIZE 256
/* Process the input file a record
int main (int argc, char *argv []) { at atime. */
int input_fd, output_fd;
ssize_t bytes_in, bytes_out;
char rec [BUF_SIZE]; while ((bytes_in = read
if (argc != 3) { (input_fd, &rec, BUF_SIZE)) > 0) {
printf ("Usage: cp file1 file2\n"); bytes_out =
return 1; write (output_fd, &rec, bytes_in);
} if (bytes_out != bytes_in) {
input_fd = open (argv [1], O_RDONLY); perror ("Fatal write error.");
if (input_fd == -1) { return 4;
perror (argv [1]); return 2; }
} }
output_fd = close (input_fd);
open(argv[2],O_WRONLY|O_CREAT,0666); close (output_fd);
if (output_fd == -1) { return 0;
perror (argv [2]); return 3; }
}
14
File Copy with Standard C Library
15
Basic cp file copy program. C library
Implementation
#include <windows.h>
#include <stdio.h>
#include <errno.h>
#define BUF_SIZE 256
int main (int argc, char *argv []) { /* Process the input file a record
FILE *in_file, *out_file; at a time. */
char rec [BUF_SIZE];
size_t bytes_in, bytes_out; while ((bytes_in =
if (argc != 3) { fread (rec,1,BUF_SIZE,in_file)) > 0) {
printf ("Usage: cp file1 file2\n"); bytes_out =
return 1; fwrite (rec, 1, bytes_in, out_file);
} if (bytes_out != bytes_in) {
in_file = fopen (argv [1], "rb"); perror ("Fatal write error.");
if (in_file == NULL) { return 4;
perror (argv [1]); }
return 2; }
}
out_file = fopen (argv [2], "wb"); fclose (in_file);
if (out_file == NULL) { fclose (out_file);
perror (argv [2]); return 0;
return 3; }
}
16
File Copying with Windows API
17
Basic cp file copy program. Windows API
Implementation
18
File Copying with Windows API
Convenience Functions
Convenience functions may improve performance
Programmer does not need to be concerned about arbitrary
buffer sizes
OS manages speed vs. space tradeoffs at runtime
#include <windows.h>
#include <stdio.h>
19
Further Reading
20