Search code examples
pointerswinapistructcastinglparam

How can lParam be casted into more than one structures?


I saw this piece of code below in here. I tested it and it works all right.

// g_hLink is the handle of the SysLink control.
case WM_NOTIFY:
    switch (((LPNMHDR)lParam)->code)            // CAST TO NMHDR*
    {
        case NM_CLICK:          // Fall through to the next case.
        case NM_RETURN:
        {
            PNMLINK pNMLink = (PNMLINK)lParam;  // CAST TO NMLINK*
            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;

The parameter lParam is casted to both NMHDR* and NMLINK* types. The documentation of WM_NOTIFY message says that lParam can be casted to NMHDR*, but NMLINK is a different structure which encapsulates NMHDR.

What does actually happen when we cast lParam to an arbitrarily chosen structure between these two?


Solution

  • NMLINK contains NMHDR as its first element:

    struct NMLINK {
      NMHDR hdr;
      LITEM item;
    };
    

    And so pointer to NMLINK equals to the pointer to its first member (which is NMHDR structure sitting at offset 0), they are the same. It means that you can cast NMHDR* to NMLINK*.