A type library is a binary file containing all the type information you need to use procedures or classes in DLLs. Forget about classes for the moment; in this chapter, we’re interested only in procedures and any constants and types those procedures use.
You get lots of type libraries with Visual Basic. You can examine the standard ones by clicking the Object Browser toolbar button or by pressing F2. If you click the Libraries drop-down list box of the Object Browser, you’ll see short names for these standard libraries: stdole, VB, VBA, and VBRUN. If you select one of these libraries, you’ll see its full name displayed in the bottom pane. You’ll also see the type libraries for your current application and for any additional controls or components used by your project.
If you open any of the sample projects provided with this book, you’ll also see a type library with the short name Win and the full name Windows API (ANSI). The Classes pane on the left will show the function groups, types, and enums of the library. The Members pane on the right shows the members of the element chosen on the left, as shown in Figure 2-1. What’s it for? In short, using this type library is equivalent to including WIN32API.TXT in your project—with one big difference. You don’t pay a resource penalty for any type library elements you don’t use.
Figure 2-1. Browsing in the Windows API type library.
For example, WIN32API.TXT contains about 1500 Declare statements, most of which you would never use from Visual Basic even in your wildest dreams. The hardest of hardcore Basic programmers would be lucky to use 100 in a single project. If you were foolish enough to load WIN32API.TXT into a project, you would pay a memory penalty of about 20 bytes minimum for each Declare in the file—and that’s not even counting the constants. So nobody actually loads all of WIN32API.TXT directly into their programs. Instead, they cut and paste the parts they need for each project. But no more.
WIN.TLB isn’t as comprehensive as WIN32API.TXT. It contains over 700 function entries focused specifically on those functions Visual Basic programmers are most likely to need—as chosen and implemented by yours truly. In contrast, WIN32API.TXT was implemented by a program that converted C include files to Declare statements in a somewhat mechanical and sometimes inaccurate way. So whom do you trust? Me? Or a machine? Unfortunately, WIN32API.TXT has a well-earned reputation for being full of errors. The first version of my type library also had a few troublesome errors, and I doubt that this version will be without sin. That’s one reason this chapter tells you how to roll your own. But the biggest difference between WIN.TLB and WIN32API.TXT is that if you use only three functions, you pay for only three functions. In this sense, a type library is more like a C++ header file or a Pascal unit than like a Basic module.
To include the type library in every project you create from now on, simply load it into AUTOLOAD.VBP and into your favorite project templates. You might also want to load it into existing projects to replace Declare and Const statements. Figure 2-2 shows the References dialog box, where you load type libraries. The checked references are the type libraries used by your current project. The unchecked references are others that have been registered in the Windows Registry.
Figure 2-2. The References dialog box.
If a type library has already been registered, you’ll see it in the References dialog box. The long and short of it is that if you ran the setup program provided on the companion CD, the Windows API type library is already registered on your machine. That’s because setup runs a program called REGTLB to register the type libraries. You can run this program yourself if you ever have any problem registering a type library. (Run it once without arguments to get a syntax screen.) I’ll talk about the code for this program later in this chapter. You can also register a type library by clicking the Browse button in the References dialog box.
Your customers don’t need the Windows API type library to run programs that you develop using the type library. The exception to this rule is if you sell programming tools containing Visual Basic source code that uses the type library. (In that case, your customers should also buy this book.) You can build a setup that copies the type library files to the customer’s disk and calls REGTLB to register them (as the setup program on the companion CD does).
Normally, you’ll want to use WIN.TLB, the ANSI version of the library that works on Microsoft Windows 95 and Microsoft Windows NT. But if you’re targeting Windows NT, you can use the Unicode version WINU.TLB. It is faster and more efficient, but it doesn’t work on Windows 95.