Programming with Lcc-Win
Total Page:16
File Type:pdf, Size:1020Kb
Programming with lcc-win by Jacob Navia and Q Software Solutions GmbH Acknowledgements Thanks to all people that have contributed to this work. Thanks to the many people that sent me those bug reports that allowed me to improve the software. To all that sent me messages of encouragement. Thanks specially to Friedrich Dominicus, John Findlay and Mike Caetano among many other people, that collaborated to make lcc-win what it is today. © 2000-2006 Jacob Navia and Q Software Solutions GmbH. This document is part of the lcc-win documentation. Distribution in any form is explicitly not allowed. Chapter 1 Introduction to C 1 Why learn C? 1 Program organization 2 Hello 3 Program input 4 What are “function parameters” ? 5 Console mode programs and windows programs 6 An overview of the compilation process 6 The run time environment 7 An overview of the standard libraries 9 The “stdheaders.h” include file 9 Passing arguments to a program 10 Iteration constructs 13 for 13 while 14 do 14 Types 15 What is a type? 15 Types classification 16 Integer types 17 Floating types 17 Compatible types 17 Incomplete types 18 Qualified types 18 Casting 18 The basic types 19 Declarations and definitions 20 Variable declaration 21 Function declaration 23 Function definitions 24 Scope of identifiers 24 Linkage and duration of objects 24 Variable definition 25 Statement syntax 25 Errors and warnings 25 Input and output 27 Predefined devices 28 The typical sequence of operations 29 Examples 29 Other input/output functions 34 File buffering 35 Commenting the source code 36 Describing a function 37 Describing a file 38 An overview of the whole language 39 Statements 40 Declarations 43 Pre-processor 45 Windows specific defined symbols 46 Structured exception handling 46 Control-flow 47 Windows specific syntax 47 Extensions of lcc-win 48 A closer view 49 Identifiers. 49 Constants. 49 Arrays. 51 Compound literals 53 Function call syntax 54 Functions with variable number of arguments. 54 Assignment. 55 The four operations 56 Postfix 57 Conditional operator. 57 Struct. 58 Union. 58 Typedef. 58 Register. 58 Sizeof. 59 Enum. 59 Prototypes. 59 Const. 60 Goto 60 Return 61 Unsigned. 62 Bit fields 62 Stdcall. 62 Break and continue statements 63 Null statements 63 Comments 64 Switch statement. 64 Inline 65 Logical operators 65 Bitwise operators 66 Shift operators 67 Address-of operator 67 Sequential expressions 68 Casts 68 Selection 70 Indirection 71 Predefined identifiers. 73 Precedence of the different operators. 73 The printf family 75 Conversions 75 The conversion flags 76 The size specification 77 The conversions 78 Scanning values 79 Pointers 81 Operations with pointers 82 Null pointers 84 Pointers and arrays 84 setjmp and longjmp 85 General usage 85 Register variables and longjmp() 87 Simple programs 89 strchr 89 strlen 89 ispowerOfTwo 90 Write ispowerOfTwo without any loops 91 strlwr 92 paste 93 Using arrays and sorting 97 How to sort arrays 100 Other qsort applications 105 Summary of Arrays and sorting 107 Pointers and references 107 Structures and unions 110 Structures 110 Structure size 113 Defining new types 114 Unions 115 Using structures 117 Fine points of structure use 119 Identifier scope and linkage 120 Top-down analysis 121 Extending a program 124 Improving the design 129 Traditional string representation in C 130 The problems with C-“Strings“ 132 Buffer-overflows 135 A buffer overflow in the C standard document 138 Memory management and memory layout 141 Functions for memory allocation 143 Memory layout under windows 143 Memory management strategies 145 Static buffers 145 Stack based allocation 145 “Arena” based allocation 146 The malloc / free strategy 146 The malloc with no free strategy 147 Automatic freeing (garbage collection). 147 Mixed strategies 148 A debugging implementation of malloc 148 Counting words 152 The organization of the table 153 Memory organization 155 Displaying the results 156 Code review 158 Time and Date functions 158 Using structures (continued) 163 Lists 163 Hash tables 166 A closer look at the pre-processor 168 Preprocessor commands 169 Things to watch when using the preprocessor 172 Using function pointers 174 Advanced C programming with lcc-win 179 Operator overloading 179 References 180 Generic functions 180 Default arguments 181 Structured exception handling 181 The signal function 188 Numerical programming 192 Floating point formats 193 What can we do with those numbers then? 195 Using the floating point environment 203 The status flags 203 Reinitializing the floating point environment 204 Numerical stability 204 Complex numbers 206 The libraries of lcc-win 208 The regular expressions libraries 208 Console formatting routines 208 Statistics library 208 Linear algebra library 208 Network utilities 209 Advanced math functions 209 Compression/decompression functions 209 Structured Query Language (SQL) 209 Safer C Library 209 The regular expressions library. A “grep” clone. 209 Programming with security in mind 213 Always include a ‘default’ in every switch statement 213 Pay attention to strlen and strcpy 213 Do not assume correct input 215 Watch out for trojans 215 Pitfalls of the C language 216 Defining a variable in a header file 216 Confusing = and == 216 Forgetting to close a comment 216 Easily changed block scope. 216 Using the ++ or -- more than once in an expression. 217 Unexpected Operator Precedence 217 Extra Semi-colon in Macros 218 Watch those semicolons! 218 Assuming pointer size is equal to integer size 218 Careful with unsigned numbers 219 Changing constant strings 219 Indefinite order of evaluation 220 A local variable shadows a global one 220 Careful with integer wraparound 220 Problems with integer casting 221 Octal numbers 221 Wrong assumptions with realloc 221 Be careful with overflow 221 Programming style 222 Fun with C 224 Calculating pi 224 Writing poetry 224 Tatoo 224 Bibliography 225 Chapter 2 Windows Programming 227 Introduction to windows programming 227 WinMain 230 Resources 233 The dialog box procedure 237 A more advanced dialog box procedure 240 User interface considerations 242 Libraries 245 Dynamically linked libraries (DLLs) 251 Using a DLL 254 A more formal approach. 257 New syntax 257 Event oriented programming 257 A more advanced window 258 Customizing the wizard generated sample code 265 Making a new menu or modifying the given menu. 265 Adding a dialog box. 265 Drawing the window 266 Initializing or cleaning up 266 Getting mouse input. 266 Getting keyboard input 267 Handling moving/resizing 267 Window controls 268 A more complex example: a "clone" of spy.exe 273 Creating the child windows 273 Moving and resizing the child windows 274 Starting the scanning. 274 Building the window tree. 275 Scanning the window tree 275 Review 276 Filling the status bar 278 Auxiliary procedures 279 Numerical calculations in C. 282 Filling the blanks 287 Using the graphical code generator 296 Customizing controls 300 Processing the WM_CTLCOLORXXX message 300 Using the WM_DRAWITEM message 302 Building custom controls 305 The Registry 308 The structure of the registry 308 Enumerating registry subkeys 309 Rules for using the registry 311 Interesting keys 312 Etc. 313 Clipboard 314 Serial communications. 315 Files 316 File systems 317 Graphics 318 Handles and Objects 318 Inter-Process Communications 318 Mail 319 Multimedia 319 Network 319 Hooks 319 Shell Programming 320 Services 320 Terminal Services 320 Windows 321 Advanced windows techniques 322 Memory mapped files 322 Letting the user browse for a folder: using the shell 325 Retrieving a file from the internet 328 Opening a web site or an HTML file using the default browser 329 Creating a desktop shortcut 329 Error handling under windows 331 Check the return status of any API call. 333 Always check allocations 333 Common errors 334 Dialog will not display when you add a control 334 Some Coding Tips 335 Determining which version of Windows is running 335 Translating the value returned by GetLastError() into a readable string 335 Clearing the screen in text mode 335 Getting a pointer to the stack 336 Disabling the screen saver from a program 336 Drawing a gradient background 337 Capturing and printing the contents of an entire window 337 Centering a dialog box in the screen 340 Determining the number of visible items in a list box 340 Starting a non-modal dialog box 341 Displaying the environment variables 341 Propagating environment variables to the parent environment 342 Restarting the shell under program control 342 Translating client coordinates to screen coordinates 342 Passing an argument to a dialog box procedure 342 Calling printf from a windows application 343 Enabling or disabling a button or control in a dialog box. 343 Making a window class available for all applications in the system. 343 Accessing the disk drive directly without using a file system 344 Retrieving the Last-Write Time 344 Retrieving the file name from a FILE pointer 345 Setting the System Time 346 Getting the list of running processes 347 Changing a File Time to the Current Time 348 Converting from GMT (UTC) time to local time 348 Displaying the amount of disk space for each drive 349 Mounting and unmounting volumes in NTFS 5.0 350 FAQ 351 How do I create a progress report with a Cancel button? 351 How do I show in the screen a print preview? 353 How do I change the color of an edit field? 354 How do I draw a transparent bitmap? 354 How do I draw a gradient background? 357 How do I calculate print margins? 358 How do I calculate the bounding rectangle of a string of text?