Partilhar via


WMI Shell Folder GetDisplayNameOf - untangling the flags

So I have a bit of a beef with GetDisplayNameOf - I don't like the flags.  I understand that when it was created, the flexibility of flags made a ton of sense.  It wasn't well understood what potentially wildly different uses might be needed, and flags are a good alternative to a giant enumeration.  But I think there are really only about 4 possibilities that matter most of the time (caveat: I'm still pretty new to shell programming).  It's a pain to figure out the flags and there are combinations that don't even make sense.  It seems to make a lot more sense to me to use an enumeration (like in IShellItem).

 

Anyway, I ended up liking a solution that very similar to the one that was put into the Registry sample that a few of us are putting together (more on that later).  I created four different functions for the different GetDisplayNameOf possibilities and hopefully the table that links them to the flags isn't too scary.

 

HRESULT CWMIFldr::GetDisplayNameOf(

                                   /* [in] */ PCUITEMID_CHILD pidl,

                                   /* [in] */ SHGDNF uFlags,

                                   /* [out] */ STRRET *pName)

{

    TRACEFNENTER();

    HRESULT hr;

 

    ZeroMemory(pName, sizeof(*pName));

 

    if (!ILIsEmpty(pidl))

    {

        static const DisplayNameFn s_rgSIGDNMapping[2][2] =

        {

            // unfriendly // friendly

            {{&CWMIFldr::_GetAbsoluteParsingName}, {&CWMIFldr::_GetAbsoluteDisplayName}}, // absolute

            {{&CWMIFldr::_GetRelativeParsingName}, {&CWMIFldr::_GetRelativeDisplayName}}, // relative

        };

 

        bool fRelative = (uFlags & SHGDN_INFOLDER) == SHGDN_INFOLDER;

        bool fFriendly = (uFlags & SHGDN_FORPARSING) != SHGDN_FORPARSING

            || (uFlags & SHGDN_FORADDRESSBAR) == SHGDN_FORADDRESSBAR;

 

        PWSTR pszDisplay;

        hr = (this->*s_rgSIGDNMapping[fRelative][fFriendly])(pidl, &pszDisplay);

   if (SUCCEEDED(hr))

        {

            pName->uType = STRRET_WSTR;

            pName->pOleStr = pszDisplay;

        }

    }

    else

    {

        hr = E_NOTIMPL;

    }

 

    TRACEFNEXIT();

    return hr;

}

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm