HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
DEVELOPING APPLICATIONS WITH VISUAL STUDIO.NET is an in-depth guide that takes Windows programming to the next level: creating .NET applications that leverage the prior knowledge and experience of C++ Win32 programmers. The .NET Framework supplies programmers with rich standard run-time services, supports the development of Web-based services, and provides both inter-language and inter-machine interoperability. Programmers can now focus on creating more complex, more distributed, and more Web-enabled applications.
This book begins by describing the .NET Framework, introducing the facilities .NET offers and the classes programmers can use. It goes on to describe the tools available in Visual Studio.NET and demonstrates their use. Readers are then ready to develop and debug applications with the help of clearly illustrated examples in C# and Managed C++.
DEVELOPING APPLICATIONS WITH VISUAL STUDIO.NET covers commonly overlooked topics such as error handling, memory management, event logging, localization issues, performance monitoring, ATL Server, and Managed C++. Author Richard Grimes shares his hard-won understanding of Visual Studio.NET, emphasizing the innovations that save you time and trouble and steering you away from those that limit programming power.
Among the topics explored in-depth:
With this book as your guide, you will learn how to efficiently develop strong, robust code within .NET.
Click below for Sample Chapter related to this title:
grimesch08.pdf
(NOTE: Each chapter concludes with a Summary.)
Preface.
Acknowledgments.
1. NET Runtime.
.NET Features.
.NET Languages.
Executing Code.
Interop.
Garbage Collector.
Security.
Types.
Classes.
Managed and Unmanaged C++ Classes.
Value Types.
Boxing.
Managed Arrays.
Member Access.
Common Language Specification.
Other .NET Features.
Interfaces.
Metadata and Attributes.
Exceptions.
Delegates.
Events.
Packaging and Deployment.
Assemblies and Modules.
Versioning before .NET.
Locating and Versioning .NET Assemblies.
Application Domains.
Administering Security.
The Win32 API.
Memory Allocation.
Error Handling.
Framework Class Library.
The Core Library.
General-Purpose Classes.
System Classes.
Tracing and Debugging.
Net.
Threading.
Reflection.
GDI+ and Windows.
System.Drawing.
System.Drawing.Drawing2D.
System.Windows.Forms.
Other Framework Class Library Classes.
Event Log.
Performance Counting.
Process Information.
Data.
Message Queuing.
Contexts.
Context Bound and Context Agile.
Passing Parameters by Reference and by Value.
Marshaling Objects between Contexts.
Context Sink Chains.
Dynamic Properties and Sinks.
Tracking Handler s.
Serialization.
Serializing Objects.
Formatters.
Object Manager.
Custom Serialization.
MSMQ Serialization.
Remoting.
.NET Remoting.
.NET Remote Components.
Marshaling.
Leases and Lifetime.
Message Sinks and Channels.
Channels.
Extending the Remoting Framework.
SOAP.
Security.
Web Services.
Architecture.
Managed Web Services.
SOAP Invocation.
Web Service Clients.
ASP.NET Services334
Web Services and Security.
Asynchronous Programming.
Asynchronous Delegates.
Fire and Forget.
Delegates and Remoting.
Interoperation.
Interop with Native Code.
Calling DLLs.
Platform Invoke.
String Conversions.
Errors and Exceptions.
Data Flow Direction.
Structures.
Arrays and Buffers.
Marshaling.
Explicit Allocation of Buffers.
Unmanaged Exceptions.
Unsafe Code.
COM Interop.
Interop of COM and Managed Code.
COM Callable Wrapper.
Exporting Type Information.
Registration.
Runtime Callable Wrapper.
COM Automation Types.
COM Interface Types.
COM Events.
COM Aggregation.
COM Interop and the CRT.
Component Services.
Serviced Components.
Assemblies and Applications.
COM+ Context.
Object Pooling.
Transactions.
COM+ Security.
Queued Components.
Loosely Coupled Events.
Catalog Configuration.
The Visual Studio Environment.
Menus, Commands, and Shortcut Keys.
Docking Windows and Toolbars.
Documents.
Start Page.
Projects and Solutions.
Project Types.
Project Wizard Files.
Project Files.
Solution Configurations.
Project Configurations.
Project Properties.
Configuring Code.
Adding Items to C++ Projects.
Resource View.
Adding Items to C# Projects.
Adding References.
Class View.
Properties.
C# Resources.
Editing Code.
The Text Editor.
HTML Editor.
XML Editor.
Data.
Designers.
Binary Editor.
Searching and Browsing Code.
Find In Files.
Find In Project.
Command Window.
Incremental Searches.
Bookmarks.
Braces.
Object Browser.
Tools.
External Tools.
The Help System.
Toolbox.
Clipboard Ring.
The Macro Explorer and Editor.
Add-In Manager.
The VS.NET Command Line.
C++ and C# Project Types.
ATL Projects.
MFC Projects.
Managed C++ Projects.
Other C++ Projects.
C++ Project Options.
C# Project Types.
C++ Classes.
Simple Object.
COM+ Component.
Control.
Performance Monitor.
C# Classes.
Components, Controls, and Forms.
Inherited Forms and Controls.
Data.
Enterprise Template Projects.
Enterprise Solutions.
Enterprise Template Files and Proxy Projects.
Policy File.
Server Explorer.
Data Connections.
Servers Node.
The Build Process.
Source Control.
Customizing Builds.
Building and Task List.
Deploying Solutions.
Deployment Solutions.
Visual Studio Analyzer.
ATL.
A Few New Keywords.
ATL Attributes.
Interfaces.
ATL Object Map.
ATL Server Project and ATL Server Web Service.
Managed Extensions for C++.
Class Declaration.
Member Access.
Properties and Fields.
Compiling to IL.
.NET Pointers.
Managed C++ and Interfaces.
Multiple Interfaces.
Managed Arrays.
Managed Strings.
Exceptions.
Operators.
Global Methods.
Using Managed Code in Unmanaged Code.
Using Managed Types within Unmanaged Types.
Calling Managed Code.
Developing Components.
Developing Controls.
Developing a Sample Control.
Property Editor.
Licensing.
Toolbox Items.
Resources and Internationalization.
Resources and .NET.
Locales, Cultures, and Languages.
Creating Resources.
Managed C++ and Resources.
C# and Resources.
Forms and Localization.
Resource Classes.
Satellite Assemblies.
The Event Log, Again.
Win32 Resources.
IDE Tool Windows.
Setting Breakpoints.
Threads and Modules Windows.
Call Stack Window.
Watch Windows.
Memory Windows.
Using the Command Window.
Formatting Values.
Exceptions.
Edit and Continue.
Using the Debugger.
Starting Debugging.
Start-up Options.
Stepping.
Crashes.
Compile-Time Checks.
Compiler Runtime Checks.
Remote Debugging.
Disassembly Window.
Debug Symbols.
Debugging ATL Server Projects.
CRT and ATL Memory Debugging.
ATL Interface Debugging.
.NET is a whole new way to program. It has taken me a while to get used to using it, but now I feel that .NET is the future of developing code. The two main features that .NET brings are the runtime and the Framework Class Library. The runtime executes all .NET code and provides a secure environment. The Framework Class Library is available to all .NET code regardless of the language that is used to create that code. The library is a culmination of all the libraries that Win32 developers are accustomed to using, and some APIs are provided through a new paradigm, but others are mere mirrors of the Win32 APIs that they replace. The significant point is that all of the APIs are accessed in the same way, so the application developer no longer has to learn multiple ways to access libraries (DLL exported functions, class libraries, template libraries, COM). The intention is to put more focus on using the APIs than on accessing them.
I have spent a considerable amount of time determining how .NET works and how the various classes in the library are related. On occasion it was a joy to see how .NET had provided a new API that put more power in the hands of the developer, and on a few occasions I was frustrated to see that .NET had provided a solution that reduced the facilities offered to the developer. In this book I want to give you the benefit of my odyssey through .NET. I will enthuse when .NET does it right, and I will tell you straight when .NET does it wrong.
One thing is clear: .NET is here to stay. Expect in the future that Microsoft will put more and more emphasis on .NET. Your future as a developer will be intimately entwined with .NET, and by becoming familiar with .NET now, you'll ensure that you'll be ready for whatever Microsoft decides to release in the future.
I have deliberately decided to target C++ Win32 developers. This book has no VB.NET code and only the barest minimum of ASP.NET. If you want to develop in VB.NET, or you want to develop Web applications with ASP.NET, there are other excellent books that will suit you better than this one.
I also make no excuses about the fact that this book is packed full of details, and in places it touches on tough concepts. I have worked hard to glean the information that I present in this book. I didn't have access to privileged knowledge. What you see here is the result of writing many hundreds of test applications, of pouring over IL generated with ILDASM, and of single-stepping through code with the .NET debuggers. Where I present code, you can be assured that what you see is code that I have written and tested. When I describe how an API works, you can be assured that I have determined that behavior by actually trying out the code. I have worked hard to write this book, and I expect you to work hard as you read through it. That way you'll have the enlightening learning experience that I had.
My intention when writing this book was to give you a grand tour of developing applications with Visual Studio.NET. As with all grand tours, you have to do a lot of preparation before you can start. For this reason the book is split into three parts: The first four chapters describe the .NET Framework, Chapters 5 through 7 describe the tools in Visual Studio.NET that you can use to develop applications, and Chapters 8 and 9 explain how to develop and debug applications.
The chapters follow a logical progression from a basic description of the .NET Runtime, through a description of the framework and the tools to develop .NET executables and libraries, to developing applications with the Visual Studio. NET design environment. I made a deliberate choice to leave an introduction of Visual Studio.NET until Chapter 5 because much of the development that you can do with .NET can be done with a simple text editor like Notepad (indeed, I urge you to use Notepad as you work through the first few chapters). However, at the point that your project grows larger than a handful of files, at the point that you move away from trivial resources and XML files to use more real-life files, you'll start to realize that you need a more accomplished editor and tools to help you develop your application.
It is at this point that you'll need to use Visual Studio.NET. If you use VS.NET before you've gone through the details of .NET's facilities, you won't appreciate the tools that VS.NET provides. I also want to give you the impression that there is a life beyond wizard-generated code.
The following is a brief description of what to expect in the chapters of this book.
In this chapter I start exactly where I should: right at the beginning with a description of the .NET Runtime. The runtime executes all .NET code, so in this chapter I describe the basic concepts of the runtime and how it locates and runs code. I describe the languages that you can use and various .NET features used to generate events, to provide error information, and to access code outside of the current code module. Then I describe the .NET facilities for adding type information to a code module, and how you can add custom type information. Finally, I explain how code is packaged and the new mechanism for locating and loading that code.
I use this chapter to give an overview of what I consider to be the "core" library. The framework library is available to all .NET languages and can be considered to be a culmination of the various APIs that Win32 developers are accustomed to using. The Framework Class Library contains many classes, and I have chosen to explain just a few. These are the classes that you are most likely to use--the classes without which you are unlikely to be able to do any .NET development.
I explain threading, basic input/output using streams to files and to the console, the debugging classes, and collection classes. Once I have made the foundation with the fundamental classes, I follow up with a description of the user interface classes to allow you to create windows and draw in those windows. Finally, I describe what I call the "other" classes. Strictly speaking, they are not core classes, but they are important for application development, so I give an overview of these classes and how to use them.
.NET provides a new level of code isolation: application domains, which I introduced in Chapter 1. In this chapter I explain how code in one application domain can access code in another domain: .NET remoting. .NET defines an idea called context--domain membership is one property of a context--and .NET needs to take special action to allow calls to be made across contexts. I explain how cross-context calls are made and how you can change the way that .NET manages these calls.
This chapter explains in detail how .NET remoting works, the types of objects that can be accessed remotely, how long they live, and the security aspects of accessing those objects. Finally, the chapter has a description of asynchronous programming, including how exceptions and return values are handled when a call is made asynchronously.
Interoperation is required to allow your .NET code to access native code. In this chapter I cover all aspects of interoperation from accessing DLLs, through Platform Invoke, to accessing COM and COM+ components. I start by describing how Platform Invoke works and contrast this to how you can access native C++ code from managed C++. I also explain marshaling non-simple types, how exceptions are handled, and "unsafe" code.
In the second half of the chapter I explain how interoperation with COM and COM+ is achieved. .NET provides tools to allow you to use COM objects in .NET code and to use .NET objects in COM code. I show you how to use these tools and describe how COM concepts like events, aggregation, and registration are achieved. The final part of the chapter explains the .NET classes that allow you to write COM+ classes.
By this point in the book you should have a good grounding in the facilities that .NET offers and the classes that you can use. In this chapter I introduce you to the facilities in Visual Studio.NET that you can use to develop .NET code. I start with a general description of the user interface: the various windows and toolbars that are provided. I then give a brief description of the code-generating wizards and the UI features of VS.NET that you can use to change the code that is generated.
In the latter half of the chapter I explain how to manage your code and the tools that you can use to search in your code and to get help. Finally, I explain the Toolbox and how to use external tools.
In this chapter I look in detail at how you create and build solutions. A solution is a collection of projects, and a project contains source files that are compiled to create an output. I start this chapter by explaining the various project types that you can add to a solution and the various classes that you can add to those projects. Following the general theme of this book, I explain only C++ and C# classes, but I do include a description of the C++ projects that create native code.
In the second half of the chapter I explain how to build and deploy solutions. I describe how to start a compilation, and how to customize it. I also explain how you can deploy the outputs from a solution and the various project types that are provided to do this.
C++ is the most flexible of the .NET languages. Visual C++ allows you to develop both native and managed code, as well as a mixture of the two. I start this chapter by describing the new features in ATL, beginning with a description of Visual C++ attributes and an explanation of how these help your development. Then I explain ATL Server, which is used to write ISAPI extensions for Web applications and Web Services.
The rest of the chapter is concerned with managed code, and I explain how managed C++ differs from native C++ in declaring classes and in handling interfaces, arrays, and strings.
This chapter is concerned with two main subjects: developing UI controls and forms, and localizing resources. In the first half of the chapter I explain components and controls and lead you through the process of developing a control that is integrated with the Visual Studio.NET Toolbox and Properties windows. In the second half of this chapter I describe .NET resources and how you can use the Visual Studio.NET tools to create localized resources.
After you have designed, written, and built your application, the next step will be to check that it does what it is designed to do, and to correct code that is in error. That is, you will have to debug your application. Visual Studio.NET allows you to debug both native and managed code, and even to step from the managed to the unmanaged world and vice versa. In this chapter I explain all the facilities that you can use to debug your code: how to set breakpoints, how to use the debugging windows, and how to debug more than one process.
7-bit encoded integers, 152
16-bit integer, converting to string, 142
64-bit integer data type, 410
64-bit number, 365
2013 warning message, 238
+= operator, 78-79AAbort() method, 187, 195
Abs function, 160
Absolute values, 9-10
Absolute zero, 143
Abstract classes, 21, 24, 59-60, 473, 592-593
Abstract methods, 24-25
Accept() method, 180
AcceptSocket() method, 181
AcceptTcpClient() method, 181
Access
attribute checking, 436-437
checking, 18
levels of, 37-38
members, 37-39
programmatic checking, 437
role checking, 436-437
Access property, 474
Access token, 17
Account object, 437
ACL (access control list), 17
ACos function, 159
Activate() method, 431, 433, 434
Activation, 432
Activator class, 163
Activator.GetObject() method, 303
Active Server Page Component wizard, 521
ActiveX components, 552
ActiveX controls, 564
ActiveXMessageFormatter class, 289
Add ATL Support to MFC wizard, 521
Add class, 530
Add Class command, 520
Add Class dialog, 464, 520, 533, 534, 665
Add Class wizard, 465-467, 473, 530, 543
Add Component command, 488
Add Connection Point wizard, 472
Add context command, 530, 552
Add context menu, 463
Add Existing Item command, 664
Add Field wizard, 471
Add handler code, 85
Add Indexer wizard, 471
Add Item dialog, 477
add_ method, 84, 85
Add Method wizard, 470
Add New Item dialog, 664
Add New Item wizard, 465, 467
Add PerfMon Counter Wizard, 527
Add PerfMon Object command, 526
Add Property wizard, 471
Add Reference dialog, 467-468
Add Reference item, 467
Add Resource command, 520
Add Resource dialog, 463, 520
Add Task List Shortcut command, 496
Add Variable wizard, 471
Add Watch command, 692
Add Web Reference item, 467, 587
Add-In Manager, 505-506
Add-ins, 505-506
Add() method, 221, 229, 634
.addon directive, 83
AddRange() function, 221, 229
AddRef() method, 389, 744
Administrative policies, 109
Administrative Tools Start Menu folder, 122-123
Age variable, 27
Aggregation and COM, 423-424
Al.exe assembly builder tool, 113, 115, 673
Alias command, 494
Aliases, 494, 696-698
Allocated memory, 5
AllocateDataSlot() method, 198, 261
AllocateNamedDataSlot() method, 199, 261
AllocCoTaskMem() method, 381
AllocHGlobal() method, 380, 381
Alpha blending, 216
Alpha component, 216
ALT$__m data segment, 575
Analyzer wizard, 558-559
Animation routines, 205
ANSI C++, compiled by managed C++ compiler, 349
ANSI strings, 362, 617
Any constant, 181
Apartments, 407
APIs, xv, 130-133
AppBase folder, 116
App.cs file, 660, 661
AppDomain class, 70, 121, 163
_AppDomain interface, 630
AppDomain.AssemblyLoad events, 202
AppDomain.CreateInstance() method, 122
AppDomain.UnhandledException event, 199
App.exe assembly, 660, 676
Application class, 219
Application domains, 118-119, 263
assemblies, 99
lease manager, 310
security policy, 125
Application Error dialog, 64, 135
Application event log, 235, 238
Application Folder node, 556
Application log, 234, 241
Application object, 130
Application Options page, 516
Application policies, 109
Application.Exit() method, 219
Application.Run() method, 219
Applications
aborting, 170
access checks, 435
application ID, 427
bugs, 168
closing, 219
component services, 427-430
configuring, 428
dependencies, 110
developing, 633
diagnostic information, 162
end point, 178
environment variables, 161
form-based, 232
information about, 160-161
settings, 120
status code, 162
stopping without throwing exception, 162
strong name, 427
type, 428
updating, 428
Application-specific private DLLs (Dynamic Link Libraries), 105
App.resources.dll library, 673, 675
Areas, 658
Arithmetic operators and watch windows, 693
Array class, 42, 44, 141
Array::CreateInstance() method, 612, 613
Array::GetUpperBound() method, 614
Array::GetValue() method, 612
ArrayList class, 42, 44, 141, 608
Arrays
accessing members, 613
allocating, 616
complex type, 367
creation of, 611-612
__gc types, 616
how much data to marshal, 368
indexes, 36
initializing, 611-612
Length property, 37
listing members, 690-691
managed, 36-37, 409-410, 611-616
managing, 10
marshaling, 371
multidimensional arrays, 611
number of elements, 36
primitive type, 367
reference types, 36
size of, 368, 370-371
unmanaged C++, 611, 612-613
value type, 36
__value types, 616
vectors, 611
watch windows, 690
Array::SetValue() method, 612
As keyword, 44
ASCII
converting, 149
strings, 360
ASCIIEncoding class, 149
Asembly.GetManifestResourceNames() method, 666
ASin function, 159
.Asmx files, 329-331
ASP.NET, 323
accessing Web Service, 330
architectural overview, 328
assemblies, 96
code-behind, 329
compiled code, 329
cookies, 334
managed Web Services, 327-331
permission set, 124
script code, 328-329
Web Application wizard, 521
Web Service wizard, 521
Web Services, 325, 329, 331, 334
XSD schema, 332
Aspnet_isapi.dll file, 328
Assem access level, 37
Assemblies, 88
adding modules, 93-94
application domains, 99
ASP.NET, 96
attributes, 48, 58, 200-201
autoregistration feature, 430
base code identifying location, 394
build and revision number, 113-114
compiled resources, 659
component services, 427-430
components, 427
containing current type, 201
C#, 90, 97
creation of, 93
culture, 114
current, 122
default, 108-109
domain-neutral, 120
dynamic, 99
dynamically loading, 117-118
embedding resources, 660
entry point, 203-204<