Programming with Microsoft Visual C++.NET 6ed [Electronic resources]

George Shepherd, David Kruglinski

نسخه متنی -صفحه : 319/ 21
نمايش فراداده

Visual C++ .NET Components

Visual C++ .NET consists of several complete Windows application development systems in one product. If you want, you can develop C-language Windows-based programs using only the Win32 API. C-language Win32 programming is described in Charles Petzold's book Programming Windows, Fifth Edition (Microsoft Press, 1998). (Petzold has a new book on Windows-based programming from Microsoft Press, called Programming Microsoft Windows with C#, which covers programming Windows using C# and Windows Forms. We'll take a look at programming Windows using Windows Forms and C++ later in this book.) You can use many Visual C++ .NET tools, including the resource editors, to make low-level Win32 programming easier. You can also use application framework libraries such as the MFC library and Windows Forms in the managed library to further speed your Windows-based application development.

Finally, Visual C++ .NET includes the Active Template Library (ATL), which you can use to develop ActiveX controls. ATL programming is neither Win32 C-language programming nor MFC programming, and it's complex enough to deserve its own book. However, we'll touch on ATL development in this book. ATL will probably find itself most at home within the high-performance Web server environment.

The first section of this book is about C++ programming within the MFC library application framework that's part of Visual C++ .NET. You'll be using the C++ classes documented in the Microsoft Visual C++ MFC Library Reference included in the Visual Studio .NET documentation, and you'll also be using application framework–specific Visual C++ .NET tools such as Class View.

Note

Use of the MFC library programming interface doesn't cut you off from the Win32 functions. In fact, you'll almost always need some direct Win32 calls in your MFC library programs.

A quick run-through of the Visual C++ .NET components will help you get your bearings before you zero in on the application framework. Figure 1-1 shows an overview of the Visual C++ MFC application build process.

Figure 1-1: The Visual C++ MFC application build process.

Visual C++ .NET and the Build Process

Visual Studio .NET is a suite of developer tools that includes Visual C++ .NET. The Visual Studio .NET integrated development environment (IDE) is shared by several tools, including Visual C++ .NET, Microsoft Visual C#, and Microsoft Visual Basic .NET. The IDE has come a long way from the original Visual Workbench, which was based on QuickC for Windows. Docking windows, configurable toolbars, and a customizable editor that runs macros are now part of Visual Studio .NET. The online help system (now integrated with the MSDN Library viewer) works like a Web browser. Figure 1-2 shows Visual C++ .NET in action.

Figure 1-2: Visual C++ .NET windows.

If you've used earlier versions of Visual C++, you already understand how Visual C++ .NET operates (although some of the menus might have changed). But if you're new to IDEs, you'll need to know what a project is. A project is a collection of interrelated source files that are compiled and linked to make up an executable Windows-based program or a DLL. Source files for each project are generally stored in a separate subdirectory. A project also depends on many files outside the project subdirectory, such as include files and library files.

Visual Studio .NET also supports building projects outside of the development environment. Within Visual Studio .NET, makefiles are still supported. (A makefile stores compiler and linker options and expresses all the interrelationships among source files.) That is, you can still type up a makefile by hand and run it though NMAKE.EXE. (A source code file needs specific include files, an executable file requires certain object modules and libraries, and so forth.) NMAKE reads the makefile and then invokes the compiler, assembler, resource compiler, and linker to produce the final output, which is generally an executable file. NMAKE uses built-in inference rules that tell it, for example, to invoke the compiler to generate an OBJ file from a specified CPP file. Note that Visual C++ .NET no longer supports the ability to export a makefile for the active project from the development environment. Use Devenv command line switches to build Visual Studio .NET projects at the command line.

In a Visual C++ .NET project, a text-format project file (with a VCPROJ extension) maintains the dependencies between project parts. A separate text-format solution file (with an SLN extension) has an entry for each project in the solution. The solution file organizes projects, project items, and solution items into a single solution by providing the environment with references to their locations on disk. It's possible to have multiple projects in a solution, but all the examples in this book have just one project per solution. To work on an existing project, you tell Visual C++ .NET to open the SLN file, and then you can edit and build the project.

Visual C++ .NET creates some intermediate files too. Table 1-1 lists the files that Visual C++ .NET generates in the solution.

Table 1-1: File Types Generated in Visual C++ .NET Projects

Filename extension

Description

APS

Supports Resource View

BSC

Browser information file

IDL

Interface Definition Language file

NCB

Supports Class View

SLN

Solution file[*]

SUO

Holds solution options and configuration

VCPROJ

Project file[*]

[*]Do not delete or edit in a text editor.

The Resource View Window and the Resource Editors

When you open the Resource View window (choose Resource View from the View menu) in the Visual C++ .NET IDE, you can select a resource for editing. The main window hosts a resource editor appropriate for the resource type. The window can also host a WYSIWYG editor for menus and a powerful graphical editor for dialog boxes, and it includes tools for editing icons, bitmaps, and strings. The dialog editor allows you to insert ActiveX controls in addition to standard Windows controls and the Windows common controls.

Each project usually has one text-format resource script (RC) file that describes the project's menu, dialog box, string, and accelerator resources. The RC file also has #include statements to bring in resources from other subdirectories. These resources include project-specific items, such as bitmap (BMP) and icon (ICO) files, and resources common to all Visual C++ .NET programs, such as error message strings. Editing the RC file outside the resource editors is not recommended. The resource editors can also process EXE and DLL files, so you can use the Clipboard to "steal" resources, such as bitmaps and icons, from other Windows-based applications.

The C/C++ Compiler

The Visual C++ .NET compiler can process both C source code and C++ source code. It determines the language by looking at the source code's filename extension. The C extension indicates C source code, and the CPP or CXX extension indicates C++ source code. The compiler is compliant with all ANSI (American National Standards Institute) standards, including the latest recommendations of a working group on C++ libraries, and has additional Microsoft extensions. Templates, exceptions, and runtime type information (RTTI) are fully supported in Visual C++ .NET. The C++ Standard Template Library (STL) is also included, although it is not integrated into the MFC library.

The Source Code Editor

Visual C++ .NET includes a sophisticated source code editor that supports many features such as dynamic syntax coloring, auto-tabbing, keyboard bindings for a variety of popular editors (such as VI and EMACS), and pretty printing. Starting with Visual C++ 6, the environment includes a feature named AutoComplete. If you have used any of the Microsoft Office products or Visual Basic, you might already be familiar with this technology. Using the Visual Studio .NET AutoComplete feature, all you have to do is type the beginning of a programming statement and the editor will provide you with a list of possible completions to choose from. This feature is extremely handy when you are working with C++ objects and have forgotten an exact member function or data member name— they're all there in the list for you. Thanks to this feature, you no longer have to memorize thousands of Win32 APIs or rely heavily on the online help system.

The Resource Compiler

The Visual C++ resource compiler reads an ASCII RC file from the resource editors and writes a binary RES file for the linker.

The Linker

The linker reads the OBJ and RES files produced by the C/C++ compiler and the resource compiler, and it accesses LIB files for MFC code, runtime library code, and Windows code. It then writes the project's EXE file. An incremental link option minimizes the execution time when only minor changes have been made to the source files. The MFC header files contain #pragma statements (special compiler directives) that specify the required library files, so you don't have to tell the linker explicitly which libraries to read.

The Debugger

If your program works the first time, you don't need a debugger. The rest of us might need one from time to time. Visual Studio .NET provides an integrated debugger by combining features of the earlier versions of Visual C++ and Visual Basic debuggers and adding many new features. These new features include the following:

    Cross-language debugging Visual Studio .NET lets you debug projects that are part of the same solution even if they're written in different languages.

    Attachment to a running program Visual Studio .NET lets you attach to and debug a program that is running outside of Visual Studio .NET.

    Remote debugging Visual Studio .NET supports remote debugging. That is, you can attach to a program that's running on another server.

    Debugging of ASP.NET Web applications ASP.NET files are compiled, so they get the same treatment during debugging that other languages get. This makes it much easier than before to debug Web applications.

    .NET Framework classes for debugging and code tracing The .NET Framework classes make it easy to instrument and put trace statements in your code. Because these classes are managed code, you can run them within managed C++ code.

Figure 1-3 shows the integrated debugger in action.

Figure 1-3: The Visual C++ .NET debugger window.

Note that the Variables and Watch windows can expand an object pointer to show all data members of the derived class and base classes. If you position the cursor on a simple variable, the debugger will show you its value in a little window. To debug a program, you must build the program with the compiler and linker options set to generate debugging information.

Visual C++ .NET includes an Edit And Continue feature. Edit And Continue lets you debug an application, change the application, and then continue debugging with the new code. This feature dramatically reduces the amount of time you spend debugging because you no longer have to manually leave the debugger, recompile, and then debug again. To use this feature, you simply edit any code while you're in the debugger and then hit the Continue button. Visual C++ .NET will compile the changes and restart the debugger for you.

The MFC Application Wizard

The MFC Application Wizard is a code generator that creates a working skeleton of a Windows-based application with features, class names, and source code filenames that you specify using dialog boxes. You'll use the MFC Application Wizard extensively as you work through the examples in this book. Don't confuse the MFC Application Wizard with older code generators that generate all the code for an application. The MFC Application Wizard code is minimalist code; the functionality is inside the application framework base classes.

The MFC Application Wizard gets you started quickly with a new application. Moreover, the wizard is extensible—you can write your own code generators. If you discover that your team needs to develop multiple projects with a telecommunications interface, you can build a special wizard that automates the process.

Class View

You can open the Class View window by choosing Class View from the View menu. You get a tree view of all the classes in your project, which displays member functions and data members, as shown in Figure 1-2. Double-click an element, and you will see the source code immediately. When you make changes to your code, Class View will reflect the changes by updating its content automatically. Earlier versions of Visual C++ included a single component named ClassWizard that handled almost all tasks involved in managing Visual C++ class code. The ClassWizard functionality has been replaced with several new wizards that individually perform such tasks as adding whole new classes, adding virtual functions to a class, and adding message-handler functions. For example, adding classes and functions has been replaced by functionality found in Class View.

Solution Explorer

Solution Explorer represents an organized view of your entire project. An entire Visual Studio .NET application might include many items—including many projects. Solution Explorer allows you to manage all aspects of a solution.

Solution Explorer includes a tree view listing the items in your project. Solution Explorer allows you to open the items for modification or perform other management tasks. The tree view of the items shows the logical relationship of the solution to projects and solution items. The view does not necessarily represent a physical storage relationship. You can associate files with the solution but not a specific project by adding them directly to the solution.

The Object Browser

If you write an application from scratch, you probably have a good mental picture of your source code files, classes, and member functions. If you take over someone else's application, however, you'll generally need some assistance. The Visual C++ .NET Object Browser (the browser, for short) lets you examine (and edit) an application from the class or function viewpoint instead of from the file viewpoint. It's a little like the "inspector" tools available with object-oriented libraries such as Smalltalk.

To invoke the browser, you choose Other Windows, Object Browser from the View menu. The browser has the following viewing modes:

    Definitions and References You select any function, variable, type, macro, or class and then see where it's defined and used in your project.

    Sorting You can sort objects and members alphabetically, by type, and by access.

    Derived Classes and Members/Base Classes and MembersT hese are graphical class hierarchy diagrams. For a selected class, you see the derived classes or the base classes plus members. You can control the hierarchy expansion with the mouse.

A typical browser window is shown in Chapter 3.

Note

If you rearrange the lines in any source code file, Visual C++ .NET will regenerate the browser database when you rebuild the project. This increases the build time.

Unified Modeling Language Tools

Visual C++ .NET now includes Unified Modeling Language (UML) tools. UML is a set of diagramming and program description conventions for describing a system. The diagram types contained in UML include class diagrams, object diagrams, activity diagrams, and state diagrams. Many organizations are standardizing on UML as the way to document their systems.

Visual C++ .NET includes a command on the Project menu for reverse-engineering a project into a UML diagram. To reverse-engineer a Visual C++ .NET project into a set of UML diagrams, you first generate browser information for the project. Then you choose Visio UML, Reverse Engineer from the Project menu. Visual C++ .NET will generate a UML package (collection of diagrams) of your project and will fire up a copy of Visio and display the package. (The UML diagrams are generated in Visio format.)

Note

To view the online help for a Visio UML solution, you must keep Visio active and running. At the end of the Visual Studio .NET Enterprise Architect installation, you will see an option to install Visio.

Online Help

Starting with Visual C++ 6, the help system has been moved to a separate application named the MSDN Library Viewer. This help system is based on HTML. Each topic is covered in an individual HTML document, and then all are combined into indexed files. The MSDN Library Viewer uses code from Microsoft Internet Explorer 4, so it works like the Web browser you already know. The MSDN Library can access the help files from the Visual Studio .NET CDs or from your hard disk, depending upon your choices during the install process, and it can access HTML files on the Internet.

Visual C++ .NET allows you to access help in different ways:

    By book When you choose Contents from Visual Studio .NET's Help menu, the Contents window opens and displays Visual Studio .NET documentation and the MSDN Library. Here you'll find Visual Studio .NET, the .NET Framework SDK, Platform SDK documentation, and more, all organized hierarchically by book and chapter. The scope of contents displayed depends on the filter you choose.

    By topic When you choose Index from Visual Studio .NET's Help menu, the Index window opens. You type a keyword in this window to see the topics and articles included for that keyword. The scope of topics displayed depends on the filter you choose.

    By word When you choose Search from Visual Studio .NET's Help menu, the Search window opens. You can use this window to perform a full-text search for a combination of words to view articles that contain those words. The scope of the search results depends on the filter you apply.

    Dynamic help Dynamic help helps you run Visual Studio .NET by providing pointers to information specific to the current area you're using or to the task you're trying to complete within the IDE.

    F1 help This is the programmer's best friend. Just move the cursor inside a function, macro, or class name and then press the F1 key, and the help system will go to work. If the name is found in several places—in the MFC and Win32 help files, for example—the Index window displays a list of topics, where you can choose the help topic you want.

However you access online help, you can copy any help text to the Clipboard for inclusion in your program.

Windows Diagnostic Tools

Visual C++ .NET contains a number of useful diagnostic tools. SPY++ gives you a tree view of your system's processes, threads, and windows. It also lets you view messages and examine the windows of running applications. Visual C++ .NET also includes a whole suite of ActiveX utilities, an ActiveX control test program, and other utilities.

The MFC Library Version 7

The MFC library version 7 is one of the main subjects of this book. It defines the application framework that you'll be learning intimately. Chapter 2 will get you started with actual code and will introduce some important concepts.

The ATL Library Version 7.0

The ATL is separate from the MFC library and is used for building ActiveX controls. You can build ActiveX controls with either the MFC or ATL library, but ATL controls are much smaller and are quicker to load on the Internet. Chapter 27 and Chapter 28 provide a brief overview of ATL and creating ActiveX controls with ATL. We'll also take a look at ATL Server in this book.

.NET Support

Visual Studio .NET fully supports the .NET Framework. While DLLs, C++, the MFC library, COM, and ATL can all work together to create Windows applications, the whole system does have a couple of warts. Once in a while, it seems that some parts are stuck together with bandages. One of the primary goals of .NET is to unify the programming model so the Windows platform is more solid. For example, the common language runtime functions to give all programming syntaxes a consistent set of data types. ASP.NET also runs under the runtime, making Web application programming much more consistent.

In addition to Visual Basic .NET, Microsoft is updating C++ to run under the new environment by adding managed extensions. You use managed extensions to tell the Visual C++ .NET compiler to emit code that runs under the runtime. There's a lot of heritage C++ code out there, and using managed extensions promises to make moving over to .NET much easier. I'll cover .NET and Visual C++ .NET's role in building .NET applications in detail in the second half of the book.