Professional Windows 7 Development Guide®

Published by

Wiley Publishing, Inc.

10475 Crosspoint Boulevard

Indianapolis, IN 46256

www.wiley.com

Copyright ©2011 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-470-88570-3

ISBN: 978-1-118-05795-7 (ebk)

ISBN: 978-1-118-05794-0 (ebk)

ISBN: 978-1-118-05793-3 (ebk)

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or website may provide or recommendations it may make. Further, readers should be aware that Internet websites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Library of Congress Control Number: 2010942183

Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Windows is a registered trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.

This book is dedicated to the newest member of our family, Shelby. She’s already warmed our hearts and given us a great deal of joy.

ABOUT THE AUTHOR

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 87 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Windows command line reference, books on VBA and Visio 2007, a C# design and development manual, and an IronPython programmer’s guide. His technical editing skills have helped over 52 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He’s also contributed articles to magazines like DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test & Performance, and Visual Basic Developer.

When John isn’t working at the computer, you can find him in his workshop. He’s an avid woodworker and candle maker. On any given afternoon, you can find him working at a lathe or putting the finishing touches on a bookcase. He also likes making glycerin soap and candles, which come in handy for gift baskets. You can reach John on the Internet at JMueller@mwt.net. John is also setting up a Website at http://www.mwt.net/~jmueller/. Feel free to look and make suggestions on how he can improve it.

ABOUT THE TECHNICAL EDITOR

Russ Mullen has been consulting and programming for more years than he cares to remember. He has tech-edited more than 70 books, ghost-written chapters, and co-authored several books.

CREDITS

ACQUISITIONS EDITOR

Paul Reese

PROJECT EDITOR

William Bridges

TECHNICAL EDITOR

Russ Mullen

PRODUCTION EDITOR

Daniel Scribner

COPY EDITOR

William Bridges

EDITORIAL DIRECTOR

Robyn B. Siesky

EDITORIAL MANAGER

Mary Beth Wakefield

FREELANCER EDITORIAL MANAGER

Rosemarie Graham

ASSOCIATE DIRECTOR OF MARKETING

David Mayhew

PRODUCTION MANAGER

Tim Tate

VICE PRESIDENT AND EXECUTIVE GROUP PUBLISHER

Richard Swadley

VICE PRESIDENT AND EXECUTIVE PUBLISHER

Barry Pruett

ASSOCIATE PUBLISHER

Jim Minatel

PROJECT COORDINATOR, COVER

Katherine Crocker

PROOFREADER

Paul Sagan, Word One

INDEXER

Robert Swanson

COVER DESIGNER

LeAndra Young

COVER IMAGE

© iStock / Rubberball

ACKNOWLEDGMENTS

Thanks to my wife, Rebecca, for working with me to get this book completed. I really don’t know what I would have done without her help in researching and compiling some of the information that appears in this book. She also did a fine job of proofreading my rough draft and page-proofing the result. Rebecca also keeps the house running while I’m buried in work.

Russ Mullen deserves thanks for his technical edit of this book. He greatly added to the accuracy and depth of the material you see here, especially of some of the more difficult source code. Russ is always providing me with great URLs for new products and ideas. However, it’s the testing Russ does that helps most. He’s the sanity check for my work. Russ also has different computer equipment from mine, so he’s able to point out flaws that I might not otherwise notice.

Matt Wagner, my agent, deserves credit for helping me get the contract in the first place and taking care of all the details that most authors don’t really consider. I always appreciate his assistance. It’s good to know that someone wants to help.

A number of people read all or part of this book to help me refine the approach, test the coding examples, and generally provide input that all readers wish they could have. These unpaid volunteers helped in ways too numerous to mention here. I especially appreciate the efforts of Eva Beattie and Osvaldo Téllez Almirall, who provided general input, read the entire book, and selflessly devoted themselves to this project.

Finally, I would like to thank Paul Reese, William Bridges, Dan Scribner, and the rest of the editorial and production staff at Wrox for their assistance in bringing this book to print. It’s always nice to work with such a great group of professionals, and I very much appreciate the friendship we’ve built over the last few years.

INTRODUCTION

Windows 7 has a Considerable Array of new features to offer. Unlike some of the additions made to previous versions of Windows, these additions are substantial and not just eye candy. Using these features can make your application stand out. They make your application significantly easier to use and more responsive to user needs. In addition, many of the Windows 7 features also make your application more reliable and secure. Unfortunately, many of these features are also difficult to access and poorly documented, which is why you need the Professional Windows 7 Development Guide.

Did you know that Microsoft has made some substantial changes to the way some features work, but not really mentioned them? For example, Windows Management Instrumentation (WMI) has new functionality, and some older features no longer work because Microsoft has changed the security they depended on to function. The Professional Windows 7 Development Guide makes you aware of changes like this and provides the solutions you need to make your old applications work in the new Windows 7 environment.

You’ll also find that Windows 7 is the first version of Windows 7 that truly feels comfortable to work with in the 64-bit environment. Of course, writing 64-bit applications requires a few changes to the way you write code. The Professional Windows 7 Development Guide provides the information you need to write 64-bit code from the Windows 7 perspective.

Of course, this book has a lot more to offer. You’ll find all kinds of tips and techniques in this book that will make Windows 7 programming a lot easier and more productive. For example, you’ll learn how to write code that uses multi-core processors efficiently in Windows 7 and discover how to make a progress bar appear on the Taskbar so that users know the status of a process in your application. Before long, you’ll wonder why you didn’t start moving your applications to Windows 7 even earlier.

WHO SHOULD READ THIS BOOK

This book is for the professional developer. While it does include introductory sections, the pace of the introductory material is quite fast and the novice developer will tend to become lost. The book assumes that you have knowledge of the .NET Framework after working with C#. Some Windows 7 features don’t work well with Visual Basic.NET, so this book is written to use C# as the base programming language. The examples do provide complete information on everything you need to include in the source code, so your knowledge of the .NET Framework doesn’t have to be extensive, but some knowledge is helpful.

HOW THIS BOOK IS ORGANIZED

The book is organized into Windows 7 functional areas, so that you can find the kind of application code you need quickly. Generally, each chapter covers one task or task group (such as Taskbar programming techniques). When a particular task or task group requires more than one chapter, the easier material normally appears first, followed by material that requires additional skills to understand. This book also requires use of a number of third-party libraries, the use of which is described in detail. You won’t be left without the proper information on how to complete the example code. This book breaks the material into the following parts:

Introducing Windows 7: This part of the book answers many of the questions that you probably expect, such as what features you’ll find in Windows 7. You’ll also find a guide that describes how to create a Windows 7 upgrade strategy. Windows 7 has so many new features that creating an upgrade strategy can prove daunting for many developers, so this book also provides some helpful tips on this process. Finally, this part of the book discusses the .NET Framework 4.0. Many of the Windows 7 features require that you use this new version of the .NET Framework.

Working with the Windows 7 User Interface: The Windows 7 user interface is the first thing you’ll notice when you boot up the operating system the first time. It looks gorgeous, but it’s also quite functional. This part of the book helps you understand all these features and make use of new features such as Jump Lists. You may have noticed that right-clicking some Windows 7 applications displays a recent file list — you can add this functionality to your application, too, and this part of the book shows you how. This part also addresses new interface functionality such as the Ribbon interface, and it shows how to maximize use of Aero Glass in your applications. You’ll even find a chapter about the Multi-Touch User Interface.

Developing Secure Applications: Something the user will fail to notice but you can’t miss are the significant changes in Windows 7 security. It’s true that Microsoft relaxed security in some places to allow more applications to run unhindered, but that’s only part of the story. In other areas, Microsoft has greatly tightened security or changed it altogether. You may suddenly find that your application doesn’t work as well as it used to because it can’t access resources or a resource isn’t even available any longer. This part of the book makes you aware of all the latest security changes. In addition, you’ll discover how to work with Windows 7 security features such as Windows Firewall. This part contains extensive code examples on adding, removing, and querying ports in the Windows Firewall that you won’t find anywhere else.

Advanced Windows 7 Programming: Many of the new Windows 7 features don’t fit neatly into existing categories. You may have noticed the new Windows 7 Libraries feature, in which users can place documents and access them without consideration of the document’s actual location. This part of the book includes code that shows how to add your application’s files to the Libraries, so that you can place the document in a secure location, yet make it accessible to users who need it. You’ll also discover new technologies such as trigger-start services, 64-bit application support, and parallel processing. This part even includes a chapter about the new sensor support that Windows 7 provides. Imagine being able to change the display so that it matches ambient lighting conditions. This is just one of many ways to use sensors, and this book tells you about the techniques for interacting with these sensors. Finally, you might have an application that simply doesn’t like Windows 7, but you need to keep using it. There’s also a chapter on using Windows XP Mode to keep that application running just a while longer.

Working at the Command Line: The command line prompt that developers and administrators use today has been around since the days of DOS. In fact, the command line has commands that haven’t changed since DOS was king. Unfortunately, the command prompt is hard to use and error-prone, so Microsoft has developed a new command prompt, Windows PowerShell. Windows 7 comes with Windows PowerShell 2.0, which fixes a lot of the problems with the first version. For example, Windows PowerShell 2.0 includes an editor so you can write code with greater ease and a debugger you can use to find errors in your code. This part of the book discusses Windows PowerShell in depth. In fact, you’ll even learn how to create your own scripts and cmdlets.

WHAT YOU NEED TO USE THIS BOOK

This is a Windows 7 programming book, so naturally, you need a copy of Windows 7. Using the Windows 7 Ultimate edition will give you access to the greatest number of features and let you use the greatest number of book examples. The Windows 7 Starter edition won’t work for this book because it lacks Aero Glass functionality and most of the other features discussed in this book. If you want to write 64-bit examples, then you must have a 64-bit version of Windows 7. Otherwise, the 32-bit version of Windows 7 should work fine.

The book also assumes that you have a copy of Visual Studio 2010. You must have C# installed in order to work with the examples. The book was tested using Visual Studio 2010 Ultimate, but the examples should also work fine with Visual Studio 2010 Professional and Visual Studio 2010 Premium. The examples are unlikely to work with Express Edition products or special school edition products because these editions usually lack the required functionality.

You’ll also need an Internet connection. Many of the examples require that you use third-party libraries that you’ll need to download and install. Windows 7 has a lot of functionality that Microsoft only partially implemented in the .NET Framework 4.0, so these third-party libraries are essential to create working examples.

If you want to work with examples that require special hardware, such as sensors, then you need that hardware attached to your system. The sensor examples in Chapter 18 actually rely on a software sensor, so you can still work with that particular example if you don’t have a special sensor attached to your system.

CONVENTIONS USED IN THIS BOOK

To help you get the most from the text and keep track of what’s happening, we’ve used some conventions throughout the book. Typical examples follow:

Notes provide special information that you might find helpful. For example, a note might include a reference to a third-party product that will make your development efforts easier. In some cases, notes contain ancillary information that’s nice to know, but not absolutely necessary. You can skip notes without losing anything — notes are simply there to provide a little extra information that you might not find elsewhere.

A warning tells you that something terrible will happen should a particular event occur. For example, if you perform a task incorrectly, you might see data loss. Always read warnings carefully and think about them before you proceed in the book.

As for styles in the text:

We italicize new terms and important words when we introduce them.

We show keyboard strokes like this: Ctrl+A.

We show filenames, URLs, and code within the text like so: persistence.properties.

We show text you should type directly in this way: type Hello for regular text and type MyFile.py for code and filenames.

We show text that you should replace with your own values in this way: MyName for regular text and MyVariable for code and filenames.

We present code in the following way:

We use a monofont type with no highlighting for most code examples.

If some code is too long to fit on a single line in the book, but you need to type it in on a single line in your editor, we’ll use a line continuation character like this:

This code snippet is really very long and it won’t fit on a single line in the book so we use a line continuation character.

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code used in this book is available for download at http://www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book. Code that is included on the Website is highlighted by the following icon:

Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-0-470-88570-3.

Once you download the code, just decompress it with your favorite tool. Alternately, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the Book Search Results page, click the Errata link. On this page, you can view all errata that have been submitted for this book and posted by Wrox editors.

A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the errata page, click the Errata Form link and complete the form to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com, you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to p2p.wrox.com and click the Register link.

2. Read the terms of use and click Agree.

3. Complete the required information to join as well as any optional information you wish to provide, and click Submit.

4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

PART I

Introducing Windows 7

• CHAPTER 1: Windows 7 Improvements

• CHAPTER 2: Developing a Windows 7 Strategy

• CHAPTER 3: Understanding .NET 4.0

Chapter 1

Windows 7 Improvements

WHAT’S IN THIS CHAPTER?

Understanding the general perception of Windows 7

Considering the developer-specific improvements in Windows 7

Using Windows XP Mode to your advantage

Working with Windows PowerShell 2

Developing a process for moving your application to Windows 7

Many people are already hailing Windows 7 as the product Microsoft should have produced instead of Vista. It’s true that Vista had more than a few problems, and that those problems remained even after several patches. Windows 7 is easier to use, more stable, and has more interesting features than Vista. In short, it’s just a better version of Windows, and many users plan to upgrade. Of course, the user perspective doesn’t tell the developer anything. Just because Windows 7 is a better product doesn’t mean you should rush out a new version of your program to make use of Windows 7 functionality. In fact, many companies will take a wait-and-see approach — some of them to their detriment.

Windows 7 does include more than just a bit of a face lift and a few patches. As a developer, you’ll find many of the changes in Windows 7 both exciting and necessary to robust application development. The new security features really do work, so using them in your application will make it more secure without affecting application performance and without garnering a host of hostile users, as Vista-specific applications have done. Features such as the ability to program the Taskbar are also much needed and helpful.

This chapter will provide you with an overview of what Windows 7 offers. It’s not a Microsoft-sponsored essay on why you should upgrade to Windows 7, but rather something to consider as you make an informed decision about upgrading. For many developers, Windows 7 is a must-have upgrade because it truly does provide worthwhile functionality.

IS WINDOWS 7 REALLY JUST VISTA FIXED?

One of the egregious features of Vista is the User Account Control (UAC). The feature is so terrible that you’ll find more than a few articles devoted to disabling it (as an example, see http://www.howtogeek.com/howto/windows-vista/disable-user-account-control-uac-the-easy-way-on-windows-vista/). Vista used UAC as a bludgeon to coerce the user into senselessly clicking the OK button every time the permission-escalation dialog box appeared — the user ended up with a Pavlovian reaction — barely seeing the dialog box, just clicking it to get the application running. This makes the feature completely useless and counterproductive. (You can find more about Pavlov’s experiments with conditioned response in dogs at http://en.wikipedia.org/wiki/Ivan_Pavlov.)

Windows 7 does fix this problem to a large degree. The UAC permission-escalation dialog box appears only when the user truly needs to approve an action by the operating system. This sort of change leads many developers to believe that Windows 7 is merely a patch to Vista. While Windows 7 does fix a number of interface, security, and development problems, and does contain all the latest patches, it provides significantly more functionality than a mere patch. Any thoughts you have of Windows 7 being the service pack that Microsoft should have released for Vista are simply wrong.

Many developers cringed at the compatibility issues present in Vista. In fact, application compatibility issues kept many organizations from even considering Vista as part of their upgrade path. Windows 7 does a significantly better job with compatibility. The built-in support provides better options for running existing applications. If your application won’t run using this support, you can always download Windows XP Mode and run your application using a virtual machine (see the “Considering Windows XP Mode” section of this chapter for additional information). Most organizations are finding fewer applications that experience difficulty running under Windows 7, because Microsoft has relaxed a few of the security features and made other compatibility decisions that provide a friendlier environment. In short, Windows 7 provides more than a simple patch to the Vista compatibility problems — it provides a significant solution.

Developers rely on Windows to provide a certain level of application support out of the package. Vista often lacked support for proper drivers, and it had other issues that kept even well-behaved applications from running properly. Windows 7, even the 64-bit edition, comes with relatively complete drivers and other support for your applications. In fact, your users might not even notice that they’re running on a 64-bit platform, should you choose the 64-bit edition.

CONSIDERING THE MAJOR IMPROVEMENTS FROM A DEVELOPER PERSPECTIVE

Windows 7 provides significant new functionality that the developer will notice almost immediately. This chapter doesn’t list every Windows 7 feature change. You can find a complete list at http://windows.microsoft.com/en-US/windows7/products/features. The following sections provide an overview of the features that most developers will want to at least consider adding or leveraging for their new applications and application updates.

Understanding the User Interface Improvements

Windows 7 contains a significant number of user-interface improvements. For example, when you select an item on the Taskbar, you’ll see a well-defined thumbnail for it, as shown in Figure 1-1. The functionality that Windows 7 provides for seeing what you need quickly is much improved over what Vista provided. Your application will have access to this kind of functionality even if you don’t provide any additional code for it.

FIGURE 1-1: Windows 7 provides an improvement over the graphics in Vista.

In many cases, your application can use the Windows 7 graphics features automatically, but there are qualifications to consider. For example, the Snap feature described at http://windows.microsoft.com/en-us/windows7/products/features/snap helps users work more efficiently with applications. However, Snap won’t work in many cases with applications that have custom window behaviors (see the caveat at http://windows.microsoft.com/en-us/windows7/Arrange-windows-side-by-side-on-the-desktop-using-Snap). Consequently, your application might work fine with Windows 7, yet the user might not be able to use all Windows 7 features when your application employs certain customizations. These customizations might appear helpful at first, but they can lead to inconsistent behavior that actually reduces user productivity. It’s important to understand that the Windows 7 user interface adds gizmos the user will want to use but can only use when your application follows the rules. Check out the related URLs for Peek (http://windows.microsoft.com/en-us/windows7/products/features/peek) and Shake (http://windows.microsoft.com/en-us/windows7/products/features/shake).

Some Windows 7 user-interface features look automatic but aren’t. For example, if you want your application to provide Jump Lists (http://windows.microsoft.com/en-us/windows7/products/features/jump-lists), you have to add the support to the application. In fact, there are a number of Windows 7 features that fall into this category, including, but not limited to:

Jump Lists

Progress Bars

Custom Switchers

Thumbnail Toolbars

Overlay Icons

Chapters 4 and 5 provide the details needed to implement these features in your applications. These features and others fall into the Taskbar-improvement category that you can read about at http://windows.microsoft.com/en-us/windows7/Whats-new-with-the-Windows-7-taskbar. If you must select one area of improvement for your application, the Taskbar is the place to make the improvement because this makes the user significantly more efficient.

Many of these new features require that you use the Aero Glass interface. Of course, that means having the required hardware support on your system. Unlike Vista, where Aero Glass was often eye candy and not much more, using Aero Glass in Windows 7 does provide opportunities to reduce user confusion, improve user efficiency, and reduce support costs. However, to get these improvements you have to make the required changes to your application.

Another highly visible feature is the use of the Ribbon for many of the Windows 7 applications such as WordPad, shown in Figure 1-2. Chapter 7 also shows how to add the Ribbon to your applications. Support for using the Ribbon is built right into Windows 7, so it’s always available to you. Many users find the Ribbon is easier and less confusing to use than the old menu and toolbar interface. (Power users often decry the Ribbon as a time waster, especially when working with applications that previously relied on a menu interface.)

FIGURE 1-2: Adding a Ribbon interface to your applications is easier in Windows 7.

This has been an overview of some of the more popular user-interface additions. The rest of the book will show you how to incorporate a number of other user-interface features into your applications. For now, all you really need to know is that Windows 7 has a lot of new user-interface perks to offer.

Considering the Security Improvements

Security is one of the more important topics today because there are more threats to your data, applications, network, and personal systems than ever before. Windows 7 adds more security to provide additional protection. However, it also takes a step back. Users found some of the security features in Vista draconian at best and impossible at worst. Microsoft has recognized that security features which users turn off because they dislike them aren’t really security features. The current section of this chapter provides some highlights of the more important security improvements. You can see a complete overview of Windows 7 security at http://technet.microsoft.com/library/dd560691.aspx. Part III of the book focuses on security and how you can implement it in your applications.

Easing the number of times that the UAC asks the user for permission elevations is a priority for Windows 7. In reality, the UAC is a good feature because it alerts the user to potentially dangerous situations; you should encourage users to keep the UAC enabled. Smart programming on your part will keep the number of UAC requests to a minimum. In addition, you should ensure that your application doesn’t request resources or access that it doesn’t actually need. However, no matter how secure you make your application, there’s a chance that someone will usurp control. If your application suddenly begins doing things you never designed it to do, you want the UAC to step in and alert the user to the problem. You’ll discover more about the UAC in Chapter 11.

Enhanced auditing is another essential feature. The act of auditing provides you with information about user and application activity. Knowing what the system is doing helps you locate the source of security issues on a system, and auditing is a cornerstone of that process. You can add auditing features to your application. When your application tells the system what it’s doing, it helps the system locate and destroy security threats.

Of course, your application still has access to standard Windows security (Chapter 10) and .NET-specific security (Chapter 12). In addition, your application will gain some security features automatically, including:

Kernel patch protection

Service hardening

Data execution prevention

Address space layout randomization

Mandatory integrity levels

Developing Extended Applications

Windows 7 allows you to extend your applications in some ways that users won’t see directly, but which they will definitely appreciate as part of the usage experience. Extending your application isn’t a requirement, of course, but once your organization begins to understand the power of Windows 7, you may decide that the benefits in speed and reliability are definitely worth the extra work. The following sections describe three kinds of extensions that many organizations are considering today.

64-bit Applications

Before Windows 7, you could work in a 64-bit environment, but it was painful. In fact, some people reported that the lack of device drivers and problems running older applications made the difficulties insurmountable. Windows 7 does provide a great 64-bit experience if your processor will support it. You can find a host of articles, such as the one at http://www.w7forums.com/windows-7-64-bit-vs-32-bit-t484.html, that describe the pros and cons of moving to 64 bits.

For the developer, the main reasons to move to 64 bits are memory and speed. A 64-bit application can access huge amounts of memory, up to 192 GB for most versions of Windows 7 (see http://msdn.microsoft.com/library/aa366778.aspx for the memory limits of each Windows 7 edition). In addition, using 64-bit registers makes it easier to optimize certain application activities, which means that your application runs faster. Chapter 16 tells you more about 64-bit programming.

Parallel Processing

Most machines today come with multiple cores, which Windows treats as separate processors. The only problem is that most applications are still designed to use a single processor. Even if the application uses multiple threads, the method used to create it prevents Windows from executing the application on multiple processors. As a result, the application runs more slowly than it otherwise might if it could make use of those additional cores. This book doesn’t provide you with a dissertation on exotic programming techniques such as functional programming (for details, see the article at http://www.devsource.com/c/a/Languages/Exploring-the-New-F-Language/). However, Chapter 17 does get you started with parallel-programming techniques. You’ll also want to check out the Parallel Computing Developer Center at http://msdn.microsoft.com/en-us/concurrency/default.aspx.

Background Processing

Background processing is a technique developers have used for a while now to make the foreground task of an application run faster. While one thread prints a document in the background, another thread provides the user with continued access to application features in the foreground. Windows 7 provides improved functionality for creating and managing background processes. You can obtain white papers about these improvements at http://www.microsoft.com/whdc/system/pnppwr/powermgmt/backgroundprocs.mspx. Chapter 14 provides you with practical information about working with background tasks in Windows 7.

CONSIDERING WINDOWS XP MODE

Some applications out there simply won’t work in Windows 7. If you don’t have the source code for all or part of the application, yet are tasked with making it work in Windows 7, you may have to resort to using Windows XP Mode. Essentially, Windows XP Mode is a virtual machine setup that you should avoid whenever possible. That said, real-world concerns often dictate that you need the application. When there isn’t any alternative, you’ll need to create a solution that incorporates Windows XP Mode to get your solution to run in Windows 7.

Windows XP Mode is a downloadable product and isn’t part of Windows 7. You can find it at http://www.microsoft.com/windows/virtual-pc/download.aspx. Simply follow the steps on the site for obtaining your copy of Windows XP Mode and installing it on your system. You’ll also want to review the resources on the Windows Virtual PC site at http://www.microsoft.com/windows/virtual-pc/. Of course, just getting Windows XP Mode installed and working with your application probably won’t address the whole picture, especially if the older software you need is just part of an entire solution. Chapter 19 discusses techniques you can use to incorporate your Windows XP Mode application with the rest of Windows 7.

DEVELOPING FOR WINDOWS POWERSHELL 2

Windows PowerShell has been an ongoing project. It first appeared as part of Vista, but you can download and add it to Windows XP as well. The main intent of Windows PowerShell is to provide a replacement for the aging command prompt. However, the command prompt has been around in some form since the days of DOS, and it’s hard to overcome the prompt’s incredible inertia simply because so many utilities are written for it. In fact, when moving from the 32-bit environment to 64 bits, you’re far more likely to encounter a 16-bit command prompt utility that doesn’t work on your system than you are to run into an aging Windows application. The command prompt continues to enjoy strong support with administrators because it provides the means to automate many tasks without a lot of effort. In addition, the command prompt can often accomplish tasks using a single command that would require many mouse clicks using the GUI.

Microsoft hopes to persuade more administrators to use Windows PowerShell in Windows 7 with the introduction of Windows PowerShell 2. This new version of Windows PowerShell offers a lot more in the way of utilities (called cmdlets). In addition, PowerShell now sports both a graphical interface and a debugger you can use to locate problems in cmdlets and scripts. Figure 1-3 shows the new Windows PowerShell Integrated Scripting Environment (ISE). If you work with multiple languages, Microsoft has provided multiple language support with the latest Windows PowerShell. In short, there’s a lot to like with this release. You can find a complete list of the new features at http://technet.microsoft.com/library/dd367858.aspx.

FIGURE 1-3: Windows PowerShell 2 now includes a graphical development environment.

Like administrators, developers will commonly use Windows PowerShell to automate tasks and perform standard tasks quickly. Developers will also like the idea of providing a command line interface to applications that use Windows PowerShell. Part V of this book focuses on tasks you can perform using Windows PowerShell. You’ll see basic usage instructions in Chapter 20. Chapter 21 shows how to create scriptlets (a sort of batch file), while Chapter 22 shows how to build cmdlets. In Chapter 23 you’ll find some advanced usage techniques, such as working with your cmdlets from a remote location. Although you won’t get a full picture of everything that Windows PowerShell can do (you’d need an entire book to discover its full functionality), you’ll obtain a basic understanding of the unique functionality it provides.

MOVING YOUR APPLICATIONS TO WINDOWS 7

You’ll definitely want to move your applications to Windows 7 at some point. Precisely when the move occurs depends on your organization’s requirements. When you do make the move, you’ll probably want to move to the 64-bit edition for a number of reasons. The most important is that the 64-bit edition provides access to additional memory. This edition truly is the path to the future, and it’s probably a good idea to start working out any kinks in your application sooner rather than later. The main site to obtain information about moving your application to Windows 7 is at http://msdn.microsoft.com/en-us/windows/ee532070.aspx. Most developers will follow these steps to make the move to Windows 7 easier:

1. Review the Windows 7 and Windows Server 2008 R2 Application Quality Cookbook found at http://msdn.microsoft.com/library/dd371778.aspx. The Application Compatibility site at http://msdn.microsoft.com/library/bb757005.aspx discusses such topics as working in the 64-bit environment, UAC, and named pipe hardening. Your applications will need to address these requirements to work well within Vista and Windows 7. If your application works on a server, make sure you pay special attention to the section on Issues Relevant to Windows Server 2008, especially the topic Application Compatibility: Windows Server 2008 Server Core. Server Core is a unique version of Windows Server 2008 that has special requirements — the tradeoff is that Server Core requires fewer resources and is both more reliable and more secure than other Windows Server 2008 editions.

2. Determine how to update your applications so they meet compatibility requirements. In some cases, you might decide to stop at this step because making the application compatible will prove difficult. In many cases, you can use the compatibility features provided by Windows 7 to run the existing application. Of course, if you’re using the 64-bit edition, you won’t be able to run any application that has any form of 16-bit support. This isn’t a problem for Windows applications for the most part, but you might be surprised at the number of command line utilities that require 16-bit support and won’t run any longer. If you encounter additional application compatibility questions during this process, check the Windows Application Compatibility site at http://msdn.microsoft.com/windows/aa904987.aspx. You can also ask questions on the Application Compatibility for Windows Development forum at http://social.msdn.microsoft.com/Forums/en-US/windowscompatibility/threads/. (The Windows 7 ISV Remediation Workshops site at http://readyset7.com/workshops/ is no longer available.) Chapter 2 provides additional information about developing a Windows 7 strategy. In some cases, you may want to remediate your application to use Windows XP Mode (see Chapter 19 for details).

3. Download or purchase the tools needed to rework your applications. You’ll want these tools as a minimum:

Windows SDK: http://msdn.microsoft.com/windows/bb980924.aspx

Microsoft Application Verifier: http://www.microsoft.com/downloads/details.aspx?FamilyID=c4a25ab9-649d-4a1b-b4a7-c9d8b095df18

Microsoft Application Compatibility Toolkit 5.5: http://www.microsoft.com/downloads/details.aspx?FamilyID=24da89e9-b581-47b0-b45e-492dd6da2971

Microsoft Visual Studio 2010: http://www.microsoft.com/visualstudio

4. Install the tools on a Windows 7 test system. Use a test configuration of your application and not the production environment when developing the application update. It’s important to have several accounts on your Windows 7 system. Make absolutely certain that you perform any testing using an account that has the same rights that your users will have. Otherwise, you won’t find security and resource-access feature issues that could keep your application from running properly under Windows 7.

5. Work through each of the compatibility issues in your application. When you have an application that runs on a standard user account in Windows 7 without triggering any UAC messages (Chapter 11) or experiencing errors due to lack of resources or access (Chapter 10), it’s time to perform your normal testing cycle with the update. At this point, you’ve completed a simple upgrade. Of course, you may decide to take advantage of special Windows 7 features, which means checking out Steps 6 and above.

6. (Optional) Update your application to use the new features in the .NET Framework 4.0. Chapter 3 helps you understand the advantages of working with this new version of the .NET Framework.

7. (Optional) Add new shell features such as programmable Taskbar support (Chapters 4 and 5).

8. (Optional) Add new interface features, such as a Ribbon interface (Chapters 6 and 7) and updated Aero Glass functionality (Chapter 8).

9. (Optional) Include an alternative user input such as the Multi-Touch User Interface (Chapter 9).

10. (Optional) Implement improved security functionality to keep your data and network safe (Chapters 12 and 13).

11. (Optional) Improve the functionality of your application in areas the user can’t easily see. For example, consider using background tasks (Chapter 14), Windows 7 libraries (Chapter 15), and parallel-programming techniques (Chapter 17). Windows 7 is also a great 64-bit platform — consider making the move to 64 bits to gain the significant benefits that this environment can provide (see Chapter 16 for details). Even though the user won’t directly see these improvements, the improved application performance will be obvious to everyone.

Many organizations are placing increased emphasis on the command line. In many situations, the command line provides a fast method for configuring applications, managing users or system resources, and obtaining both application and system status. While it’s possible to create perfectly acceptable applications that rely on the older CMD.EXE interface, Windows PowerShell has matured to the point that it provides a better command line environment. Part V of this book provides the information you need to work effectively with Windows PowerShell. This part of the book discusses techniques such as scripting and creating your own cmdlets to perform various application tasks.

Chapter 2

Developing a Windows 7 Strategy

WHAT’S IN THIS CHAPTER?

Defining a strategy that makes the user feel comfortable with the update

Deciding how and when to move to Windows 7 from another version of Windows

Moving an application from an older version of Windows to Windows 7 might seem an obvious thing to do. However, making the move without a plan probably won’t work very well. You have to create a strategy for moving the application, or you’ll face delays and unexpected tensions. The source of most of these delays and tensions will be the people who actually benefit the most from the move: the users who work with the application every day. Most users don’t care anything about your application — they care only about getting their work done. Any time you change something, it means they must focus attention away from their work to your application, and most are going to be hostile about the disruption of their schedules.

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!