2

In this Microsoft example on how to process the WM_NOTIFY message for a SysLink control they have this code,g_hLinkbeing the handle of the SysLink control:

// g_hLink is the handle of the SysLink control.
case WM_NOTIFY:

    switch (((LPNMHDR)lParam)->code)
    {
    
    case NM_CLICK:          // Fall through to the next case.
    
    case NM_RETURN:
        {
            PNMLINK pNMLink = (PNMLINK)lParam;
            LITEM   item    = pNMLink->item;
            
            if ((((LPNMHDR)lParam)->hwndFrom == g_hLink) && (item.iLink == 0))
              {
                ShellExecute(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
              }
            
            else if (wcscmp(item.szID, L"idInfo") == 0)
              {
                MessageBox(hDlg, L"This isn't much help.", L"Example", MB_OK);
              }
            
            break;
        }
    }
    
    break;

I don't uderstand why the (((LPNMHDR)lParam)->hwndFrom == g_hLink) condition is not needed for the else clause?

Or is this simply an error in the example?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 3
    Not so much an *error* but an oversight, probably. The assumption is made that there is no other control having a link with an ID of `"idInfo"`. – Adrian Mole Aug 20 '21 at 10:36
  • 2
    Good question. I'd vote for a doc bug. The corresponding source (archived) doesn't even have the first check: https://github.com/microsoftarchive/msdn-code-gallery-microsoft/blob/master/OneCodeTeam/Windows%20common%20controls%20demo%20(CppWindowsCommonControls)/%5BC%2B%2B%5D-Windows%20common%20controls%20demo%20(CppWindowsCommonControls)/C%2B%2B/CppWindowsCommonControls/CppWindowsCommonControls.cpp – Simon Mourier Aug 20 '21 at 10:42
  • 3
    Technically, both checks are coded wrong I think. For one thing, both of those `NM_...` messages are used by multiple control types, not just SysLink, so the code should be validating the `hwndFrom` before dereferencing `pNMLink->item`. And per the [`LITEM` documentation](https://learn.microsoft.com/en-us/windows/win32/api/commctrl/ns-commctrl-litem), `item.mask` should be checked to know when the `szID` and `szUrl` fields contain valid data, though `item.iLink` should always be valid – Remy Lebeau Aug 20 '21 at 14:40

0 Answers0