Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

(Ebook - Pdf) Kick Ass Delphi Programming

.pdf
Скачиваний:
284
Добавлен:
17.08.2013
Размер:
5.02 Mб
Скачать

Here are the main directories on the CD and their contents:

BOBEXPRT

Collection of Delphi “Experts” by well-known Delphi guru Dr. Bob Swart of the Netherlands.

CONTROLS

Shareware and freeware VCL software components to use in creating your own Delphi applications.

DEMOS

Demo software from Delphi component manufacturers. These are not connected with projects in the book!

SOURCE

All the source code and other project files from the book.

TOOLS

WinG and Win32 libraries.

VDM

Sample articles and free subscription offer from Visual Developer Magazine, which has strong Delphi content in every issue, including Ray Konopka’s column, “Delphi By Design.”

The majority of the components and utilities on this CD are shareware, which requires that you pay the authors “on the honor system” if you intend to use the material on a regular basis. It is up to you to make sure that the authors of the program are compensated. Check the documentation files that are related to the application you are using to find out what the restrictions and regulations are. All shareware includes information on sending payment to the authors.

Coriolis Group Contact Information:

Web: http://www.coriolis.com

Email: techsupprt@coriolis.com

Phone: 602-483-0192

Mail: 7339 E. Acoma #7

Scottsdale, AZ 85260

If you need technical support or have a question about something in the book, please send us email describing the problem. Include a description of the involved hardware, software, and any other information that may help us clarify the difficulty.

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

ABOUT THE AUTHORS

Don Taylor is well known to the Borland Pascal community as the founder of the Turbo User Group and the host of GeTUGether, an annual party/seminar for users of Borland languages. Don has written extensively for PC TECHNIQUES and other magazines in recent years. He is co-author of Delphi Programming Explorer and The New Delphi 2 Programming Explorer. In addition to programming, Don enjoys writing and marketing. His company, Turbo Communications, is building a site on the Internet to serve the interests of those in the “Baby Boomer” generation (the site can be found at http://www.best.com/ <turbocom/). Don and his wife Carol live in the hills above Poulsbo, Washington.

Jim Mischel began writing on programming in 1989, and published his first book, Macro Magic with Turbo Assembler, in 1992. His second book, The Developer’s Guide to WINHELP.EXE has been the core around which Jim has developed a seminar series on developing Help content for Windows. He is a co-author of the recent Coriolis bestsellers

Delphi Programming Explorer and The New Delphi 2 Programming Explorer. In his current position he develops cutting-edge games for Maxis. Jim lives in Austin, Texas, with his wife Debra, and operates amateur radio station KB7UQD when he isn’t out chasing armadillos through the bluebonnets.

John Penman is a freelance programmer. He has been writing software since 1979, and has worked in real time and simulation software development as well as diverse activities such as crop husbandry and potato genetics in his native Scotland. John lives with his wife and two children in South Africa, and he can be reached on the Internet at jcp@iafrica.com.

Terence Goggin is Director of Product Management at Nutshell Software Company, in Westlake Village, CA. Nutshell Software offers made-to-order programs as well as a number of add ons for Delphi, including the IM/EX ASCII™ components, which are designed to quickly and easily import and export ASCII data. He can be reached at terenceg@nutshellsotware.com. Nutshell Software can be reached at http://www.nutshellsoftware.com.

Ray Konopka is the author of Developing Custom Delphi Components, published by Coriolis Group Books. Ray is the founder of Raize Software Solutions, Inc., supplier of Delphi component products and cnsulting services. Ray can be reached on the Internet at rkonopka@raize.com.

Jon Shemitz has been a Pascal/Delphi consultant and independent developer for almost thirteen years. He lives in Santa Cruz, California with his partner and their two children, and is contemplating buying a laptop so he can work in his greenhouse. Jon can be reached on the Internet at jon@armory.com or http:/www.armory.com/<jon.

Richard Haven is on the Delphi development team at Borland. He has been working at Borland for over five years, coming to Delphi out of the Paradox and database areas. He has been a client/server database consultant, and has presented technical sessions at many industry conferences around the world. He has over thirty technical articles in print. Contact Richard at rhaven@santacruz.com.

Ed Jordan (whose last name is pronounced JER-din) develops custom Delphi Components and writes about programming, when he isn’t teaching creative writing. He lives in Tampa, Florida.

Acknowledgments

Solomon is generally considered the wisest man in all of history. In Proverbs 3:26 he wrote, “Do not withhold good from those who deserve it, when it is in your power to act.” That excellent advice also applies when “good” means thanks for a job well done.

Working with Jeff Duntemann is a pleasure that must be experienced to be fully appreciated. Jeff has a knack of leading the way and gently prodding you at the same time, all the while making sure you’re tightly involved in the process of birthing a book. It’s a marvelous skill that enables him to coax the very best from his authors. There may never be a way to turn the frustration of unexplained operating system quirks and beta software into something fun - but if it ever happens, Jeff will probably be the one to do it. Thanks, Jeff.

The production staff at the Coriolis Group did a fantastic job of getting everything together right and on time. Thanks Ron, Michelle, Toni and everyone else at Coriolis.

From its inception, Borland International has been a pioneer when it comes to programming tools. Delphi is the pinnacle of their efforts to date. It was hard to imagine how they could improve significantly on the original release, but they have managed to do it with Delphi 2. Thanks to Anders, Zack, Gary,

David and all the other wizards at Borland.

Collaborating on a book isn’t supposed to be easy, but with the great group of guys who worked on this book you’d never know it. To Jim Mischel, John Penman and Terence Goggin: Thanks, guys, for sharing your expert knowledge, and for putting up with my technohumor.

Finally, thanks to everyone who purchases Coriolis Group books. In a world flooded with “formula” products from mainstream publishers, you’ve chosen to buy books from a company created by programmers for the express purpose of publishing meaningful stuff for other programmers. A company that dares to challenge the status quo, and is not afraid to break the rules now and then. To all of you: May you be blessed for taking the Road Less Traveled.

Don Taylor

Dedication

For Carol, who puts the wind in my sails and a song in my heart. Together we steer a course to the stars.

Don Taylor

In memory of Sandy. April 1, 1981 - June 4, 1996.

Jim Mischel

To Jocie, for being a wonderful and loving wife. To Mum and Dad for their unstinting support over the years.

John Penman

To dad.

Terence Goggin

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

CHAPTER 1

32-Bit Console Applications

JIM MISCHEL

Console I/O

Console Filter Programs

Console Command-Line Parsers

Console File I/O

Using Delphi‘s Object Repository

Now that DOS has been stuffed and hung on the wall of Win32 as a minor API, how’s a hacker to do command-line text filters? Well, the POSIX fairy waved her magic wand, and Poof! DOS became The Console, and it’s deja vu all over again.

Windows, OS/2, the Macintosh, and other graphical user interfaces have

been the darlings of the computer press (both technical and non-technical) for many years now. With all that attention focused on writing applications for these GUI environments, it’s sometimes tough to remember that there is another world out there—a world of command-line tools that perform batch processing with a minimum of user input. These tools aren’t especially sexy, but they’re certainly useful. Banks, for example, still process your checks, deposits, and loan payments in batches every night. Insurance and credit card companies also perform nightly updates, as do countless other businesses. (And do they use fancy GUI environments? Ask your bank teller. Or guess.)

Command-line tools aren’t limited to companies running financial programs on big iron. Windows 95 itself comes with quite a few: ATTRIB, DISKCOPY,

FORMAT, FDISK, SORT, and XCOPY among them. Even Delphi comes with some command-line tools. A quick peek in Delphi’s BIN directory reveals (among others) the Resource Compilers (BRC32.EXE and BRCC32.EXE), the Pascal compiler (DCC32.EXE), and the linker (TLINK32.EXE).

Console Applications

Windows 95 and Windows NT support console applications—programs that have no GUI presence but instead run in what is commonly referred to as a “DOS box.” Although these applications don’t have a window, they do have access to the Windows API and the full 32-bit Windows address space (including virtual memory). This is in contrast to Windows 3.1, where GUI programs had access to Windows’ address space, and DOS programs had access to the lower 640K.

In the past, DOS applications got around the 640K limitation through the use of DOS extenders that supported standards like DPMI (DOS Protected Mode Interface) and VCPI (Virtual Control Program Interface). If you had a 16-bit extender, you had access to 16 megabytes of memory. The less common 32-bit extenders gave you access to the full 32-bit address space, and some even supported virtual memory. The problem with DOS extenders is that they are—no matter how well presented—hacks. And many users simply couldn’t get their older machines to reliably run the DOS extenders, and some DOS extenders couldn’t run in a Windows DOS box.

A console application running under Windows 95, on the other hand, is simply a Windows program without a window. There’s no special extender software required, and any computer that can run Windows 95 or Windows NT will support console applications.

So we’ve got the RAM and we’re free of the GUI. What can we do with it?

Filters

Probably the most common use of command line tools in the PC world is the broad category of programs called “filters.” A filter can be anything from a very simple line counter to a complex compiler (like Delphi’s Pascal compiler), a sorting utility, or a batch update program.

All filters operate basically the same way: They’re invoked from the command line and passed arguments that specify options, and input and output files. The filter reads the input, applies some processing (such processing modified by the options specified on the command line), and writes an output file.

Filters typically don’t access the mouse, and in fact, rarely accept user input. If they do accept user input, it’s through a very simple text-oriented interface. Output to the user is normally limited to status reports (“Working, please wait...”), error notifications, and a final “done” message.

In this chapter, we’re going to build a relatively simple filter program with Delphi, and construct a filter program “shell” that you can use to quickly build other filter programs. Along the way, we’ll learn a thing or two about Delphi’s

Object Repository, reusable code, and (shudder) process-oriented programming.

NOTE:

I find it ironic that three years ago I was explaining Windows programming to DOS programmers, telling them how to move away from their process-oriented mindset and move into the wide world of event-driven Windows programming. With the advent of visual development tools like Visual Basic and Delphi, many new programmers started with event-oriented programming and haven’t ever written a process-oriented command line tool. Now I’m explaining process-oriented programming to event-oriented programmers. Plus ça change. One good thing, though: Programmers who understand event-oriented programming have little trouble understanding process-oriented code. The reverse, sadly, is not true.

Console Applications and Delphi

Although it’s possible to write console applications with Delphi, the documentation is suspiciously silent on exactly how such a thing is done. Considering the excellent demo programs that explore so many other facets of Delphi, I found the lack of an example console application surprising. Fortunately, creating a console application with Delphi is not very difficult, although it would have been nice to be informed about a couple of the details. (Trial-and-error is such an inefficient way to learn!)

The simplest console app is, of course, the “Hello, world” program. It’s not an exciting program, but it’s usually the first program I ever write with a new tool because it lets me learn about the tool without having to worry too much about the program itself. And once we’ve created a simple console app with Delphi, we can save the code in the Object Repository so it can be used as the starting point for other console apps. Let’s get started.

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

Hello, Delphi

Start with a new application (File|New Application). First we need to change some of the project options to tell Delphi that we’re creating a console app. Select Project|Options, and on the Linker page of the Project Options dialog box, check the “Generate console application” check box, and then click on OK to save this change.

Since console applications don’t have a main form (or any other form for that matter), we need to remove the Form1 that is automagically created when you start a new application. Select File|Remove from Project, and when the Remove From Project dialog box appears, highlight the line that contains Unit1 and Form1, and click on the OK button. If a message box appears and asks you if you want to save changes to Unit1, say No. You’ll be left with a Delphi screen that has only the Object Inspector. No Form or Unit window will be shown. So where do you write the code?

The one thing that’s left is the project source file. Select View|Project Source, and Delphi will display a text editor window that contains the source of PROJECT1.DPR. It’s this file that we’re going to modify in order to create our first console application. Before you do anything else, select File|Save, and save the project as HELLO.DPR.

Modify the project source in the editor so that it resembles Listing 1.1, save your work, and then press F9 to compile and run the program.

Listing 1.1 The “Hello, Delphi” program

{$APPTYPE CONSOLE} program Hello;

uses Windows;

begin

WriteLn ('Hello, Delphi!'); Write ('Press Enter...'); ReadLn;

end.

The first line in Listing 1.1 is a compiler directive that tells Delphi to create a console application, and must be included at the top of any console application. This line should only be included in programs—not units or libraries (DLLs). The uses statement isn’t necessary for the program (after all, the program isn’t making any Windows API calls), but for some reason, Delphi doesn’t like to save a project that doesn’t have a uses statement (see again my comment about trial-and-error learning). Windows seemed like a fairly innocuous unit to list here, and listing a unit doesn’t mean that it’s linked in—only that Delphi will search that unit if it can’t find an identifier.

The rest of the program is real simple. The string “Hello, Delphi!” is output to the console (that is, the screen), and then you’re prompted to press Enter. I included a prompt for the Enter key because, without it, Delphi just displays a console window (“DOS box”) briefly, runs the program and displays the hello message, and then closes the console window. The prompt lets you see that the program actually works.

Saving a Program Template

The steps required to build a console app aren’t especially difficult, but there are a few details to remember. Rather than building from scratch every time (and forgetting a detail or two), let’s save our little Hello program in the Object Repository so that we’ll have a starting point for other console apps.

Using the Windows Exploder (what we called File Mangler under Windows NT 3.51), create a subdirectory called ConsoleApp in Delphi’s Objrepos subdirectory. If you installed Delphi using the standard options, the full path name will be:

C:\Program Files\Borland\Delphi 2.0\Objrepos\ConsoleApp

Then, select Project|Save Project as from Delphi’s main menu, and save the project as ConsoleApp.dpr (don’t you just love long file names?) in that new directory.

Once you’ve saved the project, add it to the repository by selecting Project|Add to Repository, and fill in the Add to Repository dialog box as shown in Figure 1.1.

FIGURE 1.1 Adding the console app template to the repository.

Once you’ve added the project to the Repository, select File|New from Delphi’s main menu, select the Projects page of the New Items dialog box, and