IShellLink-Shortcuts the Long Way

As an example, let’s consider two system interfaces—IShellLink and IPersistFile. IShellLink is provided by Windows as a means of managing shortcuts—the icons on your desktop that represent programs, directories, printers, and other system resources. IPersistFile is provided by COM as a means for classes to make themselves persistent, that is, to store themselves on disk. The only reason we’re interested in IPersistFile is that the standard implementation of IShellLink uses it to save shell links (shortcuts).

Creating shortcuts is such a useful task that you might expect Windows to provide a simple API function to do it. Since Windows doesn’t, you might expect that the designers of Visual Basic would fill the gap. But they don’t either, or at least not directly. They do it themselves, but they don’t give you a legal means to do it.

There’s a function called fCreateShellLink in the VB5STKIT.DLL provided with the setup kit. If you’re willing to make sure all your customers keep this file even after setup, and if you aren’t worried about changing the Declare for every version, and if you can figure out the undocumented parameters, there it is.
But why don’t they just put it in the language and document it? I guess then I wouldn’t have this excuse to tell how to wrap the Visual Basic-hostile IShellLink interface in a nice, friendly CShortcut class.

I did this once before. In the December, 1996, issue of Visual Basic Program­mer’s Journal, I described how to create a CShortCut class in C++ using the ActiveX Template Library. At the time, I believed it was impossible to use inter­faces from Visual Basic. I was half right. You need to write a Visual Basic–friendly type library for the interface, and you have to write it in a foreign language that doesn’t look much like Visual Basic. But once you figure out how to write the type library (or get one off the CD of this book), you can use it in Visual Basic.

By the way, I didn’t discover this technique on my own. Brian Harris worked it out with a little help from Matt Curland, and the results are in sample programs in the following directories of your Visual Basic CD:

\Tools\Unsupprt\ShellLnk

\Tools\Unsupprt\IHandler

There’s no documentation for these unsupported samples, but hardcore programmers could probably figure out the techniques from the program comments even if I weren’t about to describe them.