Adding entries to CRecentFileList

88 views Asked by At

I know that the CRecentFileList constructor lets you specify an lpszSection as a key.

According to the latest documentation for the Add method, it accepts a file path and states:

virtual void Add(LPCTSTR lpszPathName);

virtual void Add(
    LPCTSTR lpszPathName,
    LPCTSTR lpszAppID);

void Add(
    IShellItem* pItem,
    LPCTSTR lpszAppID);

void Add(
    IShellLink* pLink,
    LPCTSTR lpszAppID);

void Add(
    PIDLIST_ABSOLUTE pidl,
    LPCTSTR lpszAppID);

As you can see, it has the second parameter lpszAppID and the documentation says:

Specifies Application User Model ID for the application.

What is the purpose of this lpszAppID parameter since it does not seem to be used in the MFC code at all? e.g. the API allows us to get the string by using the [] operator but what's the point of lpszAppID?

Side point, why do we have to create these CRecentFileList objects with new? All sample code seem to do this.

1

There are 1 answers

1
xMRi On BEST ANSWER
  1. The code is old. And so for compatibility there are still pointers used. But it is just the internal implementation. The user doesn't sees it.

  2. The recent file is also added to the global recent file list with SHAddToRecentDocs, and this function may use the given id to locate your program to open the file. See the documentation there.

Also this app ids are used in the Windows Toasts, from where it is also possible to open files or link to actions in your application.

Looking at the source code for MFC we can see this:

void CRecentFileList::Add(LPCTSTR lpszPathName, LPCTSTR lpszAppID)
{
    CWinApp* pApp = AfxGetApp();
    if (pApp == NULL || !pApp->IsWindows7())
    {
        Add(lpszPathName);
        return;
    }

    CString strAppID = lpszAppID == NULL ? _T("") : lpszAppID;

    ASSERT(AfxIsValidString(lpszPathName));

    Add(lpszPathName);

    HRESULT hr = S_OK;
    CComPtr<IShellItem> psi = NULL;

#ifdef UNICODE
    hr = _AfxSHCreateItemFromParsingName(lpszPathName, NULL, IID_IShellItem, reinterpret_cast<void**>(&psi));
#else
    {
        USES_CONVERSION;
#pragma warning(suppress: 6255) // There is no workaround for below conversion. Keep the behavior as-is.
        LPOLESTR lpWPath = A2W(lpszPathName);
        hr = _AfxSHCreateItemFromParsingName(lpWPath, NULL, IID_IShellItem, (LPVOID*)&psi);
    }
#endif

    ENSURE(SUCCEEDED(hr));

    Add(psi, strAppID);
}