View
217
Download
0
Tags:
Embed Size (px)
Citation preview
1JMH Associates © 2004, All rights reserved
Chapter 1Chapter 1Chapter 1Chapter 1
Getting Started with Win32/64
2JMH Associates © 2004, All rights reserved
Chapter 1 OBJECTIVESChapter 1 OBJECTIVESChapter 1 OBJECTIVESChapter 1 OBJECTIVES
Upon completion of this Chapter, you will be able to: Describe the Windows API
Its role in Windows 2000, XP, 2003 (“NT5”) And obsolete systems (9X, NT4) Windows style and programming conventions
Develop Windows applications using Microsoft Visual C++ Develop and run a simple application Use the basic debugger features Use the online help to obtain additional information
Win64 migration and portability issues
3JMH Associates © 2004, All rights reserved
OVERVIEW (1 of 2)OVERVIEW (1 of 2)OVERVIEW (1 of 2)OVERVIEW (1 of 2)
Windows 2000, XP, 2003 (“NT5”) as Operating Systems Their roles as operating systems The Windows API Win64 migration and portability Differences Architecture
4JMH Associates © 2004, All rights reserved
OVERVIEW (2 of 2)OVERVIEW (2 of 2)OVERVIEW (2 of 2)OVERVIEW (2 of 2)
Getting Started with Windows Naming conventions Programming conventions Style Sample program
Lab: Use Visual C++ to build and run a sample application
5JMH Associates © 2004, All rights reserved
WINDOWS NT FAMILY AS WINDOWS NT FAMILY AS OPERATING SYSTEMSOPERATING SYSTEMS
WINDOWS NT FAMILY AS WINDOWS NT FAMILY AS OPERATING SYSTEMSOPERATING SYSTEMS
Windows 32-bit operating systems have all the features required for desktop, departmental, and enterprise computing
64-bit systems are on the way
Essential features include: Memory: large, flat, virtual memory address space File systems, console, and other I/O Multitasking: processes and threads Communication and synchronization
Single system and networked Security
6JMH Associates © 2004, All rights reserved
THE Windows APITHE Windows APITHE Windows APITHE Windows API
Windows is the 32-bit API used by: Windows 9X (95, 98, Me) Windows NT Windows CE (palmtops, embedded systems, etc.) Win64 is very similar at the source level
Supported on Windows 2003 and Itanium processor family Windows statements nearly always apply to Win64
There are several major subdivisions, including: Windows Management Graphics Device Interface (GDI) System Services Multimedia Remote Procedure Calls
7JMH Associates © 2004, All rights reserved
SYSTEM SERVICESSYSTEM SERVICESSYSTEM SERVICESSYSTEM SERVICES
This course covers the System Services The brains of Windows System Services enable everything else The Course Chapters cover the essential system service
Repeat: Topics NOT covered Device Drivers OS internals Graphical User Interface (GUI) programming COM, DCOM, MFC, .net Development environments – learn as you go
8JMH Associates © 2004, All rights reserved
WINDOWS NT 5 (1 of 2)WINDOWS NT 5 (1 of 2)WINDOWS NT 5 (1 of 2)WINDOWS NT 5 (1 of 2)
All platforms use the Windows API, BUT there are differences:
Windows NT4 (and above) has full NSA “Orange Book” C2 security features.
“NT” means NT 4.0 and above (including all NT5) Windows 9X only runs on Intel x86 architecture Only NT supports SMP Windows 2003 also runs on Itanium, . . . Windows 2003 for Win64 migration
Note: Windows CE also supports Windows on several processor architectures
9JMH Associates © 2004, All rights reserved
WINDOWS NT5 (2 of 2)WINDOWS NT5 (2 of 2)WINDOWS NT5 (2 of 2)WINDOWS NT5 (2 of 2)
Windows NT uses UNICODE international character set throughout
Windows 9X limits asynchronous I/O to serial devices Windows NT has a fully protected kernel Windows NT supports the NTFS, a robust file system Windows 9X and CE will not support as many resources
Open files, processes, etc. Many Windows 9X Windows functions have restricted
implementations
In general, Windows programs are portable between platforms at both the source and, mostly, binary level
10JMH Associates © 2004, All rights reserved
THE WINDOWS NT ARCHITECTURETHE WINDOWS NT ARCHITECTURETHE WINDOWS NT ARCHITECTURETHE WINDOWS NT ARCHITECTURE
Windows is the dominant environment running on the NT (all versions) executive
OS/2 and POSIX compatility modes are rarely used
Historical interest only
11JMH Associates © 2004, All rights reserved
KERNEL
HAL: Hardware Abstraction
HARDWARE
Process Manager Systems ServicesVirtual Memory Manager
I/O ManagerNTExecutive
ProtectedSubsystems
Applications
OS/2Program
WindowsProgram
POSIXProgram
OS/2Subsystem
WindowsSubsystem
POSIXSubsystem
12JMH Associates © 2004, All rights reserved
GETTING STARTED:GETTING STARTED:MINIMUM SYSTEM REQUIREMENTSMINIMUM SYSTEM REQUIREMENTS
GETTING STARTED:GETTING STARTED:MINIMUM SYSTEM REQUIREMENTSMINIMUM SYSTEM REQUIREMENTS
System running Windows NT5 Other versions will run most, but not all, examples
Intel Pentium CPU (or equivalent: AMD, 486, ...) Alternative: Itanium
Memory and free disk space As required by your development system
C compiler and development system Microsoft Visual C++ Version 6.0 (or higher)
.net
These requirements are easy to meet with current system prices and common configurations
13JMH Associates © 2004, All rights reserved
GETTING STARTED:GETTING STARTED:Windows PRINCIPLES (1 OF 2)Windows PRINCIPLES (1 OF 2)
GETTING STARTED:GETTING STARTED:Windows PRINCIPLES (1 OF 2)Windows PRINCIPLES (1 OF 2)
Nearly every resource is an “object” identified and referenced by a “handle” of type HANDLE
Kernel objects must be manipulated by WindowsAPIs
HANDLE datatype objects include: files pipes processes memory mapping threads events, mutexes, semaphores
Windows is rich and flexible Many functions perform the same or similar operations Each function has numerous parameters and flags
14JMH Associates © 2004, All rights reserved
GETTING STARTED:GETTING STARTED:Windows PRINCIPLES (2 OF 2)Windows PRINCIPLES (2 OF 2)
GETTING STARTED:GETTING STARTED:Windows PRINCIPLES (2 OF 2)Windows PRINCIPLES (2 OF 2)
Windows thread is the basic unit of execution, rather than a process
A process can contain one or more threads Each process has its own code and data address space Threads share the process address space Threads are “lightweight” and more efficient than
processes Used for servers, asynchronous I/O, ...
15JMH Associates © 2004, All rights reserved
Windows NAMING CONVENTIONSWindows NAMING CONVENTIONSWindows NAMING CONVENTIONSWindows NAMING CONVENTIONSLong and descriptive
WaitForSingleObject WaitForMultipleObjects
Predefined descriptive data types in upper case BOOL, DWORD, LPDWORD, ...
Predefined types avoid the * operator and make distinctions:
LPTSTR (defined as TCHAR *) and LPCTSTR (defined as const TCHAR *)
Variable names in API descriptions use “Hungarian” notation - we’ll avoid this convention
lpFileName — long pointer [to a zero terminated string]
16JMH Associates © 2004, All rights reserved
Windows PROGRAMMING Windows PROGRAMMING CONVENTIONSCONVENTIONS
Windows PROGRAMMING Windows PROGRAMMING CONVENTIONSCONVENTIONS
<windows.h> is always included
All objects identified by variables of type HANDLE CloseHandle function applies to (nearly) all objects
Symbolic constants and flags which explain their meaning INVALID_HANDLE_VALUE and GENERIC_READ
ReadFile, WriteFile, and many other Windows functions return Boolean values
System error codes obtained through GetLastError ()
C library always available But you cannot fully exploit Windows with it
17JMH Associates © 2004, All rights reserved
EXAMPLE: Windows FILE COPY (1 of EXAMPLE: Windows FILE COPY (1 of 3)3)
EXAMPLE: Windows FILE COPY (1 of EXAMPLE: Windows FILE COPY (1 of 3)3)
/* Basic cp file copy program *//* cp file1 file2: Copy file1 to file2 */
#include <windows.h> /* Always required for Windows */#include <stdio.h>#define BUF_SIZE 256 /* Increase for faster copy */int main (int argc, LPTSTR argv []){
HANDLE hIn, hOut; /* Input and output handles */DWORD nIn, nOut; /* Number bytes transferred */CHAR Buffer [BUF_SIZE];if (argc != 3) {
printf ("Usage: cp file1 file2\n");return 1;
}
18JMH Associates © 2004, All rights reserved
EXAMPLE: Windows FILE COPY (2 of EXAMPLE: Windows FILE COPY (2 of 3)3)
EXAMPLE: Windows FILE COPY (2 of EXAMPLE: Windows FILE COPY (2 of 3)3)
/* Create handles for reading and writing. Many *//* default values are used */
hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hIn == INVALID_HANDLE_VALUE) {
printf ("Cannot open input file\n");return 2;
}hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hOut == INVALID_HANDLE_VALUE) {
printf ("Cannot open output file\n");return 3;
}
19JMH Associates © 2004, All rights reserved
EXAMPLE: Windows FILE COPY (3 of EXAMPLE: Windows FILE COPY (3 of 3)3)
EXAMPLE: Windows FILE COPY (3 of EXAMPLE: Windows FILE COPY (3 of 3)3)
/* Input and output file handles are open. *//* Copy file. Note end-of-file detection */
while (ReadFile (hIn, Buffer, BUF_SIZE,&nIn, NULL) && nIn > 0)
WriteFile (hOut, Buffer, nIn, &nOut, NULL);
/* Deallocate resources, such as open handles */CloseHandle (hIn); CloseHandle (hOut);return 0;
}
20JMH Associates © 2004, All rights reserved
Getting Ready for Win64Getting Ready for Win64Getting Ready for Win64Getting Ready for Win64
Objectives: Win32 binaries run in 64-bit environment Source code can be recompiled for 64-bit environment
Cautions: Do not assume integers and pointers are same length
Win64 introduces 64-bit pointers
New data types include DWORD32, DWORD64 POINTER_32, POINTER_64 LONG32, LONG64
21JMH Associates © 2004, All rights reserved
LAB 1–A (1 of 2)LAB 1–A (1 of 2)LAB 1–A (1 of 2)LAB 1–A (1 of 2)
Use the VC++ environment Build, run, and test the Windows file copy program, cpW Extend the program so that it prints the value of the error
message in case of any failure Obtained from GetLastError()
Don’t forget to test this error reporting capability
The source code is in Chapter1\cpw.c
22JMH Associates © 2004, All rights reserved
LAB 1–A (2 of 2)LAB 1–A (2 of 2)LAB 1–A (2 of 2)LAB 1–A (2 of 2)
The instructor will show you how to: Create a console application under Microsoft Visual C++ Execute the application Use Visual C++ to edit and rebuild the program Use the Visual C++ debugger Use the online help
Note: http://world.std.com/~jmhart/wined3.htm contains many explanatory comments, examples, diagrams, and book errata