windows via c++.pdf

(5456 KB) Pobierz
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2008 by Jeffrey Richter and Christophe Nasarre
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form
or by any means without the written permission of the publisher.
Library of Congress Control Number: 2007939306
ISBN: 978-0-7356-6377-0
1 2 3 4 5 6 7 8 9 10 QGT 6 5 4 3 2 1
Printed and bound in the United States of America.
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further infor-
mation about international editions, contact your local Microsoft Corporation office or contact Microsoft
Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress.
Send comments to mspinput@microsoft.com.
Microsoft, ActiveX, Developer Studio, Intellisense, Internet Explorer, Microsoft Press, MSDN, MS-
DOS, PowerPoint, SQL Server, SuperFetch, Visual Basic, Visual C++, Visual Studio, Win32, Win32s,
Windows, Windows Media, Windows NT, Windows Server, and Windows Vista are either registered
trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. <possible
third-party trademark info>. Other product and company names mentioned herein may be the trademarks
of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places,
and events depicted herein are fictitious. No association with any real company, organization, product,
domain name, e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided
without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its
resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly
or indirectly by this book.
Acquisitions Editor:
Ben Ryan
Developmental and Project Editor:
Lynn Finnel
Editorial Production:
Publishing.com
Technical Reviewer:
Scott Seely; Technical Review services provided by Content Master, a member
of CM Group, Ltd.
Body Part No. X14-25709
Dedication
To Kristin, words cannot express how I feel about our life together.
I cherish our family and all our adventures.
I’m filled each day with love for you.
To Aidan, you have been an inspiration to me and have taught me
to play and have fun. Watching you grow up has been so rewarding
and enjoyable for me. I feel lucky to be able to
partake in your life; it has made me a better person.
To My New Baby Boy (shipping Q1 2008),
you have been wanted for so long it’s hard to believe
that you’re almost here. You bring completeness and balance
to our family. I look forward to playing with you,
learning who you are, and enjoying our time together.
— Jeffrey Richter
To my wife Florence, au moins cette fois c’est écrit: je t’aime Flo.
To my parents who cannot believe that learning English
with Dungeons & Dragons rules could have been so efficient.
— Christophe Nasarre
Contents at a Glance
Part I
Required Reading
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Working with Characters and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Kernel Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1
2
3
Part II
Getting Work Done
Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Thread Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Thread Scheduling, Priorities, and Affinities. . . . . . . . . . . . . . . . . . . . . . 173
Thread Synchronization in User Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Thread Synchronization with Kernel Objects . . . . . . . . . . . . . . . . . . . . . 241
Synchronous and Asynchronous Device I/O . . . . . . . . . . . . . . . . . . . . . . 289
The Windows Thread Pool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Fibers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
4
5
6
7
8
9
10
11
12
Part III
Memory Management
Windows Memory Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exploring Virtual Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Virtual Memory in Your Own Applications . . . . . . . . . . . . . . . . .
A Thread’s Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Memory-Mapped Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
371
395
419
451
463
519
13
14
15
16
17
18
Part IV
Dynamic-Link Libraries
DLL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DLL Advanced Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread-Local Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DLL Injection and API Hooking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
537
553
597
605
19
20
21
22
v
Zgłoś jeśli naruszono regulamin