Home Articles Books Downloads FAQs Tips

Q: Add a file to the list of recently opened files


Answer:

Windows 95 and Windows NT 4 allow users to re-open previous files by selecting the Documents menu option under the Start taskbar menu. For example, if you open a document in Microsoft Word, the filename is added to the recently opened documents list so you can access the file from the taskbar. To add a document to the list of recently opened files, use the SHAddToRecentDocs API function. Here is an example:

#include <shlobj.h>
...
SHAddToRecentDocs(SHARD_PATH, "c:\\cbuilder\\readme.hlp");

Note: The first argument to SHAddToRecentDocs should be either SHARD_PATH or SHARD_PIDL. If you want to pass the filename as a string, use the SHARD_PATH value. SHARD_PIDL allows you to pass a pointer to an ITEMIDLIST (the ITEMIDLIST structure relates to shell programming with the shell namespace functions; SHGetDesktopFolder, SHGetMalloc and friends).

Note: You can clear all of the items in the list of recent files by passing NULL as the second example.

// Clear out all documents in the list
SHAddToRecentDocs(SHARD_PATH, NULL);

Note: While testing the code for this FAQ, I found out that the shell will not allow you to add a file to the recent files list if that file does not have an associated program. For example, I tried to add the file UNIT1.~H to the list. Since files with the ~H extension don't have a registered program that opens them, the shell refused to put UNIT1.~H in the list. This makes sense. When you select a file from the recent files list, Windows opens that file with its associated viewer. It can't open a file that has no associated viewer. This behavior was witnessed on Windows NT 4.

Note: There is a a problem with shlobj.h and BCB 5. If you include shlobj.h in a BCB5 GUI project, you may get a strange compiler error, such as this:

[C++ Error] shlobj.h(1762): E2238 Multiple declaration for 'FVSHOWINFO'
[C++ Error] shlobj.h(1936): E2238 Multiple declaration for 'FOLDERSETTINGS'
[C++ Error] shlobj.h(3717): E2238 Multiple declaration for 'DESKBANDINFO'
[C++ Error] shlobj.h(4808): E2238 Multiple declaration for 'SHELLFLAGSTATE'

You can eliminate these errors by adding the string NO_WIN32_LEAN_AND_MEAN to the conditional defines of your project. To see what impact this has, open the file vcl0.h in a text editor.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.