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.