- Account
- Join for Free
- Sign In
- Help & Info
- Privacy Notice
- DMCA
- Contact Us
- Terms Of Use
Marco Cantù Delphi 2009 Handbook Piacenza, Italy, November 2008 2 - Author: Marco Cantù Publisher: Wintech Italia Srl, Italy Editor: Peter W A Wood Tech Editors: Holger Flick, Jeroen Pluimers, Jan Goyvaerts, Jeremy North, Marco Breveglieri Cover Designer: Fabrizio Schiavi Copyright 2008 Marco Cantù, Piacenza, Italy. World rights reserved. The author created example code in this publication expressly for the free use by its read- ers.
The source code for this book is copyrighted freeware, distributed via the web site http://www.marcocantu.com . The copyright prevents you from republishing the code in print media without permission. Readers are granted limited permission to use this code in their applications, as long at the code itself is not distributed, sold, or com- mercially exploited as a stand-alone product.
Aside from this specific exception concerning source code, no part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, in the original or in a translated language, including but not limited to photocopy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher. ISBN: Not Assigned. Delphi is a trademark of CodeGear, a subsidiary of Embarcadero Technologies.
Windows Vista is a trademarks of Microsoft. Other trademarks ... more.
less.
are of the respective owners, as ref- erenced in the text. The author and publisher have made their best efforts to prepare this book, and the content is based upon the final release of the software whenever possible.<br><br> The author and publisher make no representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accepts no liability of any kind including but not limited to performance, merchantability, fitness for any particular pur- pose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly from this book. Edition 1, Revision 1. November 25 th 2008.<br><br> Printed by Lulu Press in USA. Electronic edition licensed to Embarcadero Technologies. Printed copies of this book are on sale on http://www.lulu.com More information on http://www.marcocantu.com/dh2009 Marco Cantù, Delphi 2009 Handbook Preface - 3 Preface To my wife Lella, with love, encouragement, passion, dedication, and patience This is a book about CodeGear Delphi 2009.<br><br> You won't find an introduction to Delphi programming, its Object Pascal language or its Visual Component Library in it. In this book you can read only about new features of Delphi 2009 for Win32 in each of these areas. The book covers Delphi 2009 Unicode support, the new language features (such as generics and anonymous methods), the improvements of the IDE, the new classes of the Run Time Library, the new components of the VCL (including the Ribbon control), and the extensions to the database architec- ture and the DataSnap multi-tier technology.<br><br> As usual for my books, I'm covering the theory but also showing you dozens of examples, you can download and play with on your computer. If you still don't own Delphi 2009, you can download the trial version and also look at the actual programs in action in a series of videos linked from the web page: http://www.marcocantu.com/dh2009/videos.html Marco Cantù, Delphi 2009 Handbook 4 - Preface This book is a sequel to Delphi 2007 Handbook, as it doesn't repeat its con- tent at all. If you are interested in new features of Delphi 2009 since Delphi 7 (or a similar old version), you can buy both of my Handbooks.<br><br> If you are looking for an introduction to Delphi, instead, you can refer to my cEssential Pascal d for the language foundations and to the books in cMaster- ing Delphi d series (in particular either cMastering Delphi 7 d or cMastering Delphi 2005 d). While my recent Handbooks and cEssential Pascal d are available on Lulu.com, the Mastering series was published in a more tradi- tional way and can be found in physical or online bookstores. You can find more details about all of my books at my personal website: http://www.marcocantu.com As usual, writing this book was quite an effort, and I have to thank many developers from the Delphi community who supported me in various ways, starting with the tech reviewers and Delphi product managers and R&D team members.<br><br> A big thank you goes to my wife and kids for their patience and encouragement. I hope you enjoy the result, like I've enjoyed writing it. And I hope you like Delphi 2009, one of the best versions of Delphi ever, as I did.<br><br> Marco Cantù, Delphi 2009 Handbook Table of Contents - 5 Table Of Contents Preface..........................................................................................................3 Introduction................................................................................................13 The Status of Delphi........................................................................................................14 Why Win32 Matters...................................................................................................14 This Book...................................................................................................................... ....15 The Author..................................................................................................................... ..16 Contact Information...................................................................................................17 Part I: Unicode.......................................................................................................19 Chapter 1: What is Unicode?........................................................................21 Characters from the Past: from ASCII to ISO Encodings..............................................22 Unicode: An Alphabet for the Entire World..................................................................24 From Code Points to Bytes........................................................................................27 Unicode Code Points and Graphemes.......................................................................27 Marco Cantù, Delphi 2009 Handbook 6 - Table of Contents Unicode Transformation Formats (UTF).................................................................28 Looking at UTF-16 ....................................................................................................29 Unicode Code Point Descriptions.............................................................................32 Byte Order Mark........................................................................................................34 Unicode in Win32............................................................................................................34 Unicode API Call Speed.............................................................................................37 UnicodeString Parameters in API calls.....................................................................38 Unicode and Fonts and APIs.....................................................................................39 Unicode Before Delphi 2009.....................................................................................41 What's Next....................................................................................................................<br><br> ..41 Chapter 2: The Unicode String Type............................................................43 From AnsiChar to WideChar..........................................................................................44 Char as an Ordinal Type............................................................................................44 Converting with Chr...................................................................................................46 32-bit Characters........................................................................................................46 The New Character Unit...........................................................................................47 Of String and UnicodeString..........................................................................................49 The Internal Structure of Strings..............................................................................50 UnicodeString and Unicode......................................................................................52 The UCS4String type..................................................................................................54 The Many String Types...................................................................................................54 The New AnsiString Type..........................................................................................55 Creating a Custom String Type..................................................................................56 Managing UTF-8 Strings...........................................................................................60 Converting Strings...........................................................................................................61 Conversions Slow Down the Code.............................................................................62 The Ensure Calls........................................................................................................64 Watch Out for Literals in Concatenation..................................................................65 Using RawByteString.................................................................................................66 New UTF-8 Conversion Functions............................................................................70 String and Character Literals..........................................................................................70 Streams and Encodings...................................................................................................72 Streaming Strings Lists..............................................................................................73 Defining a Custom Encoding.....................................................................................76 Unicode and the VCL.......................................................................................................79 A Growing Core RTL?................................................................................................80 Unicode in DFM Files................................................................................................80 Localizing the VCL......................................................................................................81 What's Next.................................................................................................................... .82 Chapter 3: Porting to Unicode.....................................................................83 Char Operations That Fail..............................................................................................84 Watch Out for Set of Char.........................................................................................84 Avoid FillChar for Characters....................................................................................86 Marco Cantù, Delphi 2009 Handbook Table of Contents - 7 String Operations That Fail or Slow Down....................................................................87 Turn on All String Conversion Warnings.................................................................88 Don't Move String Data.............................................................................................89 Reading and Writing Buffers.....................................................................................91 Appending and Concatenating Strings.....................................................................92 Strings are... Strings (not Bookmarks)......................................................................93 Actual Troublesome cPorting d Cases........................................................................93 InliningTest used AnsiString.....................................................................................94 Calling Ansi-prefixed Functions................................................................................95 Unicode Strings and Win32............................................................................................97 Win32 Console Applications.....................................................................................98 PChar and Pointer Math.................................................................................................99 The Problem with PChar..........................................................................................100 From PChar to PByte................................................................................................101 PInteger and the POINTERMATH Directive..........................................................102 Don't use PChar for Pointer Math...........................................................................103 Variants and Open Arrays Parameters.........................................................................103 What's Next....................................................................................................................<br><br> 104 Part II: Delphi 2009 and Its Compiler...................................................................105 Chapter 4: New IDE Features.....................................................................107 Installing and Running.................................................................................................108 .NET SDK Not Needed.............................................................................................108 Windows Install Clean Up.......................................................................................109 The -idecaption Flag................................................................................................109 Managing Delphi Projects.............................................................................................110 Upgrading Project Configuration Files....................................................................110 Project Options Dialog Redesigned.........................................................................113 New Project Options for the Compiler.....................................................................114 Other New Project Options......................................................................................116 Default Projects Location.........................................................................................116 The Project Manager......................................................................................................117 Project Manager Views.............................................................................................118 Build Configurations and Configuration Settings...................................................119 Project Configuration Manager...............................................................................122 Managing Resources in the IDE....................................................................................123 A cNew d Resource Compiler....................................................................................126 The Delphi Class Explorer.............................................................................................127 Other New Features.......................................................................................................129 Tool Palette Search Box...........................................................................................130 Updated Components Wizards................................................................................131 Marco Cantù, Delphi 2009 Handbook 8 - Table of Contents Anything New in the Editor?.........................................................................................132 Debugger....................................................................................................................... .133 Debugging and New Language Features.................................................................134 What's Next.................................................................................................................... 134 Chapter 5: Generics....................................................................................135 Generic Key-Value Pairs................................................................................................136 Type Rules on Generics............................................................................................139 Generics in Delphi.........................................................................................................140 Generic Types Compatibility Rules..........................................................................141 Generic Global Functions (Well, Almost)...............................................................143 Generic Type Instantiation......................................................................................144 Generic Type Functions...........................................................................................145 Generic Constraints.......................................................................................................148 Class Constraints......................................................................................................149 Specific Class Constraints.........................................................................................151 Interface Constraints................................................................................................151 Interface References vs.<br><br> Generic Interface Constraints..........................................154 Default Constructor Constraint...............................................................................155 Generic Constraints Summary and Combining Them............................................156 Predefined Generic Containers.....................................................................................157 Using TList<T>.........................................................................................................158 Sorting a TList<T>...................................................................................................159 Sorting with an Anonymous Method.......................................................................161 Object Containers.....................................................................................................163 Using a Generic Dictionary......................................................................................164 Generic Interfaces..........................................................................................................167 Predefined Generic Interfaces.................................................................................170 Smart Pointers in Delphi................................................................................................171 What's Next...............................................................................................................176 Chapter 6: Anonymous Methods................................................................177 Syntax and Semantic of Anonymous Methods.............................................................178 An Anonymous Method Variable.............................................................................179 An Anonymous Method Parameter.........................................................................179 Using Local Variables....................................................................................................180 Extending the Lifetime of Local Variables...............................................................181 More on Anonymous Methods......................................................................................183 The (Potentially) Missing Parenthesis....................................................................183 Behind Anonymous Methods..................................................................................185 Ready To Use Reference Types................................................................................186 Anonymous Methods in the Real World.......................................................................187 Anonymous Event Handlers....................................................................................188 Timing Anonymous Methods..................................................................................190 Thread Synchronization with the VCL....................................................................193 Marco Cantù, Delphi 2009 Handbook Table of Contents - 9 Parallel For Loop......................................................................................................196 AJAX in Delphi........................................................................................................200 Debating the AJAX Demo.......................................................................................204 What's Next...................................................................................................................2 06 Chapter 7: More Language and RTL Changes.............................................207 Other New Language Features.....................................................................................208 Compiler Version.....................................................................................................208 A Commented Deprecated Directive......................................................................209 Exit with a Value......................................................................................................210 Setting Properties by Reference...............................................................................211 Changes in Overloading ..........................................................................................212 Code That Triggers a Compiler Error......................................................................213 Code That Calls a Different Method........................................................................214 New and Aliased Integral Types..............................................................................214 TObject's New Methods.................................................................................................216 The ToString Method...............................................................................................216 The Equals Method...................................................................................................217 The GetHashCode Method.......................................................................................217 The UnitName Method............................................................................................218 Porting an Example from .NET...............................................................................218 TObject Class Summary...........................................................................................221 Unicode and Class Names.......................................................................................222 Changes in Threading Support.....................................................................................223 Building Strings.............................................................................................................22 6 Methods Chaining in StringBuilder........................................................................228 The Speed of Building Strings.................................................................................229 Porting a Delphi for .NET Example.........................................................................231 Using Readers and Writers...........................................................................................232 Exception(al) Enhancements........................................................................................236 The InnerException Mechanism.............................................................................237 Preprocessing Exceptions........................................................................................241 New Exception Classes............................................................................................242 Summary of New Units and New RTL Classes............................................................243 More and Less FastCode..........................................................................................244 What's Next...................................................................................................................2 44 Part III: VCL and Databases...................................................................................245 Chapter 8: VCL Improvements..................................................................247 VCL Core Improvements..............................................................................................248 Custom Hints and Balloon Hints............................................................................248 Marco Cantù, Delphi 2009 Handbook 10 - Table of Contents Enhancements to Standard Components.....................................................................251 Buttons Get New Features.......................................................................................251 Glowing Labels and LinkLabels...............................................................................254 RadioGroup Text Wrapping....................................................................................255 Edits Get Many New Features.................................................................................256 ComboBoxes and Text Hints...................................................................................259 The New ButtonedEdit Control...............................................................................259 Updates to Common Controls......................................................................................262 Grouping in a ListView............................................................................................262 Marquee and More for ProgressBar Controls.........................................................265 Check Boxes in a Header.........................................................................................266 RichEdit 2.0..............................................................................................................267 Native VCL Components...............................................................................................269 The Action Manager Components...........................................................................269 About Panels............................................................................................................269 The New CategoryPanelGroup Control...................................................................270 TrayIcon Update......................................................................................................273 Default Fonts for Application and Screen Global Objects.....................................274 Improved Graphics Support....................................................................................276 The Clipboard and Unicode....................................................................................280 Extended Vista Support.................................................................................................281 What's Next...................................................................................................................2 82 Chapter 9: COM Support in Delphi 2009...................................................283 IDL, Type Libraries, and RIDL.....................................................................................284 A Textual RIDL........................................................................................................285 The RIDL Format (COM Servers)................................................................................286 Registering and Calling the Server..........................................................................290 The New Registered Type Libraries Pane.....................................................................291 COM and Unicode.........................................................................................................293 Returning Features: Active Forms...............................................................................294 What's Next.................................................................................................................... 297 Chapter 10: The Ribbon.............................................................................299 Introducing the Fluent User Interface.........................................................................300 The Legal Side of the Ribbon...................................................................................301 A First Simple Ribbon..............................................................................................301 Actions and The Ribbon...............................................................................................304 From Events to Actions...........................................................................................305 The ActionList and ActionManager Components..................................................306 Actions and Ribbon in Practice...............................................................................307 Groups And Commands..........................................................................................308 Application Menu.....................................................................................................310 Quick Access Toolbar...............................................................................................313 Supporting Key Tips.............................................................................. ...................313 Marco Cantù, Delphi 2009 Handbook Table of Contents - 11 The Ribbon Components...............................................................................................315 Ribbons for Database Applications...............................................................................319 Using Screen Tips..........................................................................................................323 Screen Tips with No Ribbon....................................................................................323 Screen Tips Manager and Actions...........................................................................325 What's Next...................................................................................................................3 28 Chapter 11: Datasets and dbExpress..........................................................329 A Unicode ClientDataSet..............................................................................................330 Unicode in Datasets, Take 2..........................................................................................332 Unicode String Lists.................................................................................................333 Bookmarks...............................................................................................................334 Field Types and Strings............................................................................................335 Other Dataset Enhancements.......................................................................................336 New Field Types.......................................................................................................336 A More Virtual Dataset............................................................................................337 Fields Extensions......................................................................................................341 BLOB fields Considered ANSI.................................................................................343 Parameters Extensions............................................................................................344 DataSet Internals..........................................................................................................344 Porting a (Simple) Custom Dataset.........................................................................345 dbExpress in Delphi 2009............................................................................................346 Connection Settings and Connection Strings.........................................................347 Setting Driver Properties and Delegate Drivers.....................................................349 Deployment and INI files........................................................................................350 Drivers in the Executable.........................................................................................351 Extended Metadata Support....................................................................................352 Data Pumping for dbExpress...................................................................................356 Data-Aware Controls.....................................................................................................357 From DBImage to Poor Old DBGrid.......................................................................359 What's Next...................................................................................................................3 59 Chapter 12: DataSnap 2009........................................................................361 Building a First DataSnap 2009 Demo........................................................................362 Building a Server......................................................................................................362 The First Client........................................................................................................364 From DataSnap to DataSnap 2009.........................................................................365 Adding Server Methods...........................................................................................366 Sessions and Threading with a Non-Database DataSnap Server................................370 Server Objects Life Cycle.........................................................................................373 A Client Starting the Server and Opening Multiple Connections..........................374 Memory Management..............................................................................................377 Thread Management................................................................................................378 Porting an Old DataSnap Demo....................................................................................381 Porting the Server.....................................................................................................381 Marco Cantù, Delphi 2009 Handbook 12 - Table of Contents Upgrading the Client................................................................................................382 Advanced Features of ThinPlus2009......................................................................383 The DataSnap Administrative Interface.......................................................................387 Conclusion.....................................................................................................................<br><br> 390 Index..........................................................................................................391 Marco Cantù, Delphi 2009 Handbook Introduction - 13 Introduction First introduced by Borland on February 14 th 1995, Delphi has a long and glorious history of success in the Windows development and client/server areas. With millions of applications written in its Object Pascal language, Delphi spawned an entire ecosystem of components, tools, magazines, books, and (of course) web sites and online resources. Delphi is now in its 12 th version, the 20 th if you count all the way back to its predecessor Turbo Pascal 1 , which was first released 25 years ago.<br><br> What's new in this version of Delphi is the company owning the product! With the acquisition of the CodeGear division of Borland on July 1 st , 2008, Delphi became a subsidiary of Embarcadero Technologies. This change of ownership happened quite late in the Delphi 2009 development cycle, so the only practical effect of the change is the inclusion of ER/Studio in the Archi- tect version of the product.<br><br> Since the start of the CodeGear division within Borland, though, there has been a renewed focus (and investment in terms of R&D, QA, and Docs) on Delphi, specifically in its Win32 version. That's why it is relevant to focus for a second on some higher-level political issues. 1 The current version of the Delphi compiler, in fact, is 20.00.<br><br> This is highlighted by the value of the VER200 define, mentioned in the section cCompiler Version d at the beginning of Chapter 7. Marco Cantù, Delphi 2009 Handbook 14 - Introduction The Status of Delphi As I've just mentioned, the creation of the CodeGear division and then the acquisition of that division by Embarcadero Technology is providing a new foundation for Delphi, and new funding for investing in the product. Even if not aggressively marketed, and out of the radar of most publications Delphi still has millions of active users, both in the ISV sector (where its deploy- ment simplicity wins over frameworks-based solutions) and in business client/server environments, where the stability of an investment is worth more than the coolness of the platform.<br><br> It is true that the Delphi community is smaller than it was a few years back, and that part of it sticks with older versions of the product, but it is certainly still lively in many countries and has actually got back into a nice mood over the last year. Why Win32 Matters If you read most of the IT press, follow blogs, or attend to conferences, it looks that only the latest technology (and the latest fad) are worth working on and everything else is either dead of dying away. This is far from true.<br><br> From COBOL development to mainframes, from AS/400 computers to DBF databases, there is a ton of legacy technology that's not only maintained but sees significant new investment. It might be for compatibility reasons, but it is also because companies prefer having a proven and reliable technology for their core business rather than risking their business over the most-recently hyped technology. This doesn't mean, of course, that following trends, providing higher qual- ity, and empowering users is not important.<br><br> Quite the opposite. If you can keep delivering additional value on a solid foundations, you have a win-win situation. Looking at the Windows side of things, for example, Microsoft has certainly created a lot of value with its growing set of libraries and architec- tures based on the solid foundation of the .NET framework.<br><br> On the other hand it is true that, despite the robustness and stability of the core, targeting the latest and best .NET technologies is like focusing on a fast moving tar- get, which is not exactly the best thing when you need to build your Marco Cantù, Delphi 2009 Handbook Introduction - 15 client/server application that will take a couple of years to create and you hope will last at least for the next ten years or so. The other extreme is that of Micro ISV, small tools vendors, shareware developers, Internet utility builders. They are in the situation of short life span products and could certainly benefit for staying on the edge...<br><br> but even they cannot rely on a large and even changing framework for deploying their applications. They need something that works on each and every Windows box out there. This is a situation in which Delphi shines in comparison to most other solutions.<br><br> The only real exception is Visual C++, but (if you've never tried to do so) developing in it is not a RAD and OOP experience like .NET and VCL development are. Visual C++ MFC library is only a thin layer on top of the Windows API, while Delphi provides what has been called a platform, with memory man- agement and runtime services, a pretty large class library with lots of insight into user interface creation, Internet support, and database connectivity, to name only the most noticeable areas of the product. Delphi does such a good job of producing native looking Windows applica- tions such as Skype that there is rarely any visible sign that an application has been developed with Delphi.<br><br> This Book Having introduced the status of Delphi, it is about time to talk about this book. Like my recent cDelphi 2007 Handbook d this is not an all encom- passing manual covering all of the features of Delphi, as this would probably require close to 4,000 pages 2 . Despite its size, the focus of this book is uniquely on new features found in Delphi 2009, or at least added to Delphi since Delphi 2007 was released (as BlackFish SQL and some of the dbExpress metadata extensions were included in Delphi 2007 updates).<br><br> 2 This figure (4,000 pages) is my estimate of the amount of material I've written in books about Delphi over the last 13 years. That is, without considering chapters that were included in subsequent editions of my Mastering Delphi series. Marco Cantù, Delphi 2009 Handbook 16 - Introduction Needless to say I've given a central role to Unicode and to the core language changes (like generics and anonymous methods), but there is also material on updates to the RTL and the VCL, the improved support for Vista, the latest user interface controls, and an in-depth analysis of the improved dbExpress and new DataSnap 2009 multi-tier capabilities of the product.<br><br> As in my past books, there is a lot of theory and overview material mixed with countless examples, readily available online at: http://www.marcocantu.com/dh2009 As I mentioned in the cPreface d, I've also created short demonstration videos (covering how the program works, not how it was written) for most of the examples in the book, available online and listed at: http://www.marcocantu.com/dh2009/videos.html Having published this book through Lulu.com, I could shape it the way I like most, get the help of editors and reviewers I trust, and (hopefully) make it more valuable for you while keeping the price lower than through a tradi- tional channel. When I published cDelphi 2007 Handbook d, it was my first experience of publishing via Lulu.com. Now I've learned from mistakes, streamlined some operations, and have reduced some of the publishing headaches to fully focus on writing for quite some time.<br><br> I hope you find this effort valuable! The Author For those of you new to my books, and for those who haven't read a recent one, my name is Marco Cantù, and I've been in the cDelphi book writing d business since the first version of the product, when I released the original cMastering Delphi d (a hefty tome of 1,500 pages). This was not my first writ- ing experience, as I had previously written works on Borland C++ and the Object Windows Library.<br><br> Over the last few years, beside my continuous involvement in the Delphi community, I've also devoted a lot of time to XML-related technologies and XSLT, with web services (including SOAP and REST implementations), JavaScript and AJAX, and other Web 2.0 technologies. After a break, I got back to writing by self-publishing my books, not only on Delphi, as I ended up also with a volume on social networks. Marco Cantù, Delphi 2009 Handbook Introduction - 17 Beside writing, I keep myself busy with consulting (mostly on applications architectures), helping selling Delphi in Italy, doing code reviews, Delphi mentoring, and general consulting for developers.<br><br> I'm also a frequent speaker at Delphi and general developer conferences, including the new online CodeGear conferences. If you are interested in inviting me to speak at a public event or give a training session (on Delphi 2009 or any advanced subject) at your company location, feel free to send me a note over email. Contact Information Finally, here is some contact information, with my main web sites (my blog, my not-so-up-do-date personal site, my company site): http://blog.marcocantu.com http://www.marcocantu.com http://www.wintech-italia.com My personal web site hosts a specific page devoted to the book, including updates, source code downloads, and other information: http://www.marcocantu.com/dh2009 I have an online mailing list based on a Google group you can sign up from my web site.<br><br> I also run an online newsgroup with a section devoted to dis- cuss my books and their content, available on the web (in the section called cmarco cantu d) at: http://delphi.newswhat.com Finally, feel free to drop me an email at my public address: marco.cantu@gmail.com Marco Cantù, Delphi 2009 Handbook