27

Network connection shortcut (Ethernet, Wi-Fi, etc.) have different context menus depending on the connection state (connected/disconnected). I use the following code (Delphi) to retrieve and display this menu.

var pidl, child: PItemIdList;
    pFolder: IShellFolder;
    pMenu: IContextMenu;
    menu: HMENU;
begin
  SHParseDisplayName(PChar('%USERPROFILE%\Desktop\eth0.lnk'), nil, pidl, 0, PDWORD(nil)^);
  SHBindToParent(pidl, IID_IShellFolder, Pointer(pFolder), child);
  CoTaskMemFree(pidl);
  pFolder.GetUIObjectOf(0, 1, child, IID_IContextMenu, nil, pMenu);
  menu := CreatePopupMenu;
  pMenu.QueryContextMenu(menu, 0, 0, $7fff, CMF_NORMAL);
  TrackPopupMenuEx(menu, TPM_LEFTBUTTON, 0, 0, Handle, nil);
  DestroyMenu(menu);
end;

But after changing the connection state, I keep getting the old menu. After restarting the app I sometimes get the correct menu, however, most of the time I don't.

Why it happens and how to fix it?

OS: both 32- and 64-bit Windows 7/8/10

C++ code:

PIDLIST_ABSOLUTE pidl;
if SUCCEEDED(SHParseDisplayName(L"%USERPROFILE%\\Desktop\\eth0.lnk", NULL, &pidl, 0, NULL))
{
  PCUITEMID_CHILD child;
  CComQIPtr<IShellFolder> pFolder;
  if SUCCEEDED(SHBindToParent(pidl, IID_IShellFolder, (void**)&pFolder, &child))
  {
    CComQIPtr<IContextMenu> pMenu;
    if SUCCEEDED(pFolder->GetUIObjectOf(0, 1, &child, IID_IContextMenu, NULL, (void**)&pMenu))
    {
      HMENU menu = CreatePopupMenu();
      if SUCCEEDED(pMenu->QueryContextMenu(menu, 0, 0, 0x7fff, CMF_NORMAL))
        TrackPopupMenuEx(menu, TPM_LEFTBUTTON, 0, 0, hWnd, NULL);
      DestroyMenu(menu);
    }
  }
  CoTaskMemFree(pidl);
}

Add: Maybe it Windows bug. Any examples from internet and file managers like Explorer (XYPlorer, Explorer++, etc.) have the same problem. Now I see the same issue on Windows 10 Explorer. If you make a shortcut, by Drag & Drop, to a network connection from "Control Panel\All Control Panel Items\Network and Sharing Center\Change adapter settings\adapter_name" on your Desktop, you'll see the same issue.

Jishan Shaikh
  • 1,572
  • 2
  • 13
  • 31
Asaq
  • 521
  • 5
  • 13
  • Does the menu update immediately in Explorer? Maybe it just takes a little while. – Jonathan Potter Jan 19 '16 at 10:20
  • Yes, the menu is updated immediately in Explorer. Shell extensions do not have this problem e.g. Classic Shell - Start Menu, but if it is run in a separate process (not Explorer.exe) it will have the same problem. – Asaq Jan 19 '16 at 10:46
  • It might update after you right-click on the shortcut and not while programmatically fetching it. Try to get the stat differently or refresh the state somehow. – CodeJunkie Apr 13 '22 at 05:26
  • No, as OP added later, the shortcut context menu is not updated even in Windows Explorer windows. Only the original in Control Panel does have the correct context menu. – Iziminza Apr 24 '22 at 15:04

1 Answers1

0

I think the issue is related to the context menu information for the network connection shortcut being cached, windows might not automatically refresh the cached information when you change the connection state.

You may need to manually refresh the cache by calling the function SHChangeNotify, it will notify the system of an event that an application has performed and trigger a cache refresh.
Here's an example in Delphi:

begin
// ... your code here ...

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;

And in C++:

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);

To ensure that the context menu is up-to-date, try calling SHChangeNotify before you display.

Lemonina
  • 712
  • 2
  • 14