Bailey O.H.Embedded systems.Desktop integration.2005
.pdf90 |
Chapter 4 / Designing Cross-Platform User Applications |
|
|
The Windows Application Programming
Interface (API)
We covered the architectural differences between Microsoft Windows and UNIX in the previous chapter. Windows is difficult for developers coming from other environments to understand. The reason for this long learning curve is due to the fact that a Windows application can have many windows within it and each window is an independent object that processes messages, events, and data.
Traditionally, programs were written with a main loop that handled data, messages, and events, so becoming accustomed to this fundamental difference takes time. To implement this object-oriented approach, Microsoft makes use of callback routines. Callback routines are functions that reside within the application that are called from outside the application by the OS or another program. This feature makes a Windows application completely event driven. This in and of itself is not a difficult concept to understand, but when you have several windows in an application the method of gluing each of these objects together is not terribly obvious or intuitive.
There are many tools on the market to assist in developing Windows applications. You can write all of your code the oldfashioned way with any text editor, or you can use a visual development product like Visual Studio, Delphi, or C++Builder. Manually writing Windows API code is not on my top ten list of things I want to do. It can take hundreds or even thousands of lines of code to accomplish even simple tasks in Windows. With that in mind let’s look at how Windows, UNIX, and Linux user applications compare functionally, and then explore cross-plat- form development options to make the most of time and tools.
Chapter 4 / Designing Cross-Platform User Applications |
91 |
|
|
Windows Application Programming
Layers
The Dynamic-Link Library (DLL)
Microsoft has taken a unique approach to distributed computing. First, they introduced the dynamic-link library (DLL). This is a shared code library that allows a single instance of code to support many applications at the same time. This conserves the use of memory in exchange for the overheard of switching between different application stacks that may be using the library simultaneously. In short, you save memory at the expense of speed. Since time-critical portions of code normally run at the driver or OS level, this usually has little effect on overall program performance. A counter keeps track of how many applications are using the library, and when the count reaches 0 the library is closed and eventually released from memory.
The Visual Basic Control (VBX)
Because of the amount of labor it took to build good Windows code, Microsoft responded with the VBX control architecture. This allowed developers to write controls that could be used in any language that supported the VBX package architecture.
While it didn’t reduce the amount of labor it took to build program components, it did allow the components to be included in other programs while not permitting the original code to be tampered with. The architecture was slow to be accepted by the development community due to its complexity.
Chapter 4
92 |
Chapter 4 / Designing Cross-Platform User Applications |
|
|
The ActiveX Controls
VBX was eventually repackaged and expanded to support 32-bit Windows platforms and to be more easily imported into other programming languages and environments through the use of a visual interface.
The Component Object Model (COM)
Next, Microsoft added interprocess communications through the use of remote procedure calls (RPCs). This allowed a nonvisual means of communicating with a control or other component such as a function or method. It also provided a means to use or control a component, function, method, or complete application. Since RPC is a network protocol, it became the foundation for allowing network users access to COM objects.
Network COM (COM+)
The COM (or COM+) mode added the ability to control and access objects across a network or the Internet. A separate technology known as Microsoft Message Queuing (MSMQ) was integrated into the COM+ layer, allowing remote transaction handling to be queued or managed even in an offline situation.
The .NET Specification
In 2001 Microsoft introduced the .NET (pronounced “dot-net”) specification. The .NET framework was the result of that specification and the first step in accomplishing several goals. First, it introduced developers to the “managed code” architecture. Managed code allows the operating system to chaperone the execution of programs and shared libraries by managing the memory space they reside in. This eliminates the need for a programmer to manually allocate and free memory, thus eliminating memory management problems that have plagued Windows development
Chapter 4 / Designing Cross-Platform User Applications |
93 |
|
|
over the years. .NET compiles code at run time rather than at compile time. Until .NET, most development languages for Windows generated machine-executable code. With .NET, however, an intermediate file is generated. This intermediate file is then translated into machine-executable code when the program is executed. One obvious advantage of this is the ability to create a single intermediate program file that can be run on any system with a .NET framework installed. This means that you can distribute your program to any system that supports .NET and it will run. Linux, Solaris, UNIX, and even mainframe systems will eventually support the .NET framework. While this is an advantage, there are also a couple of disadvantages. First, the intermediate code can be easily reverse-engineered so anyone can see your program and intellectual property. There are, however, tools available to encrypt the intermediate file so the code cannot be seen. It also takes longer to load the program since it has to be compiled at run time. Not all systems have .NET integrated yet, so your program may also run slower after it has been compiled to machine language.
Using .NET means the compiler vendor will parse their language file and send the resulting code to the Microsoft intermediate compiler. This will all but eliminate some of the advantages that other compiler vendors have gained over the years in speed and performance. When an update is made to the current .NET framework, programs will be able to take advantage of the features immediately and without a new distribution of the program from the vendor. All in all, the .NET strategy does make sense if implemented properly. There are two development projects currently going on to implement .NET on UNIX and Linux. When completed, you should be able to take your Windows program and execute it on UNIX and Windows (if all works as expected). Currently the .NET framework is available for Windows 98, ME, NT, 2000, and XP as a download. Windows Server 2003 and later operating system releases will have .NET integrated into the core OS. The following diagram illustrates how
.NET integrates into existing systems.
Chapter 4
94 |
Chapter 4 / Designing Cross-Platform User Applications |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.NET Development Language |
|
|
|
Intermediate Code |
|
Distributed |
|
|||||
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
Just in Time Compiler |
|
|
|||||||
|
(VB.NET, C#) |
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
Computing |
|
||||
|
|
|
Common Language Runtime (CLR) |
|
|
||||||||
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
Technologies |
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(Delphi, C++Builder |
|
|
|
|
|
|
|
|
|
|
|
|
|
Visual C++) |
|
Windows Executable |
|
|
.NET Framework |
|
|
Remoting |
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Windows 32-Bit API |
|
|
|
COM / COM+ |
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device Interface |
|
|
OpenGL |
|
Windows GDI |
|
|
|
|
|
||
|
Layer |
|
|
Device Drivers |
|
System Services |
|
|
|
Distributed COM |
|
||
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hardware Layer
Figure 4-1
As you can see, Windows has several layers associated with user application development, depending on the development language or platform (native language vs. .NET), and whether local or distributed computing is being used.
The UNIX Application Programming
Model
The UNIX application model is somewhat different from Windows. UNIX was written years before color monitors were available, and graphical user interfaces have only been widely used for the last 15 or so years. Prior to that, suitable hardware was either not available or was simply too expensive for the mass market. This made providing backward compatibility while supporting current input and display technology a unique challenge. How users interacted with the computer was changing. Research at the Xerox Palo Alto labs had introduced a pointing device (mouse), and display technology was becoming both cheaper and more functional in high-resolution color displays.
Chapter 4 / Designing Cross-Platform User Applications |
95 |
|
|
In the early 1980s, MIT introduced the UNIX world to X Windows. Since UNIX was not originally designed as a graphicsbased system, its graphic desktop implemention was developed in layers. The lowest level layer was the display manager or server. The display manager was combined with a graphic server with the task of collecting input from users (keystrokes and mouse data) and responding with the data for those requests. To display the data in a display-independent graphic format, the client was written as a separate program that could be run either on the local machine or on a remote desktop. Separating the display manager and display client allowed a single display manager the ability to handle multiple displays of different height, width, and color depth. The client in this case was responsible for the display parameters and the server was responsible for seeing that input and output requests were handled in an orderly fashion.
A layered approach also provides more of a building block type approach to user application development. Each layer can build upon the previous layers, which allow high-level user interfaces and developer libraries to be implemented. This architecture also allows cross-platform tools and libraries to be more easily developed as shown in Figure 4-2.
Remote Access Methods
|
|
|
Development Tools and |
Kylix / GNU - C / C++ / Pascal IDE |
|
|
|
|
Frameworks |
Qt / wxWindows / Zinc Framework Layer |
|
|
|
|
|
|
|
|
|
|
Graphic Desktop |
Motif / KDE / Gnome / X-Open Desktop Manager |
|
Remote Client / VNC |
|
|||
Managers |
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console Mode Manager |
X Display Manager |
|
|
|
||
System Level Support |
|
|
|
System Services |
|
Telnet / Hardwired |
|
|
Scheduler |
Device Drivers |
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hardware Layer
Figure 4-2
This figure breaks down the different layers of the UNIX display management system. There are many desktop managers
Chapter 4
96 |
Chapter 4 / Designing Cross-Platform User Applications |
|
|
available to the UNIX and Linux user including X Windows, Motif, KDE, and GNOME, to name a few. These desktop managers combined with either a framework or X Windows are the equivalent of the Windows API layer. Qt, wxWindows, and Zinc are just a few examples of portable application frameworks. These frameworks are also available on the Windows platforms, and toolkits are available for Windows developers.
X Windows in Depth — A True
Client/Server GUI
The X Windows server is also known as the X Display Manager (XDM for short) and is used as a functional model for other popular desktop managers like KDE (KDM) and GNOME (GDM). XDM routes graphic I/O to and from the client desktop. A single display manager handles all graphic desktop sessions on that or any remote machine attached to XDM. The client program manages the graphic desktop, mouse, and keyboard input from the user. The client may be run on the local machine, a network machine, or a machine on the other side of the world across the Internet.
Chapter 4 / Designing Cross-Platform User Applications |
97 |
|
|
Figure 4-3
As you can see, a display manager can serve clients on remote machines as easily as it can on the server machine. The UNIX or Linux server task manager communicates with the X Server layer, which is located in the same layer as the physical transport layer. The physical transport layer handles both local and remote clients in addition to keyboard, mouse, and network communications.
Chapter 4
98 |
Chapter 4 / Designing Cross-Platform User Applications |
|
|
Figure 4-4
A remote X Client application appears as just another application when it is running. The only indication the application is actually hosted on a remote machine appears in the upper-left corner of the main application window. The host machine name and display number indicate the application is running remotely. This means that while it appears you have several programs running on your computer, programs connected to remote X Servers are simply using your system for user input and display output. The actual programs are running on the machine in which the X Server is running. It is possible to run as many remote sessions or programs as desired using this method. Display output is configured to the display parameters on the client machine (where the output is displayed). It is also possible to run a client on Windows machines using any X Client/Server designed for Windows. Red Hat’s Cygwin has a free XFree86-based server that I use on Windows 2000 and XP, which works very well. Products that allow an X Windows client on Windows may have the X Server built into
Chapter 4 / Designing Cross-Platform User Applications |
99 |
|
|
the same application as the client. The available X Window client/server products have a wide variety of price ranges and features.
Alternatives to the X Windows Approach
There are alternatives to using the X Client/Server approach. Now you may be asking why a developer would want to use an alternative if this approach is truly efficient on resource usage. The answer lies in how much time you can afford to spend setting up the X environment for remote access and how familiar you are with X Windows setup and administration.
Setting up a suitable X Windows client/server environment can be a very time-consuming task. It requires the altering of setup files on the client and server machines and additional network setup as well. If you are going to use this feature across the Internet, a secure socket layer is strongly recommended, which adds even more complexity to the setup process.
One alternative to X Windows is the Virtual Network Computing product (VNC). VNC was originally developed by Bell Labs of the UK (the former Olivetti development center). VNC also requires some setup on the client and server but is easier to learn and does not require an understanding of how X Windows works. VNC displays an entire desktop rather than a single application window. If you use Secure Sockets, the difference is running a single application versus displaying and accessing an entire desktop session as a client. If user security is an important issue, providing access on an application basis is worth the additional effort and learning curve of X Windows.
Chapter 4