public static bool GetNewContextMenu(DirectoryInfoEx item, out IntPtr iContextMenuPtr, out IContextMenu iContextMenu) { if (ShellAPI.CoCreateInstance( ref ShellAPI.CLSID_NewMenu, IntPtr.Zero, ShellAPI.CLSCTX.INPROC_SERVER, ref ShellAPI.IID_IContextMenu, out iContextMenuPtr) == ShellAPI.S_OK) { iContextMenu = Marshal.GetTypedObjectForIUnknown(iContextMenuPtr, typeof(IContextMenu)) as IContextMenu; IntPtr iShellExtInitPtr; if (Marshal.QueryInterface( iContextMenuPtr, ref ShellAPI.IID_IShellExtInit, out iShellExtInitPtr) == ShellAPI.S_OK) { IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown( iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit; item.RequestPIDL(pidlLookup => { iShellExtInit.Initialize(pidlLookup.Ptr, IntPtr.Zero, 0); Marshal.ReleaseComObject(iShellExtInit); Marshal.Release(iShellExtInitPtr); }); return(true); } else { if (iContextMenu != null) { Marshal.ReleaseComObject(iContextMenu); iContextMenu = null; } if (iContextMenuPtr != IntPtr.Zero) { Marshal.Release(iContextMenuPtr); iContextMenuPtr = IntPtr.Zero; } return(false); } } else { iContextMenuPtr = IntPtr.Zero; iContextMenu = null; return(false); } }