14. Advanced Windows Concepts
Total Page:16
File Type:pdf, Size:1020Kb
• multi-tasking or multi-threading of a single application, which 14. Advanced Windows Concepts allow parts of an application to still move ahead with useful This section will survey some advanced concepts used in MS- computations even though other parts of the same application Windows programming. First, we will look at a number of are blocked. forms of Inter-Process Communication (IPC). IPC is the generic Optional Readings: the rest of the chapters from [Shildt95] term used for passing data between different processes, programs, or threads: • The clipboard can be used to cut and paste not only within one application, but between applications. • Drag-And-Drop is a feature for dragging a file to an application window or icon, and having the destination application know what to do with it (and if not running, to be started to deal with it). • Dynamic Data Exchange (DDE) is a simple way for a data server process to allow access by client processes. • Object Linking and Embedding (OLE) has developed past simple embedding another application’s objects (e.g. diagrams or tables) in a document. • Component Object Model (COM) is the underlying mechanism by which the sophisticated features of OLE can work; in fact COM has recently been extended to provide inter- communication between application running on different computers (‘Distributed COM’). We will also briefly look into Dynamic Link Libraries, which allow run-time linking to application or system procedure libraries. Finally, if time permits, we may discuss: • how some of the complications of Windows programming can be abstracted and simplified using a C++ OO class library of various window and control types. 14-1 14-2 14.1 The Clipboard TABLE OF CONTENTS: The clipboard is commonly used to cut and paste data from one 14. Advanced Windows Concepts................................................. 14-1 window to another. This works well between windows of the 14.1 The Clipboard............................................................................................... 14-4 14.1.1 What Is The Clipboard? .................................................................. 14-4 same application because they use the same format to represent 14.2 File Drag And Drop...................................................................................... 14-6 the data. Think, for instance, of a word processor which must 14.3 Dynamic Data Exchange.............................................................................. 14-8 transfer bold, italicized, underlined, colored text of various fonts 14.3.1 Client/Server Architecture .............................................................. 14-8 from one portion of a document to another (or from one 14.3.2 Connections..................................................................................... 14-8 document to another in the same application, or from one 14.3.3 DDE Scenarios.............................................................................. 14-10 program instance of an application to another instance of the 14.4 Object Linking and Embedding................................................................ 14-13 14.4.1 OLE 1.0......................................................................................... 14-13 same application). The representation which communicates the 14.5 Dynamic Link Libraries ............................................................................ 14-15 text and formatting must be understood by the destination 14.6 OLE 2 and COM ........................................................................................ 14-18 window, otherwise the clipboard data transfer result would be 14.6.1 Component Object Model............................................................. 14-19 garbage. 14.6.2 GUIDs ........................................................................................... 14-20 14.6.3 Component Interfaces ................................................................... 14-21 Windows also facilitates cutting from one application’s window 14.6.4 Interface = Pointer to Table of Pointers to Functions ................... 14-23 and then pasting the data into another application’s window. 14.6.5 Remote Procedure Calls................................................................ 14-26 14.6.6 COM Classes/Objects ................................................................... 14-27 This works as long as the destination window’s application can 14.6.7 Other Elements of OLE 2.............................................................. 14-28 properly interpret the data representation. Even plain 14.7 Multiple Document Interface .................................................................... 14-31 unformatted text can be difficult, because foreign language 14.8 GUI Class Libraries ................................................................................... 14-32 applications often use a multibyte (16 bit per character) 14.8.1 Borland Object Windows Library (OWL) .................................... 14-32 representation to store special characters which have accent 14.8.2 Microsoft Foundation Classes (MFC)........................................... 14-34 marks. 14.9 References ................................................................................................... 14-39 14.1.1 What Is The Clipboard? The clipboard is region of shared global memory managed by the GUI OS for cut and paste purposes. There are about 16 functions in the clipboard API. Before writing to the clipboard, an application must: 1) First, obtain rights to a hunk of shared global memory. 2) Then ‘lock’ that region, so that no other application can read or write to it while your application is writing to it. If another application were to read it when it was only half written by your application, it might read half an integer that was written and the other half garbage from what was previously in 14-3 14-4 the memory. As discussed in any operating systems course, two 14.2 File Drag And Drop applications both writing to shared memory at the same time is a recipe for problems. File drag-and-drop is a feature which allows a user to drag a file using the mouse, and drop it on (i.e. release the mouse button When finished with the clipboard, your application must unlock, over) some other application icon. and give up your pointer/rights to it before closing the application, so that other applications can subsequently use the When an application implements the drag-drop feature, a user clipboard memory. can select one or more files in File Manager, drag them to a non- running application’s icon or running application’s window Any number of clipboard viewers can be monitoring and (whether it is fully visible, or minimized to an icon), and drop displaying the contents of the clipboard memory at the same them there. The application in which the files were dropped is, if time (cf. MVC - model and multiple viewers), though this is necessary started and receives a WM_DROPFILES message. feature is seldom used. This allows the target application to obtain the file name(s) and the coordinates of the point at which the files were dropped. The file drag-and-drop API contains 4 functions. Before an application can accept dropped files, it must first call the DragAcceptFiles() function to register its interest in receiving WM_DROPFILES messages in a particular window(s). The WM_DROPFILES message contains the handle of an internal data structure which the application can query, using DragQueryFile(), to find out the number and name(s) of the files that were dropped. The DragQueryPoint() function returns the window coordinates of the cursor when the user released the mouse button, in case it is important to the destination application whether the file was dropped, say, in the left or right portion of the window. To free the memory allocated by the system for the WM_DROPFILES messages, an application should call the DragFinish() function when it is no longer interested in accepting drops. Here is a simple example. Recall that a window is sent a WM_CREATE message by Windows right after being created, and a WM_DESTROY message when it is being closed. This allows a window to prepare itself in many ways (registering 14-5 14-6 interest in accepting drops is only one), and to gracefully wind 14.3 Dynamic Data Exchange its life up: DDE was the first, and is the simplest way for programs/ processes/threads to exchange data without user intervention case WM_CREATE: DragAcceptFiles(hwnd, TRUE); (recall that the clipboard and drag-and-drop required the user to break; perform some action). DDE is thus an excellent example of IPC. DDE is simpler than OLE, so we will look at it first. case WM_DROPFILES: //handle dropped file(s). There is also a new API called DDE Management LIbrary break; (DDEML) that makes DDE even simpler. We will not study case WM_DESTROY: DDEML so as to concentrate on the fundamentals. DragFinish(hwnd, FALSE); break; 14.3.1 Client/Server Architecture DDE is based on a client/server architecture. A server is a program/process/thread that provides services to clients. In the You should know that there is another, more advanced form of case of DDE, a server is the process with data that some other drag-and-drop available with Object Linking and Embedding. It process wants. It is clients which initiate a connection. allows you to drag an object (e.g. wordprocessor paragraph, drawing tool drawing, spreadsheet table) and place it on any It is possible for a server to handle connections from several