Advanced Visual Basic 6: Power Techniques for Everyday Programs with Cdrom
Matthew Curland, Bill Storage · Addison-Wesley Longman Publishing Co., Inc. eBooks · 2000
From the Book: PREFACE: Microsoft Visual Basic has always been positioned by Microsoft as a high-level tool specifically designed to let even inexperienced programmers create Windows applications. VB squarely hits the target of being a high-level language with a rich toolset of rapid-application development features. In addition, VB is also used by a growing number of advanced programmers who make it their language of choice because it gets the job done faster and with fewer bugs than other languages. This advanced group of VB programmers has a love-hate relationship with VB. They love VB because it takes care of the low-level details and lets them concentrate on their customer's business problems. But they hate VB because it doesn't give them control of their program at a low enough level to fully control the system. Since low-level code generally comprises only a small part of a large application, the love wins out and the programmers tolerate the problems. The vast majority of applications require only a little bit of low-level coding: to provide special UI effects, remove performance bottlenecks, reduce memory usage, and solve other problems. These finishing touches might make up only a small percentage of your application, but they are absolutely essential for shipping a professional software product. This book focuses on blowing away the wall that many VB programmers hit as they try to polish their application. This is the same wall that managers must consider as they evaluate technologies to apply to new software projects. Managers often choose to code in a lower-level language because they fear that the project cannot be completed in VB. This choice isoftenunfortunate, because low-level languages are simply not needed for the bulk of today's application development. Precious weeks and months are squandered laboring through mundane C code just in case the project requires some low-level code later on. The easiest way to breach the VB wall is to make use of VB's underlying data types. VB6 works very closely with COM and OLE Automation to do everything from creating objects to managing arrays and strings. Although you don't see the underlying COM types directly in VB, they are always just under the surface: you don't have to dig very far beneath VB's objects, strings, arrays, and Variants in order to find the corresponding IUnknown, BSTR, and SAFEARRAY, and VARIANT data types. COM is based on a binary standard, so the memory layout of all COM types is well documented and easily duplicated. By learning how VB works with COM types, you can open up a whole new world of possibilities in VB. By exploring these data types, I'll break through the thin VB veneer and show you not only what is happening under the surface of normal VB code, but also how to produce your own customized objects. Once you've convinced VB that a piece of customized memory is actually an array or an object, you can use normal VB code to manipulate your data. I am not trying to show you how to write C code in VB. I'll show you how to give VB a boost so that VB code runs better and supports otherwise unavailable capabilities. I have three main goals for readers of this book. Learn how to write better standard VB code. Even if you come away from this book and decide that you don't want to use the advanced techniques, you will still be able to write VB code that is fundamentally more sound and that performs better. Learn a new set of advanced VB programming techniques. You can use these programming techniques to enhance your everyday coding productivity by minimizing code reuse and enabling otherwise unavailable algorithms. Following VB's lead, I have made every attempt to package the low-level technology in this book in such a way that you can easily use it without fully understanding what is happening beneath the surface. Intended Audience This book is specifically for advanced Visual Basic programmers, but it is also for all VB and COM programmers who want a better understanding of how VB works. I strongly advise that anyone reading this text have a working knowledge of either VB or COM. If you don't have a working knowledge of VB, then please buy another book along with this one. You will get a lot of value out of this book even if you don't consider yourself an advanced VB programmer and don't understand the low-level manipulations as well as I do. The CD provides easily accessible code that enables common tasks such as subclassing, as well as the previously inaccessible constructs of object aggregation, calls to function pointers, direct memory sharing, and worker thread creation. With just a little bit more of your own code, you get scoped termination events on user-defined types, lightweight object systems that scale painlessly to thousands of objects, custom window creation, and function overriding. You also get three powerful type library add-ins that let you modify both the type libraries VB consumes and the ones it generates. Although much of the enabling code in this book is very low-level, the book doesn't focus on how to write code at this level. Rather, the goal is to produce a set of tools that you can use repeatedly for everything from full interface implementation reuse to painless hierarchical object models. You can take advantage of the aggregation technologies to greatly improve code reuse by making composite objects that use stock implementations of common interfaces. The longer I use this technology, the more I find myself using it to simplify my day-to-day code. Every VB6 programmer will find code in this book that can be applied to their everyday programming problems. Future Compatibility The marketing machine for VB7 is already in motion, so you may already be aware that some of the technology I'll show you is natively available in VB7. For example, VB7 has native support for overriding functions with inheritance, calling function pointers, and providing pointers to class functions. If you make use of the technology in this book to use these design principles now, you can move your application to VB7 simply by replacing the framework calls with the native VB7 equivalents. Only the framework code should change: your design constructs and code logic can remain the same. I'll post more information on VB7 compatibility on the book's web site at ...