Windows Programming
Total Page:16
File Type:pdf, Size:1020Kb
Windows Programming en.wikibooks.org December 29, 2013 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A URI to this license is given in the list of figures on page 221. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 219. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 225, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 221. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www. pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z. To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX source itself was generated by a program written by Dirk Hünniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. Contents 1 Windows System Architecture 3 1.1 History ...................................... 3 1.2 Windows Kernels ................................ 3 1.3 System Architecture ............................... 3 1.4 Windows Does It ................................ 5 2 User Mode vs Kernel Mode 7 2.1 Virtual Memory ................................. 8 2.2 User Mode .................................... 9 2.3 Kernel Mode, Interrupts, and System Calls .................. 9 2.4 Context Switching ................................ 10 2.5 Next Chapter .................................. 10 3 C and Win32 API 11 3.1 C and Windows ................................. 11 3.2 Visual Basic and Windows ........................... 11 3.3 COM and Windows ............................... 11 3.4 Other Languages ................................ 12 3.5 Next Chapter .................................. 12 4 <Windows.h> 13 4.1 windows.h .................................... 13 4.2 Child Header Files ............................... 13 4.3 Additional Header Files ............................. 13 4.4 windows.h Macros ................................ 13 4.5 Next Chapter .................................. 14 5 Handles and Data Types 15 5.1 Hungarian Notation ............................... 15 5.2 LPVOID ..................................... 15 5.3 DWORD, WORD, BYTE ........................... 16 5.4 LONG, INT, SHORT, CHAR ......................... 17 5.5 STR, LPSTR .................................. 18 5.6 TCHAR ..................................... 18 5.7 TSTR, LPTSTR ................................. 19 5.8 HANDLE .................................... 19 5.9 WPARAM, LPARAM ............................. 20 5.10 Next Chapter .................................. 21 III Contents 6 Unicode 23 6.1 Introduction to Unicode ............................ 23 6.2 Windows Implementation ............................ 24 6.3 Unicode Environment .............................. 24 6.4 TEXT macro .................................. 25 6.5 Unicode Reference ................................ 26 6.6 Next Chapter .................................. 27 7 Dynamic Link Libraries 29 7.1 Dynamic link libraries .............................. 29 7.2 __declspec ................................... 30 7.3 DllMain ..................................... 31 7.4 Linking a DLL .................................. 32 7.5 Next chapter ................................... 33 8 Programming Windows With OSS Tools 35 8.1 Getting an Open Source Compiler ....................... 35 8.2 GCC, DJGPP, MinGW and others ...................... 36 8.3 Cygwin ...................................... 37 8.4 GNU Tools .................................... 39 8.5 Next Chapter .................................. 39 9 Resource Scripts 41 9.1 What is a Resource Script ........................... 41 9.2 Types of resources ................................ 41 9.3 Making a Resource Script ........................... 42 9.4 Using a Resource ................................ 42 9.5 MAKEINTRESOURCE ............................ 42 9.6 Next Chapter .................................. 43 10 Message Loop Architecture 45 10.1 WinMain ..................................... 45 10.2 Create Windows ................................. 46 10.3 Message Loop .................................. 47 10.4 The Window Procedure ............................. 48 10.5 Messages ..................................... 49 10.6 Next Chapter .................................. 50 11 Interfacing 51 11.1 The Keyboard .................................. 51 11.2 The Mouse .................................... 53 11.3 The Timer .................................... 54 11.4 Next Chapter .................................. 56 12 Window Creation 57 12.1 WNDCLASS ................................... 57 12.2 Creating Windows ................................ 59 12.3 An Example ................................... 60 12.4 -EX members .................................. 62 IV Contents 12.5 Dialog Boxes ................................... 62 12.6 Default Window Classes ............................ 62 12.7 Menus ...................................... 63 12.8 Next Chapter .................................. 67 13 User Interface Controls 69 13.1 Standard Windows Controls .......................... 69 13.2 Common Controls ................................ 76 13.3 Next Chapter .................................. 80 13.4 References .................................... 80 14 GDI and Drawing 81 14.1 Device Contexts ................................. 81 14.2 Brush object ................................... 81 14.3 Pen object .................................... 82 14.4 Font object .................................... 83 14.5 Basic Drawing .................................. 83 14.6 Metafiles ..................................... 84 14.7 Next Chapter .................................. 84 15 Dialog Boxes 85 15.1 MessageBox ................................... 85 15.2 Dialog Box Procedures ............................. 87 15.3 Creating Modal Dialog Boxes ......................... 87 15.4 Creating Modeless Dialog Boxes ........................ 88 15.5 Without Class .................................. 88 15.6 With Class .................................... 89 15.7 Common Dialog Boxes ............................. 89 15.8 Dialog Box Resources .............................. 91 15.9 Next Chapter .................................. 92 16 Input-Output 93 16.1 File API ..................................... 93 16.2 Console API ................................... 97 16.3 Device IO API .................................. 98 16.4 Completion Ports ................................ 99 16.5 Next Chapter .................................. 99 17 File Management 101 17.1 File Attributes .................................. 101 17.2 Copying, Moving and Renaming Files . 101 17.3 Deleting Files .................................. 103 17.4 Directories .................................... 103 17.5 File Enumeration ................................ 104 17.6 Locking Files ................................... 104 17.7 Encrypting Files ................................. 104 17.8 Compressing Files ................................ 104 17.9 Next Chapter .................................. 104 V Contents 18 Memory Subsystem 105 18.1 Windows Memory ................................ 105 18.2 Virtual Memory Subsystem . 106 18.3 Heap Memory .................................. 106 18.4 Global Memory ................................. 107 18.5 Local Memory .................................. 108 18.6 Next Chapter .................................. 108 19 Multitasking 109 19.1 Processes and Threads ............................. 109 19.2 Next Chapter .................................. 112 20 Interprocess Communication 113 20.1 Pipes ....................................... 113 20.2 Mailslots ..................................... 113 20.3 Sockets ...................................... 113 20.4 Next Chapter .................................. 113 21 MDI Programs 115 21.1 Steps to Making an MDI ............................ 115 21.2 Register the Window Classes . 115 21.3 Modify the Message Loop . 115 21.4 The Frame Window ............................... 116 21.5 The Client Window ............................... 116 21.6 MDI Child Windows .............................. 116 21.7 The "Window"Menu .............................. 116 21.8 Next Chapter .................................. 117 22 Registry API 119 22.1 The Registry ................................... 119 22.2 Opening a Key .................................. 119 22.3 Values ...................................... 119 22.4 Key Hierarchy .................................. 120 22.5 Next Chapter .................................. 120 23 Security API 121 23.1 Security Overview ................................ 121 23.2 User Permissions ...............................