private void ReleaseAll() { if (null != this._oContextMenu) { Marshal.ReleaseComObject(this._oContextMenu); this._oContextMenu = null; } if (null != this._oContextMenu2) { Marshal.ReleaseComObject(this._oContextMenu2); this._oContextMenu2 = null; } if (null != this._oContextMenu3) { Marshal.ReleaseComObject(this._oContextMenu3); this._oContextMenu3 = null; } if (null != this._oDesktopFolder) { Marshal.ReleaseComObject(this._oDesktopFolder); this._oDesktopFolder = null; } if (null != this._oParentFolder) { Marshal.ReleaseComObject(this._oParentFolder); this._oParentFolder = null; } if (null != this._arrPIDLs) { this.FreePIDLs(this._arrPIDLs); this._arrPIDLs = null; } }
public void ShowContextMenu(Point pointScreen) { IntPtr intPtr = IntPtr.Zero; IntPtr zero = IntPtr.Zero; IntPtr zero2 = IntPtr.Zero; IntPtr zero3 = IntPtr.Zero; try { if (null == this._arrPIDLs) { this.ReleaseAll(); } else if (!this.GetContextMenuInterfaces(this._oParentFolder, this._arrPIDLs, out zero)) { this.ReleaseAll(); } else { intPtr = ShellContextMenu.CreatePopupMenu(); int num = this._oContextMenu.QueryContextMenu(intPtr, 0u, 1u, 30000u, ShellContextMenu.CMF.EXPLORE | (((Control.ModifierKeys & Keys.Shift) != Keys.None) ? ShellContextMenu.CMF.EXTENDEDVERBS : ShellContextMenu.CMF.NORMAL)); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu2, out zero2); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu3, out zero3); this._oContextMenu2 = (ShellContextMenu.IContextMenu2)Marshal.GetTypedObjectForIUnknown(zero2, typeof(ShellContextMenu.IContextMenu2)); this._oContextMenu3 = (ShellContextMenu.IContextMenu3)Marshal.GetTypedObjectForIUnknown(zero3, typeof(ShellContextMenu.IContextMenu3)); uint num2 = ShellContextMenu.TrackPopupMenuEx(intPtr, ShellContextMenu.TPM.RETURNCMD, pointScreen.X, pointScreen.Y, base.Handle, IntPtr.Zero); ShellContextMenu.DestroyMenu(intPtr); intPtr = IntPtr.Zero; if (num2 != 0u) { this.InvokeCommand(this._oContextMenu, num2, this._strParentFolder, pointScreen); } } } catch { throw; } finally { if (intPtr != IntPtr.Zero) { ShellContextMenu.DestroyMenu(intPtr); } if (zero != IntPtr.Zero) { Marshal.Release(zero); } if (zero2 != IntPtr.Zero) { Marshal.Release(zero2); } if (zero3 != IntPtr.Zero) { Marshal.Release(zero3); } this.ReleaseAll(); } }
public int ShowContextMenu(Point pointScreen, string[] AddedItems) { IntPtr intPtr = IntPtr.Zero; IntPtr zero = IntPtr.Zero; IntPtr zero2 = IntPtr.Zero; IntPtr zero3 = IntPtr.Zero; int num = -1; int result; try { if (null == this._arrPIDLs) { this.ReleaseAll(); result = -2; } else if (!this.GetContextMenuInterfaces(this._oParentFolder, this._arrPIDLs, out zero)) { this.ReleaseAll(); result = -2; } else { intPtr = ShellContextMenu.CreatePopupMenu(); int num2 = this._oContextMenu.QueryContextMenu(intPtr, 0u, 1u, 30000u, ShellContextMenu.CMF.EXPLORE | (((Control.ModifierKeys & Keys.Shift) != Keys.None) ? ShellContextMenu.CMF.EXTENDEDVERBS : ShellContextMenu.CMF.NORMAL)); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu2, out zero2); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu3, out zero3); this._oContextMenu2 = (ShellContextMenu.IContextMenu2)Marshal.GetTypedObjectForIUnknown(zero2, typeof(ShellContextMenu.IContextMenu2)); this._oContextMenu3 = (ShellContextMenu.IContextMenu3)Marshal.GetTypedObjectForIUnknown(zero3, typeof(ShellContextMenu.IContextMenu3)); for (int i = 0; i < AddedItems.Length; i++) { ShellAPI.InsertMenu(intPtr, (uint)i, ShellAPI.MFT.BYPOSITION, (uint)(30001 + i), AddedItems[i]); } ShellAPI.InsertMenu(intPtr, (uint)AddedItems.Length, ShellAPI.MFT.SEPARATOR | ShellAPI.MFT.BYPOSITION, (uint)(30001 + AddedItems.Length), "-"); uint num3 = ShellContextMenu.TrackPopupMenuEx(intPtr, ShellContextMenu.TPM.RETURNCMD, pointScreen.X, pointScreen.Y, base.Handle, IntPtr.Zero); ShellContextMenu.DestroyMenu(intPtr); intPtr = IntPtr.Zero; if (AddedItems.Length > 0 && num3 >= 30001u && (ulong)num3 <= (ulong)((long)(AddedItems.Length - 1 + 30001))) { num = (int)(num3 - 30001u); } else if (num3 != 0u) { this.InvokeCommand(this._oContextMenu, num3, this._strParentFolder, pointScreen); } result = num; } } catch { Console.WriteLine("Error in Context Menu generation"); result = -1; } finally { if (intPtr != IntPtr.Zero) { ShellContextMenu.DestroyMenu(intPtr); } if (zero != IntPtr.Zero) { Marshal.Release(zero); } if (zero2 != IntPtr.Zero) { Marshal.Release(zero2); } if (zero3 != IntPtr.Zero) { Marshal.Release(zero3); } this.ReleaseAll(); } return(result); }