Startup Cop |
|
Reading and Writing Shortcuts Continued from Disabling and Removing Items The MakeALink() and ReadALink() functions, both defined in the module Allfuncs.pas, are general purpose functions designed to read and write shortcuts to files and folders. Both take the exact same set of arguments, and both return True if successful. The first argument, S, is simply the name of the LNK file, and the last is a pointer-to-integer that holds the shortcut's icon index, if used. In between come five pChar arguments: pPath, pArgs, pWork, pIcon, and pDesc. These represent, respectively, the full pathname of the target file, the command-line arguments (if any), the working directory, the full pathname of the file containing the shortcut's icon, and the description. Most of these are self-explanatory, but the description is probably less familiar. According to the Platform SDK documentation: "Shortcuts have descriptions, but the user never sees them. An application can use a description to store any text information." The calling process can pass nil for any of the pointer arguments, indicating that it does not want to get or set that particular shortcut property. Each non-nil pChar argument passed to ReadALink() must point to a buffer of at least 260 characters. The MakeALink() and ReadALink() functions both declare three local variables named vUNK, vISL, and vIPF, respectively of type IUnknown, IShellLink, and IPersistFile. These types directly correspond to the COM interfaces of the same names. The function initializes the vUNK variable by passing the class ID for the IShellLink interface (CLSID_IShellLink) to the Delphi built-in function CreateComObject(), which in turn uses the API function CoCreateInstance() to create an instance of the desired interface. The vISL variable is initialized by typecasting vUNK to the type IShellLink; the vIPF variable is initialized by typecasting vUNK to the type IPersistFile. These two variables give the program easy access to the vUNK variable's IShellLink and IPersistFile interface methods. Both functions convert the passed filename to Unicode using the Delphi function StringToWideChar(). This is necessary because the IPersistFile interface's Load and Save methods expect a filename in Unicode. With these preparations complete, the functions simply call on methods of the interface variables. ReadALink() calls the IPersistFile.Load() method to load the shortcut from disk into the vUNK object. Then it fills each non-nil pChar argument by calling the appropriate method of the IShellLink interface. For example, if the pPath argument is not nil, it calls IShellLink.GetPath(). If it can't load the shortcut, or if an exception occurs while getting data for the pChar arguments, it returns False. MakeALink() stores each non-nil pChar argument by calling the appropriate method of the IShellLink interface. For example, if the pWork argument is not nil, it calls IShellLink.SetWorkingDirectory(). It then writes out the actual shortcut file by calling IPersistFile.Save(). If it cannot save the shortcut, or if an exception occurs while attempting to set the object's data, it returns False. Startup Cop uses the ReadALink() function to read each LNK file in the StartUp and Common StartUp menu folders, and in the corresponding "Disabled StartUp Items" and "Startup Cop Removals" folders. It passes the shortcut name and a buffer for the target file's pathname, and passes nil for all the other arguments. If the item is not actually a valid shortcut, the call to ReadALink() will return False; in that case Startup Cop prefixes the item's name with an asterisk. When you click one of the Shortcut buttons on the Restore profile tab, Startup Cop calls MakeALink() to create the shortcut. To get the location of the folder that defines the desktop, it passes the flag CSIDL_DESKTOPDIRECTORY to the SHGetSpecialFolderLocation() API function. Then it creates a shortcut in this folder whose name is the same as that of the profile, prefixed with [L] or [R] if the shortcut will log off or restart after restoring the profile. MakeALink() passes its own executable name as the target filename, passes the appropriate command-line arguments, and passes its own location as the working directory. It sets the icon path and icon index arguments to nil, causing the shortcut to use Startup Cop's main icon. The shortcut description is set to "Shortcut created by PC Magazine's Startup Cop utility". Finally, Startup Cop notifies the user that the shortcut creation succeeded, and summarizes what will happen when the shortcut is activated. Next: Simulating Context Help Published in the 4/20/99 issue of PC Magazine. |