Rolling Your Own

The source code for the Windows API type library is provided on the CD for this book, but I’m not going to describe it. This is a Basic book, but type libraries aren’t written in Basic. They’re written in Interface Description Language (IDL), which is similar to the subset of C used for writing function prototypes, structures, and constants. You can use a program called MIDL to compile IDL source code into type libraries. If you thought type libraries were written in Object Description Language (ODL) and compiled with MKTYPLIB, get with the program. That was last year.

Unfortunately, MIDL isn’t provided with Visual Basic. Instead, the Visual Basic CD provides MKTYPLIB in the Tools\Unsupprt\Typlib directory. I don’t know why they chose to provide the ancient MKTYPLIB instead of the newer MIDL. Either tool can be used not only for creating API type libraries but also for creating interface type libraries. Perhaps MKTYPLIB works better for interfaces, but MIDL is by far the better program for creating API type libraries. In any case, all my type library source code uses IDL rather than ODL, and I advise you to get your hands on MIDL version 3 or higher (it comes with newer versions of Visual C++) if you ever want to write type library entries for the API functions I don’t handle. That’s all I’m going to say on the subject. The IDL source code is on the CD that accompanies this book if you want to know more. Most people don’t need to write API type libraries anyway because I’ve already done it.

So now that I’ve explained why you never need to write Declare statements again, let’s move on to the rest of the chapter where I’ll talk about how to write Declare statements. That might sound like a contradiction, but no matter. To master the Windows API, you still need to understand what’s going on behind the scenes, and Declare statements are the most Basic way to learn that. You won’t regret anything you learn here even if you never use it.

You might be tempted to skip this chapter because you already have both WIN.TLB and WIN32API.TXT as alternate sources of API declarations, but before you do, listen to this little story of heartbreak, despair, and wasted effort.

WritePrivateProfileString used to be one of the most popular functions in the Windows API because it allowed you to save data between sessions—functionality that Basic should have provided long ago but didn’t until version 4. Everyone wanted to use this function. Everyone pasted the declaration from the API declare file. Everyone had trouble using it. Everyone then called Microsoft Product Support or, if they worked at Microsoft, sent e-mail to the internal Visual Basic programming alias. I used to see at least one such query a month about WritePrivateProfileString.

The problem was that the declaration shipped with Visual Basic was, to put it charitably, strange. It wasn’t wrong. In fact, a note in the text file (which no one

Problem:  Compare counting with various Basic types: Integer, Long, Single, Double, Currency, and Variant.
Problem Native Code P-Code
Integer 0.0153 sec 0.4012 sec
Long 0.0224 sec 0.4250 sec
Single 0.1124 sec 0.5248 sec
Double 0.1402 sec 0.5185 sec
Currency 0.3232 sec 0.6179 sec
Variant 0.6578 sec 1.0241 sec

Conclusion:  Notice how the compiler gives huge speedups for simple types, but as the types get larger and more complex, the margin narrows. For example, most of the work for handling Variants is done by the Automation system, so native code isn’t going to make much difference.

seemed to read) explained how to use the declaration. I would say, however, that of the four or five alternatives for writing this declaration, the one chosen was the worst and the most likely to cause problems. Fortunately, the GetSetting, SaveSetting, DeleteSetting, and GetAllSettings functions make this problem
irrelevant.

The point is, don’t trust anyone’s declarations—especially not mine. If you roll your own (or at least know how), you won’t suffer because of someone else’s bad choices. You have a lot of options, many of which I’ll show you later in this chapter.