How to Talk To Windows What did it say? 1
DOS was essentially subservient. Your program was the master. When you wanted some service from DOS you called DOS it obeyed. You could even bypass DOS and communicate with the PC hardware directly. Why was this OK? DOS the slave Why is this the case? Well DOS is essentially a set of functions in memory that our code can call on. It provides these services as we call on them. Our program is the only program running at any one time. DOS is not a multiprogramming OS. 2
Windows the boss In Windows the OS is the boss. My programs Can t talk directly to hardware All communications (in/out) with the outside world must pass through Windows The user (of my program) really only talks to Windows Windows passes messages to/from my program Why is this necessary? Why? Windows is a multi-programming OS. Lots of programs are running at the same time. Windows must allocate resources. Windows must protect open files. Windows must stop one program interfering with another programs memory etc Windows must interpret all the data (mouse/keyboard/network data packet etc) that arrives into the Pc and decide which program should respond. Which program should be resized. Which program should display the HTL that just arrived in the N/W card. Equally Windows has a look and feel and when you write code for it you must comply. The is a standard for the interface and Windows is the one who enforces this standard. Windows could not achieve any of this if it was subservient. The designers of Win2K chose a client-server architecture for its core implementation. A user application runs as a client of OS services. The user application runs in a special mode of the hardware known generically as user mode. Within this mode, code is restricted to non-harmful operations. For example, through the magic of virtual memory mapping, code cannot touch the memory of other applications (except by mutual agreement with another application). Hardware I/O instructions cannot be executed. Indeed, an entire class of CPU instructions (designated privileged), such as a CPU Halt, cannot be executed. Should the application require the use of any of these prohibited operations, it must make a request of the operating system kernel. A hardware-provided trap mechanism is used to make these requests. Operating system code runs in a mode of the hardware known as kernel mode. Kernel mode code can perform any valid CPU instruction, notably including I/O operations. Memory from any application is exposed to kernel-mode code, providing, of course, that the application memory has not been paged out to disk. 3
Windows 2000 The Windows 2000 kernel is primarily responsible for the scheduling of all thread activity. Other OS components perform the necessary work of memory, process, security, and I/O management. These components are collectively known as the Executive. 4
Executive components Executive components provide the base services for the Windows 2000 operating system (other than thread scheduling), their needs and responsibilities are fairly clear. These components are: SYSTEM SERVICE INTERFACE OBJECT MANAGER CONFIGURATION MANAGER PROCESS MANAGER VIRTUAL MEMORY MANAGER LOCAL PROCEDURE CALL FACILITY I/O MANAGER ACTIVE DIRECTORY SERVICE 5
Windows 2000 The Hardware Abstraction Layer (HAL) isolates the OS and device driver code from the processor and platform dependencies from. In general, when device driver code is ported to a new platform, only a recompile is necessary. How can this work since device driver code is inherently device-, processor-, and platform-specific? Clearly, the device driver code must rely on code (macros) within the HAL to reference hardware registers and buses. 6
Talking to Windows APIs The Executive components of Windows 2000 define and implement core services of the OS however these services are not directly exposed to user-mode applications. Microsoft defines several Application Programming Interfaces (APIs) that user-mode code treats as abstractions of OS services. 7
Talking to Windows APIs These APIs form different environmental subsystems that application code could live within. The Win32 subsystem (our concern here) Others The Virtual DOS Machine (VDM) subsystem provides a 16-bit MSDOS The Windows on Windows (WOW) subsystem supports an environment for oldstyle 16-bit Windows applications for The POSIX subsystem provides API support for Unix-style applications The OS/2 subsystem creates the execution environment for 16-bit OS/2 applications A given application is tightly coupled to exactly one environmental subsystem. Applications cannot make API calls to other environments/subsystems. Only the Win32 subsystem is native Environmental subsystems are generally implemented as separate user-mode processes. The environmental subsystem becomes the server for the usermode client. The server process (i.e., the environmental subsystem) either performs the work to fulfill the request directly or it, in turn, makes a request of the appropriate Executive component. 8
How to Talk to Windows The 32-bit API is huge. It contains thousands of functions broken up into hundreds of categories. It is a comprehensive, all-encompassing programmer interface to the many capabilities provided by the Windows operating system. The API is the same whether you are using Windows 2000 or Windows 98. However, a number of functions will report "not implemented" errors in Windows 98 because they are not supported. The 32-bit API is so called to distinguish it from the older 16-bit API used by standard Windows 3.1 running on top of MS-DOS. The 16-bit API contains all of the Windows and Graphics functions found in the 32-bit API (that is why most older Windows programs port to Windows 2000 and 95 so easily), but the 16-bit API does not have any of the system services found in the 32-bit API. The 32-bit API also includes a number of operations normally found in the standard C libraries, in an attempt to centralize things. For example, the 32-bit API includes functions to move, zero, and copy blocks of memory 9
Who can we talk to? What the MSDN library says : the Win32 API can be grouped into these functional categories: Window Management Window Controls Shell Features Graphics Device Interface System Services International Features Network Services MSDN library ref: The Win32 API allows applications to exploit the power of the 32 bit Windows family of operating systems. The functions, structures, messages, macros, and interfaces form a consistent and uniform API for all of Microsoft's 32-bit platforms. Using the Win32 API, you can develop applications that run successfully on all platforms while still being able to take advantage of unique features and capabilities of any given platform. 10
Using MSDN Library in Visual C++ This library contains a significant amount of reference material Here I m looking at the File and I/O subsection 11
The system services of the 32-bit API The Win32 API you access to Windows system services Processes and threads Interprocess communication Remote Procedure Calls (RPCs) Network communications Serial communications Graphics Device Interface (drawing and painting)* Windows management functions ( creating GUIs)* * Because of MFC you generally do not access either of these sections directly when creating Windows applications in C++. The system services are the core of the operating system. They give you access to the modern capabilities that make the 32-bit versions of Windows interesting: Processes and threads Synchronization Remote Procedure Calls (RPCs) Event logging Network communications TCP/IP networking with UNIX and other machines File mapping Interprocess communication Compressed file access Network drive and printer sharing Serial communications Services (background processing like UNIX daemons) Object and file security Graphics Device Interface (drawing and painting) Windows management functions ( creating GUIs) The other two major sections of the API are the Graphics Device Interface (GDI) for drawing and printing, and the Window Management functions for creating GUIs. Both of these sections are encapsulated by MFC. You will generally write and compile your Win32 programs using either "the Microsoft Win32 SDK" or Visual C++. The SDK is a command-line environment, while Visual C++ is a graphical environment. Visual C++ (but not the SDK) ships with a C++ class library called the Microsoft Foundation Class library, or MFC. MFC makes the creation of GUI programs much easier. It encapsulates all of the window-management and GDI functions available in the 32-bit API. 12
Services Almost all services offered by the OS are modeled with an object* File access (read and write) are offered through an object COM1 access offered through an object *Note: this is not an OO object! 13
My way of talking to objects Owned By OS My APP This handle points to an Object Object Object Actual System Resource Object ObjectActual System Resource Windows uses the concepts of a handle and an object throughout the system services. An object is owned by the operating system and represents a system resource. A handle points to an object. It is possible to create a single object and refer to it with several different handles. You can think of the handles as pointers that let you access the object. A HANDLE is a generic 32-bit pointer. It tells you, as a programmer, nothing about the object it points to, so you are unable to get into the low-level structures and code directly to them. This allows the operating system to change over time without its designers having to worry about code that breaks the rules. 14
Kernel Objects Some of the " kernel objects" defined by the 32-bit API are listed in the notes. Also listed are the function calls that return handles to these objects Here we see a call the function CreateConsoleScreenBuffer and we see it returns a handle to the console object. Object Function Access tokens OpenProcessToken CloseHandle Console CreateConsoleScreenBuffer CloseHandle Console device GetStdHandle CloseHandle Communication port CreateFile CloseHandle Event CreateEvent CloseHandle Event log OpenEventLog CloseEventLog File CreateFile CloseHandle File change FindFirstChangeNotification FindCloseChangeNotification File mapping CreateFile CloseHandle Find file FindFirstFile FindClose Mailslot CreateMailslot CloseHandle Mutex CreateMutex CloseHandle Named pipe CreateNamedPipe CloseHandle Pipe CreatePipe CloseHandle Process CreateProcess CloseHandle Semaphore CreateSemaphoreCloseHandle Thread CreateThread CloseHandle 15
Looking at the function calls Win32 uses an extended set of data types, using C's typedef mechanism These include; 16
Understanding API function calls 1 1 This API function has two parameters. It really wants the addresses (hence LP) of suitable variables in our program, such that the function can fill those variables with data. char computername[max_computername_length + 1]; DWORD computernamelen = MAX_COMPUTERNAME_LENGTH+ 1; success = GetComputerName(computerName, &computernamelen); if (success) cout << "Computer name: " << computername << endl; 3 3 We accept the returned value and work on 2 2 Here we create suitable variables And tell the API function the variables addresses Argument requires a LPTSTR lpbuffer STR =>We need a string data type variable... we ll use a character array LP => and we need to provide the function with the address of our variable. So we create a (big enough) character array, computer name, of size MAX_COMPUTERNAME_LENGTH (defined in <windows.h> plus 1 for terminating character \0 ) When we call GetComputerName() we can supply the name of the array as the first argument because its a synonym for the address of the array Argument requires a LPDWORD nsize DWORD => We use a type DWORD (defined in <windows.h>) variable (basically an integer) that specifies the max number of characters we can put into the character array LP => and we need to provide a the address of the variable that defines the maxlength of the string that can be placed in the character array. 17
Help on line... 18
Sample Program 19
EXERCISE: Your first API calls Write a C++ program that will ask Windows for the following and then prints it to std::cout name of your computer BOOL GetComputerName(); the type of keyboard int GetKeyboardType(); Use the API help to better understand the API function calls. Remember to include <windows.h> to allow you to make the function call char computername[max_computername_length + 1]; DWORD computernamelen = MAX_COMPUTERNAME_LENGTH+ 1; success = GetComputerName(computerName, &computernamelen); if (success) cout << "Computer name: " << computername << endl; 20
Getting computer name and keyboard information 21