SHAddToRecentDocs() only works once within a running program !

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]
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
You could always maintain your own internal list of documents, and add them yourself to your menu/other control used to display...
muttley3141Author Commented:
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.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.


>> 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.
muttley3141Author Commented:
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.
>> 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.
muttley3141Author Commented:
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.
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.
muttley3141Author Commented:
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.
muttley3141Author Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.