Porting to Win32 1M Springer-Science+Business Media, LLC Thomas Lauer Porting to Win32™ A Guide to Making Your Applications Ready for the 32-Bit Future of Windows™ With 67 Figures DOS Diskette Included Springer Thomas Lauer Jm Bruehl17 55288 Udenheim Germany This is a translation from the German Die 32-Bit-Expedition: Win 32,TM Windows™ 4.0 und Windows NT. TM LeitJaden und Referenz zur Portierung van Windows 3.x-Programmen, published by Springer-VerIag Berlin © 1993. Windows, Win32, Win32s, and Windows 95 are registered trademarks of Microsoft. Windows NT is a trademark of Microsoft Corporation. Library of Congress Cataloging-in-Publication Data. Lauer, Thomas. Porting to Wm32 / Thomas Lauer. p. cm. Includes bibliographical references and index. ISBN 978-0-387-94572-9 1. Application software. 2. Microsoft Wm32. 1. TItIe. QA76.76.A65L37 1995 005.26-dc20 95-23946 CIP Printed on acid-free paper. © 1996 Springer Science+Business Media New York Originally published by Springer-Verlag New York, Ine. in 1996 AlI rights reserved. This worlc may not be translated or copied in who1e or in part without the written permission of the publisher (Springer Science+Busmess Media, LLC), except for brief excerpts in connection with reviews or scholarly aoaIysis. Use in connection with any form of information storage and retrievai, electronic adaptation, computer software, or by similar or dissimilar m.ethodology now known or hereafter developed is forbidden. The use of general descriptive names, 1rade names, trademarks, etc., in this publication, even if the former are not especially identified, is not to be taken as a sign 1hat such names, as lmderstood by the Trade Marlcs and Merchandise Ma1Xs Act, may accordingly be used freely by anyone. Production managed by Frederick H. Bartlett and Terry Komak; manufacturing supervised by Jeffrey Taub. Photocomposed copy prepared from the author's Microsoft Word files. 987654321 Additional material to tbis book can be downloaded rrom bttp://extras.springer.com. ISBN 978-0-387-94572-9 ISBN 978-1-4612-0727-6 (eBook) DOI 10.1007/978-1-4612-0727-6 Contents Contents at a Glance Introduction and Overview. .............•....•.•...........•........................ 1 1 Fundamental Aspects and Preliminary Considerations .......... 7 1.1 Windows: 3.x (16 Bit) versus NT and 95 (32 Bit) .......................... 7 1.2 Win32 and the Competition ........................................................... 17 1.3 Basic Architecture of Windows NT .............................................. 22 1.4 Basic Architecture of Windows 95 ............................................... .40 1.5 What Changes on My Source Codes are Required to Support the Win32 API? .................................................................. 44 1.6 A Look on the Win32 SDK. ............................................................5 8 1.7 Is My Application Substantially Improved After Porting to Win32? ........................................................................................... 62 1.8 ... And What About the Effort? .................................................... 66 1.9 The Little Brother: Win32s ............................................................. 71 1.10 Summary ........................................................................................7 8 2 Strategies to Portable Programming ....................................... 81 2.1 Starting Points and Objectives ...................................................... 81 2.2 Two Alternatives for Porting .........................................................8 9 2.3 Methodical Advice to Planning a Port.. .......................................9 3 2.4 Tools and Other Assistance ...........................................................9 8 2.5 STRICT Software, Portable Software ......................................... 105 2.6 Naming of Variables: The Hungarian Variant... ....................... 113 2.7 Which Platform for Windows 3.1: Win32s or Win16? ............. 122 2.8 Some Hints for Porting OS/2 Programs .................................... 126 2.9 Summary ........................................................................................ 132 3 Portable Programs in C and C++ ............................................ 135 3.1 Some Fundamental Considerations ........................................... 135 3.2 ANSI C, What Else? ...................................................................... 139 3.3 The C Pre-processor ...................................................................... 142 3.4 Simple Data Types ........................................................................ 149 3.5 Structured Data ............................................................................. 159 3.6 Pointer Subtleties ........................................................................... 171 3.7 Coding Practice ............................................................................. 175 3.8 Compiler Warnings and Error Messages ..................................1 78 3.9 And What about C++? ................................................................. 197 v Contents 4 PoMable Windows Programming ........................................... 201 4.1 Some Signposts for Portable Windows Programs ................... 201 4.2 Using the Enlarged Win32 Data Types ..................................... 205 4.3 WINDOWSX.H: A Way to Portable Programs ........................ 212 4.4 Message Processing and Parameter Packing ............................ 227 4.5 Portable Macros to Communicate with Child Windows ........ 234 4.6 Changes in the Base Functionality ............................................. 241 4.7 Changes in the Window Management ...................................... 262 4.8 Changes and Improvements in the Graphics Device Interface (GDI) ................................................................................ 278 4.9 The Programming of DLLs ......................................................... 290 4.10 Unicode: Plus and Minus .......................................................... 300 4.11 Some Guidelines for Binary Compatible Win16 Applications .................................................................................... 309 4.12 A Study Trip: From MS-DOS to Win32 ................................... 314 5 Win32 Development Tools and Their Use .................................................................................. 319 5.1 The Creation of Win32 GUI Applications ................................. 319 5.2 Particularities for Text Mode Programs .................................... 336 5.3 DLLs: What Has Been Modified? ............................................... 339 5.4 What Do the Borland Tools Offer? ............................................ 349 5.5 Latest News: Visual C++ 2 and Win32 Enhancements ........... 351 Appendices .............................................................................. 355 Al Important Data Types Compared' ............................................. 355 A2 PORT.INI: Enhanced and Clarified ........................................... 359 A3 The Message Cracker Signatures ............................................... 369 A4 The Example Program Used to Demonstrate Warnings and Error Messages ....................................................................... 379 A5 The New _stdcall Calling Sequence ......................................... 387 A6 Some Hints for Pascal and Modula-2 ........................................ 393 A7 The Contents of the Enclosed Disk ............................................ 401 Annotated Bibliography .......................................................... 403 Glossary ................................................................................... 409 Index ......................................................................................... 421 VI Contents Table of Contents Introduction and Overview ........................... 1 1 Fundamental Aspects and Preliminary Considerations .............................................. 7 1.1 Windows: 3.x (16 Bit) versus NT and 95 (32 Bit) ....................... 7 Old: Windows 3.x ....................................................................................8 And new: Windows NT ....................................................................... 10 And what about Windows 95? ............................................................ 14 1.2 Win32 and the Competition ....................................................... 17 IBM and OS/2 ........................................................................................ 17 Xas UNIX ............................................................................................... 19 The most important technical features in comparison ....................2 1 1.3 Basic Architecture of Windows NT .......................................... 22 The NT executive .................................................................................. 23 Basic kernel features ............................................................................. 25 The NT subsystems ............................................................................... 31 MS-DOS and Win16 support ............................................................... 32 Big boss: the Win32 subsystem ...........................................................3 4 Communication with the Win32 subsystem ..................................... 37 1.4 Basic Architecture of Windows 95 ........................................... 40 Virtual: machine manager and device drivers ................................. .41 Two personalities: Win95 system OLLs ............................................. 42 1.5 What Changes on My Source Codes are Required to Support the Win32 API? ........................................................................... 44 The linear 32-bit address space ........................................................... 45 Separated address spaces, isolated applications ............................. .48 User input: Mouse and keyboard ....................................................... 51 GDI coordinates and company ........................................................... 54 MS-DOS and CPU specifics .................................................................5 5 Using undocumented features ............................................................5 5 Difficulties in OLL programming .......................................................5 7 Conclusions ............................................................................................5 8 VII Contents 1.6 A Look on the Win32 SDK ..............................................•.......... 58 Win32 SDK: a single version for NT and 95 ..................................... 59 Programming with the SDK ............................................................... 60 1.7 Is My Application Substantially Improved After Porting to Win32? ....................................................................................... 62 Just porting? .......................................................................................... 63 Or immediately include improvements? .......................................... 64 1.8 ... And What About the Effort? ................................................. 66 Some clues, anyway ............................................................................. 70 1.9 The Little Brother: Win32s ........................................................ 71 What is Win32s and what does it do? ............................................... 71 Pluses and minuses for the developer ............................................... 75 1.10 Summary .................................................................................... 78 2 Strategies to Portable Programming ......... 81 2.1 Starting Points and Objectives ................................................. 81 Preferably STRICT ............................................................................... 82 At the very least, fully protected mode compatible! ....................... 84 K&R or ANSI C? ................................................................................... 85 And the goal? Either one-way porting ............................................. 86 ... Or programming for Win16 and Win32? ..................................... 87 2.2 Two Alternatives for Porting ..................................................... 89 Bottom up .............................................................................................. 89 Top down ............................................................................................... 89 Which method is better? ...................................................................... 90 2.3 Methodical Advice to Planning a Port ..................................... 93 Big projects require some planning ................................................... 93 Self-defined guidelines and helper libraries ..................................... 95 2.4 Tools and Other Assistance ..................................................... 98 Add-on libraries as helpers ................................................................. 98 And what about class libraries for C++? ........................................... 99 More tools: PORTTOOL and the SDK documentation ................. 101 Tools for developers coming from OS/2 ........................................ 103 CompuServe, a gold mine of information ...................................... 104 2.5 STRICT Software, Portable Software ..................................... 105 Okay, STRICT! But what's the best way? ........................................ 107 Some hints for C++ programmers ................................................... 111 VIII Contents 2.6 Naming of Variables: The Hungarian Variant ........................ 113 Advantages of Hungarian notation .................................................. 113 More to the utilization ........................................................................ 115 Some suggestions ................................................................................ 118 2.7 Which Platform for Windows 3.1: Win32s or Win16? ........... 122 Pro Win32s ........................................................................................... 123 And contra? .......................................................................................... 124 2.8 Some Hints for Porting OS/2 Programs ................................. 126 Tools and methods of approach ........................................................ 127 Some hints for text-mode programs ................................................. 129 Porting OS/2 PM applications .......................................................... 130 2.9 Summary ................................................................................... 132 3 Portable Programs in C and C++ .............. 135 3.1 Some Fundamental Considerations ....................................... 135 3.2 ANSI C, What Else? ................................................................. 139 The most important advantages of ANSI C .................................... 139 A few words about Pascal and Modula-2 ....................................... 140 3.3 The C Pre-processor ................................................................ 142 Some simple tricks with #define ....................................................... 142 Complex macro definitions: a dash of portability .......................... 146 3.4 Simple Data Types ................................................................... 149 Own data type definitions ................................................................. 150 ... and related problems ..................................................................... 153 About erroneous casts and other wrongdoers ................................ 154 May I introduce myself: sizeofO ....................................................... 156 Always good for a surprise: BOOL! ................................................. 157 3.5 Structured Data ........................................................................ 159 Unions: memory economy, shady maneuvers or what? ............... 160 Structured use of structs .................................................................... 162 Structures and memory layout ......................................................... 166 3.6 Pointer Subtleties .................................................................... 171 FAR? NEAR? Neither-nor: FLAT! ............................................... 171 Segment and offset arithmetics: hands off! ..................................... 173 IX Contents 3.7 Coding Practice ....................................................................... 175 Macros, prototypes, and libraries .................................................... 175 3.8 Compiler Warnings and Error Messages .............................. 178 Warnings and errors in detaiL ........................................................ 179 3.9 And What about C++? ............................................................. 197 4 Portable Windows Programming ............. 201 4.1 Some Signposts for Portable Windows Programs ............... 201 Syntax versus semantics: two perfect examples ............................ 202 Windows portability: an overview .................................................. 203 4.2 Using the Enlarged Win32 Data Types .................................. 205 WORD, the second ............................................................................ 205 Where does the pointer point? ......................................................... 207 New polymorphic types: LPARAM and WPARAM .................... 211 4.3 WINDOWSX.H: A Way to Portable Programs ........................ 212 The simple helper macros ................................................................. 213 To the fullest: message crackers ....................................................... 215 Examples: WM_COMMAND .......................................................... 217 ... and WM_MOUSEMOVE .............................................................. 221 The most important benefit: portability .......................................... 224 4.4 Message Processing and Parameter Packing ...................... 227 The little brothers of the message crackers ..................................... 228 Helper macros to reassemble the message parameters ................ 233 4.5 Portable Macros to Communicate with Child Windows ....... 234 Some common definitions ................................................................. 235 Class-specific macros ......................................................................... 236 4.6 Changes in the Base Functionality ........................................ 241 The access to data of preceding instances ....................................... 241 Memory management: the local and global heaps ........................ 245 Solved in a completely different way: shared memory ................ 249 A ticket to the 1/ 0 system and back, please!. ................................. 251 Long or short: the treatment of filenames ....................................... 255 INI files and registry .......................................................................... 258 4.7 Changes in the Window Management ................................... 262 The calling sequence of window procedures ................................. 263 DOE and colleagues ........................................................................... 264 Cooperation with other programs ................................................... 269 x
Description: