Tài liệu Programming with Lcc win 32 docx

267 1.9K 0
Tài liệu Programming with Lcc win 32 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Programming with lcc-win32 by Jacob Navia and Q Software Solutions GmbH © 2000-2003 Jacob Navia and Q Software Solutions GmbH. This document is part of the lcc-win32 documentation. Distribution in any form is explicitly not allowed. Chapter 1 Introduction to C 1 1.1 Organization of C programs 2 1.2 Hello 3 1.2.1 An overview of the compilation process 5 1.2.2 Summary of section Hello. 6 1.3 An overview of the standard libraries 7 1.4 Windows specific headers 8 1.5 Passing arguments to a program 8 1.5.1 Iteration constructs 10 1.5.1.1 for 11 1.5.1.2 while 11 1.5.1.3 do 11 1.5.2 Basic types 11 1.5.3 Summary. 12 1.6 Declarations and definitions 13 1.6.1 Variable declaration 13 1.6.2 Function declaration 15 1.6.3 Function definitions 16 1.6.4 Variable definition 16 1.6.5 Statement syntax 16 1.7 Errors and warnings 17 1.8 Reading from a file 18 1.9 Commentaries 23 1.9.1 Standard comments 24 1.9.1.1 Describing a function 24 1.9.1.2 Describing a file 26 1.10 An overview of the whole language 26 1.10.1 Statements 28 1.10.2 Declarations 32 1.10.3 Pre-processor 34 1.10.4 Windows specific defined symbols 35 1.10.5 Control-flow 36 1.10.6 Windows specific syntax 36 1.10.7 A closer view 37 1.10.7.1 Identifiers. 37 1.10.7.2 Constants. 37 1.10.7.3 Array syntax. 38 1.10.7.4 Function call syntax 38 1.10.7.5 Functions with variable number of arguments. 39 1.10.7.6 Assignment. 39 1.10.7.7 Postfix 40 1.10.7.8 Subtraction. 40 1.10.7.9 Conditional operator. 40 1.10.7.10 struct. 41 1.10.7.11 Unions. 41 1.10.7.12 typedef. 41 1.10.7.13 register. 41 1.10.7.14 sizeof. 41 1.10.7.15 enum. 41 1.10.7.16 Prototypes. 41 1.10.7.17 variable length array. 42 1.10.7.18 const. 42 1.10.7.19 unsigned. 42 1.10.7.20 bit fields 42 1.10.7.21 stdcall. 43 1.10.7.22 break and continue statements 43 1.10.7.23 Null statements 44 1.10.7.24 Comments 44 1.10.7.25 Switch statement. 44 1.10.7.26 inline 45 1.10.7.27 Logical operators 46 1.10.7.28 Bitwise operators 46 1.10.7.29 Address-of operator 47 1.10.7.30 Indirection 47 1.10.7.31 Precedence of the different operators. 49 1.11 Simple programs 51 1.11.1 Problem 1: strchr 51 1.11.2 Problem 2: strlen 51 1.11.3 Problem 3: ispowerOfTwo 52 1.11.4 Problem 4: strlwr 53 1.11.5 Problem 5: paste 54 1.12 Using arrays and sorting 58 1.12.1 Summary of Arrays and sorting 67 1.13 Pointers and references 67 1.14 Structures and unions 70 1.14.1 Structures 70 1.14.2 Structure size 73 1.14.3 Defining new types 74 1.14.4 Unions 75 1.15 Using structures 77 1.15.1 Fine points of structure use 79 1.16 Identifier scope and linkage 80 1.17 Top-down analysis 82 1.18 Extending a program 85 1.19 Improving the design 90 1.20 Path handling 91 1.20.1 Normal handling 91 1.20.2 Security considerations 94 1.21 Traditional string representation in C 97 1.22 Memory management and memory layout 99 1.22.1 Functions for memory allocation 101 1.22.2 Memory layout under windows 101 1.23 Counting words 102 1.23.1 The organization of the table 104 1.23.2 Memory organization 105 1.23.3 Displaying the results 107 1.23.4 Code review 108 1.24 A closer look at the pre-processor 109 1.25 Time and Date functions 111 1.26 Using structures (continued) 114 1.26.1 Lists 114 1.26.2 Hash tables 117 1.27 The preprocessor in detail 118 1.27.1 Preprocessor commands 118 1.27.1.1 Preprocessor macros 119 1.27.1.2 Conditional compilation 120 1.27.1.3 The pragma directive 120 1.27.1.4 The ## operator 121 1.27.2 Things to watch when using the preprocessor 121 1.28 Pitfalls of the C language 122 1.28.1 Defining a variable in a header file 122 1.28.2 Confusing = and == 122 1.28.3 Forgetting to close a comment 122 1.28.4 Easily changed block scope. 122 1.28.5 Using the ++ or more than once in an expression. 123 1.28.6 Unexpected Operator Precedence 123 1.28.7 Extra Semi-colon in Macros 124 Chapter 2 Windows Programming 125 2.1 Introduction 125 2.2 WinMain 128 2.3 Resources 131 2.4 The dialog box procedure 135 2.5 A more advanced dialog box procedure 138 2.6 User interface considerations 140 2.7 Libraries 143 2.8 Dynamically linked libraries (DLLs) 149 2.9 Using a DLL 152 2.10 A more formal approach. 155 2.10.1 New syntax 155 2.10.2 Event oriented programming 156 2.11 A more advanced window 157 2.12 A more complex example: a "clone" of spy.exe 163 2.12.1 Creating the child windows 163 2.12.2 Moving and resizing the child windows 164 2.12.3 Starting the scanning. 164 2.12.4 Building the window tree. 165 2.12.5 Scanning the window tree 165 2.12.6 Review 166 2.12.7 Filling the status bar 168 2.12.8 Auxiliary procedures 169 2.13 Numerical calculations in C. 172 2.14 Filling the blanks 177 2.15 Using the graphical code generator 186 2.16 Understanding the wizard generated sample code 190 2.16.1 Making a new menu or modifying the given menu. 190 2.16.2 Adding a dialog box. 190 2.16.3 Drawing the window 191 2.16.4 Initializing or cleaning up 191 2.16.5 Getting mouse input. 191 2.16.6 Getting keyboard input 191 2.16.7 Handling moving/resizing 191 2.16.8 Getting notified when the mouse passes over your window 192 2.16.9 Creating additional controls in your window without using a dialog box 192 2.17 Customizing controls 193 2.17.1 Processing the WM_CTLCOLORXXX message 193 2.17.2 Using the WM_DRAWITEM message 195 2.18 Etc. 199 2.18.1 Clipboard 200 2.18.2 Communications. 200 2.18.3 Files 200 2.18.4 File systems 201 2.18.5 Graphics 201 2.18.6 Handles and Objects 201 2.18.7 Inter-Process Communications 202 2.18.8 Mail 202 2.18.9 Multimedia 203 2.18.10 Network 203 2.18.11 Hooks 203 2.18.12 Registry 203 2.18.13 Shell Programming 204 2.18.14 Services 204 2.18.15 Windows 204 2.19 Advanced C programming with lcc-win32 206 2.19.1 Operator overloading 206 2.19.2 References 206 2.19.3 Overloaded functions 207 2.19.4 Default arguments 207 2.20 Advanced windows techniques 207 2.20.1 Memory mapped files 207 2.20.2 Letting the user browse for a folder: using the shell 210 2.21 Some Coding Tips 213 2.21.1 Determining which version of Windows the program is running 214 2.21.2 Translating the value returned by GetLastError() into a readable string 214 2.21.3 Clearing the screen in text mode 214 2.21.4 Getting a pointer to the stack 215 2.21.5 Disabling the screen saver from a program 215 2.21.6 Drawing a gradient background 216 2.21.7 Capturing and printing the contents of an entire window 216 2.21.8 Centering a dialog box in the screen 219 2.21.9 Determining the number of visible items in a list box 219 2.21.10 Starting a non-modal dialog box 220 2.21.11 Propagating environment variables to the parent environment 220 2.21.12 Restarting the shell under program control 220 2.21.13 Translating client coordinates to screen coordinates 221 2.21.14 Passing an argument to a dialog box procedure 221 2.21.15 Calling printf from a windows application 221 2.21.16 Enabling or disabling a button or control in a dialog box. 221 2.21.17 Making a window class available for all applications in the system. 222 2.21.18 Accessing the disk drive directly without using a file system 222 2.21.19 Enumerating registry subkeys 223 2.21.20 Retrieving the Last-Write Time 224 2.21.21 Setting the System Time 225 2.21.22 Changing a File Time to the Current Time 225 2.21.23 Displaying the amount of disk space for each drive 225 2.22 FAQ 226 2.22.1 How do I create a progress report with a Cancel button? 226 2.22.2 How do I show in the screen a print preview? 228 2.22.3 How do I change the color of an edit field? 229 2.22.4 How do I draw a transparent bitmap? 229 2.22.5 How do I draw a gradient background? 232 2.22.6 How do I calculate print margins? 233 2.22.7 How do I calculate the bounding rectangle of a string of text? 233 2.22.8 How do I close an open menu? 234 2.22.9 How do I center a dialog box in the screen? 234 2.22.10 How do I create non-rectangular windows? 235 2.22.11 How do I implement a non-blinking caret? 235 2.22.12 How do I create a title window (splash screen)? 235 2.22.13 How do I append text to an edit control? 239 2.22.14 How do I spawn a process with redirected stdin and stdout? 240 2.22.15 How to modify the width of the list of a combo box 240 2.22.16 How do I modify environment variables permanently? 242 2.22.17 How do I translate between dialog units and pixels? 243 2.22.18 How do I translate between client coordinates to screen coordinates? 243 2.22.19 When should I use critical sections and when is a mutex more appropi- ate? 243 2.22.20 Why is my call to CreateFile failing when I use conin$ or conout$? 244 2.23 Overview of lcc-win32’s documentation 245 2.24 Bibliography 245 2.25 Newsgroups 246 2.26 Appendix. Code listings 250 2.26.1 The window tree. 250 2.26.2 Counting chars: countchars.c 255 1 Chapter Introduction to C This tutorial to the C language supposes you have the lcc-win32 compiler system installed. You will need a compiler anyway, and lcc-win32 is free for you to use, so please (if you haven’t done that yet) download it and install it before continuing. http://www.q-software- solutions.com What the C language concerns, this is not a full-fledged introduction to all of C. There are other, better books that do that (see the bibliography at the end of this book). Even if I try to explain things from ground up, there isn’t here a description of all the features of the language. Note too, that this is not just documentation or a reference manual. Functions in the standard library are explained, of course, but no exhaustive documentation of any of them is provided in this tutorial. 1 But before we start, just a quick answer to the question: why learn C? C has been widely criticized, and many people are quick to show its problems and drawbacks. But as languages come and go, C stands untouched. The code of lcc-win32 has software that was written many years ago, by many people, among others by Dennis Ritchie, the creator of the language itself 2 . The answer to this question is very simple: if you write software that is going to stay for some time, do not learn “the language of the day”; learn C. C doesn’t impose you any point of view. It is not object oriented, but you can do object ori- ented programming in C if you wish. 3 It is not a functional language but you can do functional programming 4 with it if you feel like. Most LISP interpreters and Scheme interpreters/compil- ers are written in C. You can do list processing in C, surely not so easily like in lisp, but you can do it. It has all essential features of a general purpose programming language like recur- sion, procedures as first class data types, and many others that this tutorial will show you. Many people feel that C lacks the simplicity of Java, or the sophistication of C++ with its tem- plates and other goodies. True. C is a simple language, without any frills. But it is precisely this lack of features that makes C adapted as a first time introduction into a complex high-level language that allows you fine control over what your program is doing without any hidden 1. For an overview of the lcc-win32 documentation see "How to find more information" 2. Dennis Ritchie wrote the pre-processor of the lcc-win32 system. 3. Objective C generates C, as does Eiffel and several other object-oriented languages. C, precisely because of this lack of a programming model is adapted to express all of them. Even C++ started as a pre- processor for the C compiler. 4. See the “Illinois FP” language implementations in C, and many other functional programming languages that are coded in C. [...]... distributed with lccwin32 Please look there for an in-depth view 8 C Tutorial 1.4 Windows specific headers windows.h All windows definitions Creating a window, opening a window, this is an extensive header file, makes approx half a megabyte of definitions Note that under lcc- win3 2, several headers like winbase.h of other distributions are concentrated in a single file winsock.h Network (tcpip) shellapi.h Windows... has nothing to do with object oriented programming of course! 19 The result of this process can be seen if you call the compiler with the –E flag For instance, to see what is the result of pre-processing the hello.c file you call the compiler in a command shell window with the command line: lcc –E hello.c 20 Again, you can see the intermediate code of lcc by calling the compiler with lcc –z hello.c This... 24 Here we will only describe the standard way of passing arguments as specified by the ANSI C standard, the one lcc- win3 2 uses Under the Windows operating system, there is an alternative entry point, called WinMain, and its arguments are different than those described here See the Windows programming section later in this tutorial Passing arguments to a program 9 char *argv[] This is an array of pointers... finish the scope of the for statement with a closing brace This means, the iteration definition ends here Now we are ready to run this program Suppose that we have entered the text of the program in the file “args.c” We do the following: h: \lcc\ projects\args> lcc args.c h: \lcc\ projects\args> lcclnk args.obj We first compile the text file to an object file using the lcc compiler Then, we link the resulting... the program executable is called: h: \lcc\ projects\hello.exe We open a command shell window, and type the command: C:\>h: \lcc\ projects \lcc1 \hello.exe Hello C:\> Our program displays the character string “Hello” and then a new line, as we wanted If we erase the \n of the character string, press F9 again to recompile and link, the display will be: C:\>h: \lcc\ projects \lcc1 \hello.exe Hello C:\> But how did... using the linker lcclnk Now, we can invoke the program just by typing its name:28 h: \lcc\ projects\args> args Argument 0 = args We have given no arguments, so only argv[0] is displayed, the name of the program, in this case “args” Note that if we write: h: \lcc\ projects\args> args.exe Argument 0 = args.exe We can even write: h: \lcc\ projects\args> h: \lcc\ projects\args.exe Argument 0 = h: \lcc\ projects\args.exe... keyword can be used within any loop construct to provoke a jump to the end of the statement block The loop continues normally, only the statements between the continue keyword and the end of the loop are ignored 1.5.2 Basic types The implementation of the C language by the lcc- win3 2 compiler has the following types built in:29All this types are part of the standard ANSI C language With the exception... exit(1); } c = fgetc(infile); while (c != EOF) { count++; c = fgetc(infile); } printf("%d\n",count); return 0; } We try again: H: \lcc\ examples> lcc countchars.c H: \lcc\ examples> lcclnk countchars.obj 22 C Tutorial H: \lcc\ examples> countchars sfsfsfsfs File sfsfsfsfs doesn't exist H: \lcc\ examples> Well this error checking works But let’s look again at the logic of this program Suppose we have an empty file... then linked with the link editor to build the final program Normally each module is written in a separate text file that contains functions or data declarations Interfaces between modules are written in “header files” that describe types or functions visible to several modules of the program Those files have a “.h” extension, and they come in two flavors: system-wide, furnished with lcc- win3 2, and private,... description of this, so that at least you know what’s happening behind the scene Wedit calls the C compiler proper This program is called lcc. exe and is in the installation directory of lcc, in the bin directory For instance, if you installed lcc in c: \lcc, the compiler will be in c: \lcc\ bin This program will read your source file, and produce another file called object file,18 that has the same name as the source . Registry 203 2.18.13 Shell Programming 204 2.18.14 Services 204 2.18.15 Windows 204 2.19 Advanced C programming with lcc- win3 2 206 2.19.1 Operator overloading. the C language supposes you have the lcc- win3 2 compiler system installed. You will need a compiler anyway, and lcc- win3 2 is free for you to use, so please

Ngày đăng: 27/01/2014, 08:20

Từ khóa liên quan

Mục lục

  • Introduction to C

    • 1.1 Organization of C programs

    • 1.2 Hello

      • 1.2.1 An overview of the compilation process

      • 1.2.2 Summary of section Hello.

      • 1.3 An overview of the standard libraries

      • 1.4 Windows specific headers

      • 1.5 Passing arguments to a program

        • 1.5.1 Iteration constructs

          • 1.5.1.1 for

          • 1.5.1.2 while

          • 1.5.1.3 do

          • 1.5.2 Basic types

          • 1.5.3 Summary.

          • 1.6 Declarations and definitions

            • 1.6.1 Variable declaration

            • 1.6.2 Function declaration

            • 1.6.3 Function definitions

            • 1.6.4 Variable definition

            • 1.6.5 Statement syntax

            • 1.7 Errors and warnings

            • 1.8 Reading from a file

            • 1.9 Commentaries

              • 1.9.1 Standard comments

                • 1.9.1.1 Describing a function

                • 1.9.1.2 Describing a file

                • 1.10 An overview of the whole language

                  • 1.10.1 Statements

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan