Learn how to a build a cloud-first strategyRegister Now


SHAddToRecentDocs() only works once within a running program !

Posted on 2008-11-08
Medium Priority
Last Modified: 2013-11-20
I can use SHAddToRecentDocs() with a filename fine and it adds that filename to the "my recent documents" list; I have a button on the program's main dialog which adds the filename to the MR list.

However, if I do the following sequence of actions:

Get the program to add the filename to the MRD list
Bring up the MRD list
The document is present on it.
Delete the document from the MRD list
Get the program to add the filename to the MRD list again
Bring up the MRD list
The document is no longer there.

Presumably, the program is doing some sort of caching: it knows it's added the document once so doesn't try to add it again if called with the same filename.

Does anybody know how to clear this caching mechanism if that is indeed what it is doing.

This isn't a show-stopper but is very odd.

Richard [in SG1]
Question by:muttley3141
  • 5
  • 4
LVL 19

Expert Comment

ID: 22913354
Clearing the entire MRD list by passing NULL as the second argument works however it unfortunately clears all items in the MRD list and not just a particular file:

::SHAddToRecentDocs(SHARD_PATH, NULL);
::SHAddToRecentDocs(SHARD_PATH, _T("C:\\test.txt"));

Another expert might have better advice however.
LVL 19

Expert Comment

ID: 22920632
You could always maintain your own internal list of documents, and add them yourself to your menu/other control used to display...

Author Comment

ID: 22921542
The intention is to be able to add "my" document to the "My Recent Documents" list. Maintaining my own list of documents does not help.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 19

Expert Comment

ID: 22941233

>> Maintaining my own list of documents does not help.
OK, what I meant was as follows:
According to the original question text, you say that deleting the document from the MRD list, then reopening it results in the framework not re-adding the item to the list.
This, I feel, can be expected.  As far as the framework is concerned, the entry has already been added.  It is not going to check that it is already there when it thinks it is.
This is why I suggested maintaining your own list of documents.  In the following example, I am going to assume that your MRD list is part of the File menu, as is so often the case.
Open the document, and add it to some internal storage.  Let the framework add it to the File menu.
You then pull down the File menu and the entry is present.  All good.  You then proceed to delete the item.
Now, in your update handler for the File menu, you iterate your list of MRDs, and check the file menu to see if they are present.  If they are not, append the entry yourself in the update handler.  If it is, do nothing.
How exactly you implement this is up to you.
>> Does anybody know how to clear this caching mechanism if that is indeed what it is doing.
As said before, I think that once the framework has added the item to the menu, it will not attempt to add it again.  It does not know if you delete it or not.

Author Comment

ID: 22941392
The framework has no business thinking that it already is there. The My Recent Documents list is shared across all programs for the one user and any program can add to it at any time.

No, the My Recent Documents list is not part of the File Menu: it's off the Start Menu.

What you are suggesting does not help.

When I do SHAddToRecentDocs(), I expect it to add the given file and not just assume that it's aready there.

As I said, maintaining my own list of what files my and other programs had/have opened is not going to help. It's no good if the My Recent Documents list does not get updated.
LVL 19

Expert Comment

ID: 22941534
>> No, the My Recent Documents list is not part of the File Menu: it's off the Start Menu.

Oh, I do appologise. I was not quite with you on your term "recent documents".  My mistake.
 >> As I said, maintaining my own list of what files...
Yes, I can now see that.
I will look into this as I am now intrigued.

Author Comment

ID: 22941548
I have just checked with another program (TextPad, as it happens) and it behaves the same way as my program:

1. I create a file aaa.txt in TextPad
2. I save it from TextPad
3. I look in the Start | My Recent Documents list; aaa.txt is there.
4. I delete aaa.txt from the S | MRD list; it is no longer there.
5. I leave TextPad open for say 15 minutes, but with all files closed.
6. I open up aaa.txt again from TEXTPAD's MRU (not MRD) list; it opens OK.
7. I look at the S | MRD list; aaa.txt is not there.

This is the same way as my program works but it is not what I would expect.
Is this the way CUA says it should work ?

No matter, when I do a SHAddToRecentDocs(), it should do what the name of the function call implies, not think "Oh, I've already added it; I won't do it again". How does the program know what other programs people have been using and what those have done to the MRD list; it can't (and shouldn't) know.

The only thing I can think of is to spawn a new .EXE whose sole purpose in life is to add a given filename to the S|MRD list.
LVL 19

Expert Comment

ID: 22941665
Yes, I can replicate this.
However, you should find that if you save the doc, and then re-open it via a double click in explorer, the item does get added to the MRD menu again.  It does not add when you open the document from applications own "recent document" list.  Perhaps this is a shortcoming in the file->open code for the application concerned.
Either way, if you are calling SHAddToRecentDocs when you open a document, this should not be happening.  I will look into this, so please leave this question open for ~24 hours.

Author Comment

ID: 22941794
Yes, if I manually delete the file from the S|MRD list and then reopen it from any other program (such as Explorer) then it will open that file and add it to the S|MRD list. But it only does that because Explorer does a ShellExecute() or whatever to open (a new version of) the application concerned; if I invoked the "same" filename more than once from any other sort of running program then I suspect it wouldn't add it to the S|MRD list a second time unless that program is exited and restarted.

Sorry about the naming anomaly. On the UK versions of Windows XP Home, the thing off the Start Menu is called "My Recent Documents". It might be called something else on other-national versions of Windows.

Your comment about this being a shortcoming in the file|open code for the application concerned doesn't really apply to me because I am maintaining my own File|MRU list; I am adding/deleting/moving items to it programmatically as my program goes about its business. I am then opening files when a menu handler for one of the menu items corresponding to the files thereon is invoked. All this works perfectly well.

I just expected to be able to programmatically add items to the S|MRD list with a simple call.

If MFC provides its own File/Open/whatever mechanism, and that somehow integrates with a framework-provided MRU list and/or the S|MRD list, I have no idea how that works; I've never uses such.

Accepted Solution

muttley3141 earned 0 total points
ID: 22983696
I've decided that what I'm going to do is to have a short-lived executable whose sole purpose in life is to add the given file to the Start|MRD list. Hopefully, the fact that it's a new running executable every time will cause whatever internal caching is happening, to be overridden.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question