Пример #1
0
        public bool GetNewContextMenu(IListItemEx item, out IntPtr iContextMenuPtr, out IContextMenu iContextMenu)
        {
            Guid CLSID_NewMenu = new Guid("{D969A300-E7FF-11d0-A93B-00A0C90F2719}");
            Guid iicm          = typeof(IContextMenu).GUID;
            Guid iise          = typeof(IShellExtInit).GUID;

            if (Ole32.CoCreateInstance(
                    ref CLSID_NewMenu,
                    IntPtr.Zero,
                    Ole32.CLSCTX.INPROC_SERVER,
                    ref iicm,
                    out iContextMenuPtr) == (int)HResult.S_OK)
            {
                iContextMenu = Marshal.GetObjectForIUnknown(iContextMenuPtr) as IContextMenu;

                IntPtr iShellExtInitPtr;
                if (Marshal.QueryInterface(
                        iContextMenuPtr,
                        ref iise,
                        out iShellExtInitPtr) == (int)HResult.S_OK)
                {
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(
                        iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    try {
                        iShellExtInit.Initialize(item.PIDL, null, 0);

                        Marshal.ReleaseComObject(iShellExtInit);
                        Marshal.Release(iShellExtInitPtr);
                        return(true);
                    } finally {
                    }
                }
                else
                {
                    if (iContextMenu != null)
                    {
                        Marshal.ReleaseComObject(iContextMenu);
                        iContextMenu = null;
                    }

                    if (iContextMenuPtr != IntPtr.Zero)
                    {
                        Marshal.ReleaseComObject(iContextMenuPtr);
                        iContextMenuPtr = IntPtr.Zero;
                    }

                    return(false);
                }
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu    = null;
                return(false);
            }
        }
Пример #2
0
        public static bool GetNewContextMenu(IntPtr pidl, out IntPtr iContextMenuPtr, out IContextMenu iContextMenu)
        {
            //アンマネージなIContextMenuを作成する
            if (Ole32Wrapper.CoCreateInstance(
                    ref GUIDs.CLSID_NewMenu,
                    IntPtr.Zero,
                    CLSCTX.INPROC_SERVER,
                    ref GUIDs.IID_IContextMenu,
                    out iContextMenuPtr) == Shell32Wrapper.S_OK)
            {
                //アンマネージからマネージなオブジェクトを取得
                iContextMenu = Marshal.GetTypedObjectForIUnknown(iContextMenuPtr, typeof(IContextMenu)) as IContextMenu;

                //アンマネージなIContextMenuからアンマネージなIShellExtInitを取得する
                IntPtr iShellExtInitPtr;
                if (Marshal.QueryInterface(
                        iContextMenuPtr,
                        ref GUIDs.IID_IShellExtInit,
                        out iShellExtInitPtr) == Shell32Wrapper.S_OK)
                {
                    //アンマネージからマネージなオブジェクトを取得
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    //とりあえず初期化する
                    //マナーのようです
                    int hresult = iShellExtInit.Initialize(pidl, 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);
            }
        }
        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);
            }
        }
Пример #4
0
        private bool GetNewContextMenu(out IntPtr iContextMenuPtr, out IContextMenu iContextMenu)
        {
            if (ShellFolders.CoCreateInstance(
                    ref ShellFolders.CLSID_NewMenu,
                    IntPtr.Zero,
                    ShellFolders.CLSCTX.INPROC_SERVER,
                    ref ShellFolders.IID_IContextMenu,
                    out iContextMenuPtr) == ShellFolders.S_OK)
            {
                iContextMenu = Marshal.GetTypedObjectForIUnknown(iContextMenuPtr, typeof(IContextMenu)) as IContextMenu;

                IntPtr iShellExtInitPtr;
                if (Marshal.QueryInterface(
                        iContextMenuPtr,
                        ref ShellFolders.IID_IShellExtInit,
                        out iShellExtInitPtr) == ShellFolders.S_OK)
                {
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(
                        iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    iShellExtInit.Initialize(folderPidl, 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);
            }
        }
Пример #5
0
        public bool GetOpenWithContextMenu(IListItemEx[] itemArray, out IntPtr iContextMenuPtr, out IContextMenu iContextMenu)
        {
            Guid CLSID_OpenWith = new Guid(0x09799AFB, 0xAD67, 0x11d1, 0xAB, 0xCD, 0x00, 0xC0, 0x4F, 0xC3, 0x09, 0x36);
            Guid iicm           = typeof(IContextMenu).GUID;
            Guid iise           = typeof(IShellExtInit).GUID;

            if (Ole32.CoCreateInstance(
                    ref CLSID_OpenWith,
                    IntPtr.Zero,
                    Ole32.CLSCTX.INPROC_SERVER,
                    ref iicm,
                    out iContextMenuPtr) == (int)HResult.S_OK)
            {
                iContextMenu = Marshal.GetObjectForIUnknown(iContextMenuPtr) as IContextMenu;

                IntPtr iShellExtInitPtr;
                if (Marshal.QueryInterface(
                        iContextMenuPtr,
                        ref iise,
                        out iShellExtInitPtr) == (int)HResult.S_OK)
                {
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    try {
                        IntPtr doPtr;
                        iShellExtInit.Initialize(IntPtr.Zero, itemArray.GetIDataObject(out doPtr), 0);

                        Marshal.ReleaseComObject(iShellExtInit);
                        Marshal.Release(iShellExtInitPtr);
                        return(true);
                    } catch { }
                }
                else
                {
                    if (iContextMenu != null)
                    {
                        Marshal.ReleaseComObject(iContextMenu);
                        iContextMenu = null;
                    }

                    return(false);
                }
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu    = null;
                return(false);
            }
            return(false);
        }
        private bool GetNewContextMenu(ShellFolder folder)
        {
            if (Interop.CoCreateInstance(
                    ref Interop.CLSID_NewMenu,
                    IntPtr.Zero,
                    CLSCTX.INPROC_SERVER,
                    ref Interop.IID_IContextMenu,
                    out iContextMenuPtr) == NativeMethods.S_OK)
            {
                iContextMenu = Marshal.GetTypedObjectForIUnknown(iContextMenuPtr, typeof(IContextMenu)) as IContextMenu;

                if (Marshal.QueryInterface(
                        iContextMenuPtr,
                        ref Interop.IID_IShellExtInit,
                        out var iShellExtInitPtr) == NativeMethods.S_OK)
                {
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(
                        iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    iShellExtInit?.Initialize(folder.AbsolutePidl, IntPtr.Zero, 0);

                    if (iShellExtInit != null)
                    {
                        Marshal.ReleaseComObject(iShellExtInit);
                    }

                    if (iShellExtInitPtr != IntPtr.Zero)
                    {
                        Marshal.Release(iShellExtInitPtr);
                    }

                    return(true);
                }
            }

            return(false);
        }
        public bool GetOpenWithContextMenu(ShellObject item, out object iContextMenuPtr, out IContextMenu iContextMenu)
        {
            Guid CLSID_OpenWith = new Guid(0x09799AFB, 0xAD67, 0x11d1, 0xAB, 0xCD, 0x00, 0xC0, 0x4F, 0xC3, 0x09, 0x36);
            Guid iicm           = typeof(IContextMenu).GUID;
            Guid iise           = typeof(IShellExtInit).GUID;

            if (WindowsAPI.CoCreateInstance(
                    ref CLSID_OpenWith,
                    IntPtr.Zero,
                    0x1,
                    ref iicm,
                    out iContextMenuPtr) == (int)HResult.Ok)
            {
                iContextMenu = iContextMenuPtr as IContextMenu;

                IntPtr iShellExtInitPtr;
                if (Marshal.QueryInterface(
                        Marshal.GetIUnknownForObject(iContextMenuPtr),
                        ref iise,
                        out iShellExtInitPtr) == (int)HResult.Ok)
                {
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(
                        iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    try
                    {
                        iShellExtInit.Initialize(item.PIDL, IntPtr.Zero, 0);

                        Marshal.ReleaseComObject(iShellExtInit);
                        Marshal.Release(iShellExtInitPtr);

                        return(true);
                    }
                    finally
                    {
                    }
                }
                else
                {
                    if (iContextMenu != null)
                    {
                        Marshal.ReleaseComObject(iContextMenu);
                        iContextMenu = null;
                    }

                    if (iContextMenuPtr != null)
                    {
                        Marshal.ReleaseComObject(iContextMenuPtr);
                        iContextMenuPtr = null;
                    }

                    return(false);
                }
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu    = null;
                return(false);
            }
        }
        public bool GetNewContextMenu(ShellObject item, out object iContextMenuPtr, out IContextMenu iContextMenu)
        {
            Guid CLSID_NewMenu = new Guid("{D969A300-E7FF-11d0-A93B-00A0C90F2719}");
            Guid iicm          = typeof(IContextMenu).GUID;
            Guid iise          = typeof(IShellExtInit).GUID;

            if (WindowsAPI.CoCreateInstance(
                    ref CLSID_NewMenu,
                    IntPtr.Zero,
                    0x1,
                    ref iicm,
                    out iContextMenuPtr) == (int)HResult.Ok)
            {
                iContextMenu = iContextMenuPtr as IContextMenu;

                IntPtr iShellExtInitPtr;
                if (Marshal.QueryInterface(
                        Marshal.GetIUnknownForObject(iContextMenuPtr),
                        ref iise,
                        out iShellExtInitPtr) == (int)HResult.Ok)
                {
                    IShellExtInit iShellExtInit = Marshal.GetTypedObjectForIUnknown(
                        iShellExtInitPtr, typeof(IShellExtInit)) as IShellExtInit;

                    try
                    {
                        iShellExtInit.Initialize(item.PIDL, IntPtr.Zero, 0);

                        Marshal.ReleaseComObject(iShellExtInit);
                        Marshal.Release(iShellExtInitPtr);

                        return(true);
                    }
                    finally
                    {
                    }
                }
                else
                {
                    if (iContextMenu != null)
                    {
                        Marshal.ReleaseComObject(iContextMenu);
                        iContextMenu = null;
                    }

                    if (iContextMenuPtr != null)
                    {
                        Marshal.ReleaseComObject(iContextMenuPtr);
                        iContextMenuPtr = null;
                    }

                    return(false);
                }
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu    = null;
                return(false);
            }
        }
Пример #9
0
        /// <summary>
        /// If this method returns true then the caller must call ReleaseNewMenu
        /// </summary>
        /// <param name="itm"></param>
        /// <param name="contextMenu"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool SetUpNewMenu(ShellItem itm, IntPtr contextMenu, int index)
        {
            int     HR;
            HResult idCount;

            newMenuPtr = IntPtr.Zero;
            HR         = Ole32.CoCreateInstance(Shell32.CLSID_NewMenu, IntPtr.Zero, (uint)CLSCTX.INPROC_SERVER, Shell32.IID_IContextMenu, out newMenuPtr);
            if (HR == (int)HResult.S_OK)
            {
                newMenu = (IContextMenu)Marshal.GetObjectForIUnknown(newMenuPtr);

                IntPtr p = IntPtr.Zero;
                Marshal.QueryInterface(newMenuPtr, ref Shell32.IID_IContextMenu2, out p);
                if (p != IntPtr.Zero)
                {
                    newMenu2 = (IContextMenu2)Marshal.GetObjectForIUnknown(p);
                }


                Marshal.QueryInterface(newMenuPtr, ref Shell32.IID_IContextMenu3, out p);
                if (p != IntPtr.Zero)
                {
                    newMenu3 = (IContextMenu3)Marshal.GetObjectForIUnknown(p);
                }


                if (!p.Equals(IntPtr.Zero))
                {
                    Marshal.Release(p);
                    p = IntPtr.Zero;
                }

                IntPtr iShellExtInitPtr = IntPtr.Zero;
                HR = (Marshal.QueryInterface(newMenuPtr, ref Shell32.IID_IShellExtInit, out iShellExtInitPtr));
                if (HR == (int)HResult.S_OK)
                {
                    IShellExtInit shellExtInit = (IShellExtInit)Marshal.GetObjectForIUnknown(iShellExtInitPtr);
                    shellExtInit.Initialize(itm.Pidl, IntPtr.Zero, 0);

                    Marshal.ReleaseComObject(shellExtInit);
                    Marshal.Release(iShellExtInitPtr);
                }
                if (!newMenuPtr.Equals(IntPtr.Zero))
                {
                    Marshal.Release(newMenuPtr);
                    newMenuPtr = IntPtr.Zero;
                }
            }

            if (HR != (int)HResult.S_OK)
            {
                ReleaseNewMenu();
#if DEBUG
                Marshal.ThrowExceptionForHR(HR);
#endif
                return(false);
            }

            idCount    = newMenu.QueryContextMenu(contextMenu, (uint)index, min, max, (int)CMF.NORMAL);
            newMenuPtr = User32.GetSubMenu(contextMenu, index);

            return(true);
        }