Unmanaged Development Enhancements
For Visual C++ programmers writing unmanaged code, Visual Studio .NET offers enhancements that make it a good upgrade. We'll discuss the IDE-specific features a bit later in the chapter. Here, we'll give a brief overview of the version 7 compiler and library features.
New Compiler Options
Visual C++ .NET, the C++ component of Visual Studio .NET, has been enhanced in a number of ways to help C++ programmers write robust, secure, and efficient code. Much of this improved functionality has been achieved through the version 7.1 compiler. Microsoft has added 17 new compiler switches and 12 new linker switches to Visual C++ .NET. The following are some of the more compelling new compiler switches. Even without the new features of the IDE and .NET, for some developers these enhancements alone would justify an upgrade.
Code optimization: /GL
The /GL switch performs whole-program optimization on a project. This optimization usually results in measurable performance improvements, but you should be sure to read the cautionary notes in the documentation regarding the use of this switch.What the /GL switch essentially does is optimize the whole program based on information from each of the modules. This type of optimization allows for the use of registers across function boundaries. It also allows for the generation of inline functions, even when the functions are defined in separate modules.NoteWhen the compiler team at Microsoft tests switches such as this internally, they're actually able to do so against real-world code. For example, they used the /GL switch on the game engine that Ensemble Studios uses in the Age of Empires titles. This switch produced a 10 percent increase in the performance of the engine.
Buffer overrun checks: /GS
Nearly any application you write today has the potential to be used over the Internet. The most common technique for exploiting applications over the Internet is the buffer overrun. When a hacker takes advantage of a buffer overrun, he uses memory past the boundary that the function's programmer thought would be necessary for the execution of the routine. All a hacker needs is a tiny bit of space to enter some assembly code, and then he can usually call any function that's available to the hacked program. The /GS compiler switch can help prevent this sort of compromise by injecting security checks into the target modules at compile time.The security code works by allocating some memory on the stack just before a function's return address. When the function is entered, a security cookie is placed in that space. When the function is exited, the cookie is checked; if it's been changed, a security error handling routine kicks in. By default, this means that the user is notified of the potential compromise and the process is exited. You can change this default behavior of the error handling routine using the _set_security_error_handler function.NoteWhen I was tech editing the second edition of Writing Secure Code, I ran across a slight problem with the samples designed to demonstrate buffer overruns. I was using Visual Studio .NET and I found that I couldn't duplicate the examples in the way described. As you can guess, the checks that were set by default in version 7 of the compiler were blocking the buffer overrun hacks. It's fairly clear that without even trying, code compiled in Visual Studio .NET will be safer code. There's no panacea for preventing security problems in code, but Visual C++ .NET can get you off to a pretty good start.
Run-time error checks: /RTC
n
The run-time error checks are designed to help you catch bugs that are normally hard to detect. The /RTCn switches help you detect problems with stack corruption (/RTCs), dependencies on uninitialized variables (/RTCu), and data loss that can occur when you assign larger data to a smaller variable and array overruns (/RTCc).Like the /GS switch, the run-time error checks inject code at compile time. The /RTCn switches aren't used with optimized /O builds, and you'll get an error if you try. The /RTCn switches have integrated support in the Visual Studio .NET debugger. If a run-time error check condition is detected, the application will break into the debugger by default.You can set any of these compiler switches inside the Visual Studio .NET IDE through the Property Pages dialog box available from Solution Explorer. Just right-click on the project you want to set the options for, and choose Properties. We'll discuss the project's Property Pages dialog box in more detail in Chapter 2.
Updated Class Libraries
Visual C/C++ .NET lets developers continue to use the class libraries that they've targeted over the years. Microsoft has stated that it is committed to supporting developers who use the unmanaged class libraries that ship with Visual Studio .NET for the foreseeable future. This support includes updates for future versions of Windows.NoteMicrosoft is also moving to managed code internally at a fairly rapid pace. We encourage you to learn and explore the .NET Framework because it is destined to become the native API for Windows-based applications.The major class libraries that ship with Visual Studio .NET have been versioned to match the build number of Visual Studio .NET, so these libraries are now at version 7. These libraries include MFC 7, which features native support for Windows XP, Windows 2000, and ATL 7. A new MFC feature is DHTML dialog boxes, which allow you to create active, compelling dialog boxes for your MFC applications. Finally, ATL and MFC now share some commonly used classes, such as CString, that allow ATL programmers to take advantage of MFC functionality without having to load all of MFC.A new feature of ATL is ATL Server. ATL Server is used to create high-performance, ISAPI-based Web applications and XML Web services. You can use ATL Server to build unmanaged XML Web services solutions or to integrate legacy ATL code into your .NET solutions.Other class libraries that have been updated include the Standard Template Library (STL) and the C runtime (CRT).
C++ Attributes
C++ attributes allow programmers to add Interface Definition Language (IDL)style attributes to C++ source files. These attributes are different from the attributes that you can add to managed code. Attributes reduce the amount of code that you have to write by hand by providing specific ATL functionality that the compiler emits when the application is built.
Standards Conformance
Something that the Visual C++ team has worked extremely hard on over the last couple of years is ANSI/ISO C++ standards compliance. In Visual C++ .NET 2003, the C++ compiler has reached a compliance level of more than 98 percent, making this compiler one of the most standards-compliant you can buy.
A New IDE
Microsoft's shift to .NET has required a new set of tools to make it easy for developers to target the environment. Visual Studio .NET provides a number of compelling features for developers who are writing code and for Windows and Web developers moving to .NET.First, Visual Studio .NET unifies the IDEs of the major languages that were available in Visual Studio 6. Developers can now move freely between the different languages hosted by the same IDE. Developers working in different languages in Visual Studio .NET can work together more seamlessly and efficiently than they've ever been able to.Second, the languages that ship with Visual Studio .NET are all able to target the CLR. More specifically, Visual Basic .NET and Visual C# both target the CLR exclusively. Visual C++ .NET can target both the managed CLR and the unmanaged Windows environment. Because all .NET code eventually becomes MSIL and then JIT-compiled binaries, the runtime operates in basically the same way whether you're working in Visual C# or Visual Basic .NET. It might be easier for certain languages to access functions outside the CLR, but languages that target the CLR are functionally virtually identical. Developers are acutely aware of the language chauvinism that tends to exist between programmers who specialize in one language or another. With .NET, those lines start to blur and can cause developers to see a once-dismissed language in a whole new light.Finally, Visual Studio .NET provides editing and extensibility features that make this IDE a best-of-class tool, regardless of the target platform. The advanced features built into the designers and editors in Visual Studio .NET make creating Windows-based and Web applications a breeze. A managed-code macros facility and IDE make recording and running macros easy and seamless. And an updated extensibility API exposes parts of the IDE that have never before been available to Visual Studio developers.