public ShellNode(ShellBrowser browser, IntPtr pidl, IntPtr shellFolderPtr)
        {
            this.Browser = browser;

            this.shellFolderPtr = shellFolderPtr;
            this.shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder));
            this.SubFiles = new ShellItemCollection(this);
            this.SubFolders = new ShellItemCollection(this);

            PIDLRel = new Pidl(pidl, false);

            Text = "Desktop";
            Path = "Desktop";

            SetAttributesDesktop(this);

            ShFileInfo info = new ShFileInfo();
            Shell32.SHGetFileInfo(PIDLRel.Ptr, 0, ref info, Marshal.SizeOf(info), SHGFI.Pidl | SHGFI.TypeName | SHGFI.SysIconIndex);

            Type = info.TypeName;

            ShellImageList.SetIconIndex(this, info.IconIndex, false);
            ShellImageList.SetIconIndex(this, info.IconIndex, true);

            SortFlag = 1;
        }
 public static ExtractIconWrapper FromShellFolder(IShellFolder folder, IntPtr pidl)
 {
     if (folder == null)
     {
         throw new ArgumentNullException("folder");
     }
     if (pidl == IntPtr.Zero)
     {
         throw new ArgumentException("pidl == IntPtr.Zero", "pidl");
     }
     try
     {
         object obj2;
         IntPtr[] apidl = new IntPtr[] { pidl };
         folder.GetUIObjectOf(IntPtr.Zero, 1, apidl, typeof(IExtractIconW).GUID, IntPtr.Zero, out obj2);
         if (obj2 != null)
         {
             return new ExtractIconWrapper((IExtractIconW) obj2);
         }
     }
     catch (FileNotFoundException)
     {
     }
     return null;
 }
        internal ShellItem(ShellBrowser browser, IntPtr pidl, IntPtr shellFolderPtr)
        {
            this.browser = browser;

            this.shellFolderPtr = shellFolderPtr;
            this.shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder));
            subFiles = new ShellItemCollection(this);
            subFolders = new ShellItemCollection(this);

            pidlRel = new PIDL(pidl, false);

            text = "Desktop";
            path = "Desktop";

            SetAttributesDesktop(this);

            ShellAPI.SHFILEINFO info = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGetFileInfo(pidlRel.Ptr, 0, ref info, ShellAPI.cbFileInfo,
                                   ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.TYPENAME | ShellAPI.SHGFI.SYSICONINDEX);

            type = info.szTypeName;

            ShellImageList.SetIconIndex(this, info.iIcon, false);
            ShellImageList.SetIconIndex(this, info.iIcon, true);

            sortFlag = 1;
        }
Exemple #4
0
        private static void GetChilds(IShellFolder desktop)
        {
            Console.WriteLine("===============GetChilds================");

            //循环查找子项
            Microsoft.WindowsAPICodePack.Shell.IEnumIDList Enum = null;
            IEnumIDList EnumPtr;
            IntPtr pidlSub;
            uint celtFetched;

            if (desktop.EnumObjects(IntPtr.Zero, ShellNativeMethods.ShellFolderEnumerationOptions.Folders | ShellNativeMethods.ShellFolderEnumerationOptions.IncludeHidden | ShellNativeMethods.ShellFolderEnumerationOptions.Storage, out EnumPtr) == CONST.S_OK)
            {
                while (EnumPtr.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == CONST.S_FALSE)
                {
                    IntPtr strr;
                    desktop.GetDisplayNameOf(ref pidlSub, (uint)SHGNO.INFOLDER, out strr);
                    string name = Marshal.PtrToStringAuto(strr);

                    //SHFILEINFO info = new SHFILEINFO();
                    //Shell32.SHGetFileInfo(pidlSub, 0, ref info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.PIDL | SHGFI.DISPLAYNAME | SHGFI.ICONLOCATION);

                    //IShellFolder iSub;
                    //desktop.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub);

                    Console.WriteLine(name);
                }
            }
            Console.WriteLine();
            Console.WriteLine();
        }
        internal override IShellView CreateShellView(IShellFolder folder)
        {
            //  Setup create info for a new default folder view.
            var createInfo = new SFV_CREATE
            {
                cbSize = (uint) Marshal.SizeOf(typeof (SFV_CREATE)),
                pshf = folder,
                psvOuter = null,
                psfvcb = null
            };

            //  TODO: IMPORTANT: This is the function that's failing now for creating the 
            //  view, it seems that it could be due to not providing psvOuter (which may be
            //  required as we're not far off being a common dialog) or more likely because we
            //  are not providing the callback. Try both...
            //  NOTE: A quick test shows it's unlikely to be the psvOuter, try the CB.
            //  NOTE: adding the callback hasn't helped, we can try the alternative call
            //  which is shcreateshellfolderviewex
            //  NOTE: None of those suggestions worked.
            IShellView view;
            if (Shell32.SHCreateShellFolderView(ref createInfo, out view) != WinError.S_OK)
            {
                throw new Exception("An error occured creating the default folder view.");
            }

            return view;
        }
 internal ShellFolder(IShellItem shellItem, IShellFolder shellFolder, ShellItemKnownInfo knownInfo)
     : base(GetShellItemFromFolder(shellItem, shellFolder), knownInfo)
 {
     if (shellFolder == null)
     {
         shellFolder = (IShellFolder)shellItem.BindToHandler(null, ShellConsts.BHID_SFObject, typeof(IShellFolder).GUID);
     }
     NativeFolder = shellFolder;
 }
 private static IShellItem GetShellItemFromFolder(IShellItem shellItem, IShellFolder shellFolder)
 {
     if (shellItem == null && shellFolder == null)
         throw new ArgumentNullException("shellItem, shellFolder", "shellItem and shellFolder cannot both be null");
     if (shellItem != null)
         return shellItem;
     IdList pidl = SHGetIDListFromObject(shellFolder);
     return SHCreateItemFromIDList<IShellItem>(pidl);
 }
 /// <summary>
 /// 获取显示名称
 /// </summary>
 public static string GetNameByIShell(IShellFolder Root, IntPtr pidlSub)
 {
     IntPtr strr = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);
     Marshal.WriteInt32(strr, 0, 0);
     StringBuilder buf = new StringBuilder(MAX_PATH);
     Root.GetDisplayNameOf(pidlSub, SHGNO.INFOLDER, strr);
     Shlwapi.StrRetToBuf(strr, pidlSub, buf, MAX_PATH);
     return buf.ToString();
 }
        internal ShellItem(ShellBrowser browser, ShellAPI.CSIDL specialFolder)
        {
            IntPtr pidl = IntPtr.Zero;
            ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, specialFolder, out pidl);
            //
            IntPtr shellFolderPtr;
            this.browser = browser;
            //
            if (specialFolder == ShellAPI.CSIDL.DESKTOP)
            {
                ShellAPI.SHGetDesktopFolder(out shellFolderPtr);
                //
                this.shellFolderPtr = shellFolderPtr;
                this.shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder));
            }
            else
            {
                this.shellFolder = GetShellFolderFromPidl(pidl, out shellFolderPtr);
                //
                this.parentItem = browser.DesktopItem;
            }
            //
            subFiles = new ShellItemCollection(this);
            subFolders = new ShellItemCollection(this);

            pidlRel = new PIDL(pidl, false);

            ShellAPI.SHFILEINFO info = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGetFileInfo(pidlRel.Ptr, 0, ref info, ShellAPI.cbFileInfo,
                ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.TYPENAME | ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.DISPLAYNAME);

            type = info.szTypeName;
            text = info.szDisplayName;
            //
            StringBuilder pathBuilder = new StringBuilder(ShellAPI.MAX_PATH);
            ShellAPI.SHGetFolderPath(
                    IntPtr.Zero, specialFolder,
                    IntPtr.Zero, ShellAPI.SHGFP.TYPE_CURRENT, pathBuilder);
            this.path = pathBuilder.ToString();
            //
            if (specialFolder == ShellAPI.CSIDL.DESKTOP)
            {
                ShellImageList.SetIconIndex(this, info.iIcon, false);
                ShellImageList.SetIconIndex(this, info.iIcon, true);
                //
                SetAttributesDesktop(this);
            }
            else
            {
                SetAttributesFolder(this);
                SetInfo(this);
            }
            sortFlag = 1;
        }
 public ShellItem(IShellFolder folder, IntPtr relativePidl)
 {
     if (folder == null)
     {
         throw new ArgumentNullException("folfer");
     }
     if (relativePidl == IntPtr.Zero)
     {
         throw new ArgumentException("relativePidl == IntPtr.Zero", "relativePidl");
     }
     this.FAbsolutePidl = GetAbsolutePidl(folder, relativePidl);
 }
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (NativeFolder != null)
         {
             Marshal.ReleaseComObject(NativeFolder);
             NativeFolder = null;
         }
     }
     base.Dispose(disposing);
 }
        private static bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr PIDLs, out IntPtr ctxMenuPtr)
        {
            var arrPidLs = new IntPtr[1];
            arrPidLs[0] = PIDLs;
            var nResult = oParentFolder.GetUIObjectOf(IntPtr.Zero, (uint) arrPidLs.Length, arrPidLs, ref _iidIContextMenu, IntPtr.Zero, out ctxMenuPtr);
            if (SOk == nResult)
            {
                _oContextMenu = (IContextMenu) Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof (IContextMenu));

                return true;
            }
            ctxMenuPtr = IntPtr.Zero;
            _oContextMenu = null;
            return false;
        }
        internal ShellItem(ShellBrowser browser, ShellItem parentItem, IntPtr pidl, IntPtr shellFolderPtr)
        {
            this.browser = browser;

            this.parentItem = parentItem;
            this.shellFolderPtr = shellFolderPtr;
            this.shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder));
            subFiles = new ShellItemCollection(this);
            subFolders = new ShellItemCollection(this);

            pidlRel = new PIDL(pidl, false);

            SetText(this);
            SetPath(this);
            SetAttributesFolder(this);
            SetInfo(this);

            sortFlag = MakeSortFlag(this);
        }
        public ShellNode(ShellBrowser browser, ShellNode parentItem, IntPtr pidl, IntPtr shellFolderPtr)
        {
            this.Browser = browser;

            this.ParentItem = parentItem;
            this.shellFolderPtr = shellFolderPtr;
            this.shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder));
            SubFiles = new ShellItemCollection(this);
            SubFolders = new ShellItemCollection(this);

            PIDLRel = new Pidl(pidl, false);

            SetText(this);
            SetPath(this);
            SetAttributesFolder(this);
            SetInfo(this);

            SortFlag = MakeSortFlag(this);
        }
        /// <summary>Gets the interfaces to the context menu</summary>
        /// <param name="oParentFolder">Parent folder</param>
        /// <param name="arrPIDLs">PIDLs</param>
        /// <returns>true if it got the interfaces, otherwise false</returns>
        private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs, out IntPtr ctxMenuPtr)
        {
            int nResult = oParentFolder.GetUIObjectOf(
                IntPtr.Zero,
                (uint)arrPIDLs.Length,
                arrPIDLs,
                ref IID_IContextMenu,
                IntPtr.Zero,
                out ctxMenuPtr);

            if (S_OK == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(ctxMenuPtr, typeof(IContextMenu));

                return true;
            }
            else
            {
                ctxMenuPtr = IntPtr.Zero;
                _oContextMenu = null;
                return false;
            }
        }
 public ShellItem(IShellFolder folder, IntPtr hwndOwner, string relativeName)
 {
     if (folder == null)
     {
         throw new ArgumentNullException("folder");
     }
     if (relativeName == null)
     {
         throw new ArgumentNullException("relativeName");
     }
     if (relativeName == string.Empty)
     {
         throw new ArgumentException("relativeName is empty");
     }
     IntPtr relativePidl = folder.ParseDisplayName(hwndOwner, relativeName);
     try
     {
         this.FAbsolutePidl = GetAbsolutePidl(folder, relativePidl);
     }
     finally
     {
         Marshal.FreeCoTaskMem(relativePidl);
     }
 }
        public void Dispose()
        {
            _disposed = true;
            if (_iShellFolder != null)
            {
                Marshal.ReleaseComObject(_iShellFolder);
                _iShellFolder = null;
            }

            if (_ptrShellFolder != IntPtr.Zero)
            {
                try
                {
                    Marshal.Release(_ptrShellFolder);
                }
                catch (Exception) { }
                finally
                {
                    _ptrShellFolder = IntPtr.Zero;
                }
            }

            GC.SuppressFinalize(this);
        }
 internal static extern void SHBindToParent(
     [In][MarshalAs(UnmanagedType.Struct)] PIDL pidl,
     [In][MarshalAs(UnmanagedType.LPStruct)] Guid riid,
     [MarshalAs(UnmanagedType.Interface)] out IShellFolder pParentShellFolder,
     IntPtr ppidlLast = default(IntPtr));
 public static string GetDisplayName(IShellFolder shellFolder, IntPtr pIDLLast, bool fDisplayName) {
     STRRET strret;
     uint uFlags = fDisplayName ? 0 : 0x8000u;
     StringBuilder pszBuf = new StringBuilder(260);
     if(shellFolder.GetDisplayNameOf(pIDLLast, uFlags, out strret) == 0) {
         PInvoke.StrRetToBuf(ref strret, pIDLLast, pszBuf, pszBuf.Capacity);
     }
     return pszBuf.ToString();
 }
        /// <summary>
        /// When the mouse goes up on an node and suspendContextMenu is true, this method will show the
        /// ContextMenu for that node and after the user selects an item, it will execute that command.
        /// </summary>
        void FolderView_MouseUp(object sender, MouseEventArgs e)
        {
            if (suspendContextMenu || contextMenuVisible)
            {
                suspendContextMenu = false;
                return;
            }

            TreeViewHitTestInfo hitTest = br.FolderView.HitTest(e.Location);

            contextMenuVisible = true;
            if (e.Button == MouseButtons.Right &&
                (hitTest.Location == TreeViewHitTestLocations.Image ||
                 hitTest.Location == TreeViewHitTestLocations.Label ||
                 hitTest.Location == TreeViewHitTestLocations.StateImage))
            {
                #region Fields
                ShellItem item = (ShellItem)hitTest.Node.Tag;

                IntPtr contextMenu             = IntPtr.Zero,
                       iContextMenuPtr         = IntPtr.Zero,
                       iContextMenuPtr2        = IntPtr.Zero,
                       iContextMenuPtr3        = IntPtr.Zero;
                IShellFolder parentShellFolder =
                    (item.ParentItem != null) ? item.ParentItem.ShellFolder : item.ShellFolder;

                #endregion

                #region Show / Invoke
                try
                {
                    if (ContextMenuHelper.GetIContextMenu(parentShellFolder, new IntPtr[] { item.PIDLRel.Ptr },
                                                          out iContextMenuPtr, out iContextMenu))
                    {
                        contextMenu = ShellAPI.CreatePopupMenu();

                        iContextMenu.QueryContextMenu(
                            contextMenu,
                            0,
                            ShellAPI.CMD_FIRST,
                            ShellAPI.CMD_LAST,
                            ShellAPI.CMF.EXPLORE |
                            ShellAPI.CMF.CANRENAME |
                            ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellAPI.CMF.EXTENDEDVERBS : 0));

                        string       topInvoke = hitTest.Node.IsExpanded ? "Collapse" : "Expand";
                        ShellAPI.MFT extraFlag = (hitTest.Node.Nodes.Count > 0) ? 0 : ShellAPI.MFT.GRAYED;
                        ShellAPI.InsertMenu(contextMenu, 0,
                                            ShellAPI.MFT.BYPOSITION | extraFlag,
                                            (int)CMD_CUSTOM.ExpandCollapse, topInvoke);
                        ShellAPI.InsertMenu(contextMenu, 1,
                                            ShellAPI.MFT.BYPOSITION | ShellAPI.MFT.SEPARATOR,
                                            0, "-");

                        ShellAPI.SetMenuDefaultItem(
                            contextMenu,
                            0,
                            true);

                        Marshal.QueryInterface(iContextMenuPtr, ref ShellAPI.IID_IContextMenu2, out iContextMenuPtr2);
                        Marshal.QueryInterface(iContextMenuPtr, ref ShellAPI.IID_IContextMenu3, out iContextMenuPtr3);

                        try
                        {
                            iContextMenu2 =
                                (IContextMenu2)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr2, typeof(IContextMenu2));

                            iContextMenu3 =
                                (IContextMenu3)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr3, typeof(IContextMenu3));
                        }
                        catch (Exception) { }

                        Point ptInvoke = br.FolderView.PointToScreen(e.Location);
                        uint  selected = ShellAPI.TrackPopupMenuEx(
                            contextMenu,
                            ShellAPI.TPM.RETURNCMD,
                            ptInvoke.X,
                            ptInvoke.Y,
                            this.Handle,
                            IntPtr.Zero);

                        br.OnContextMenuMouseHover(new ContextMenuMouseHoverEventArgs(string.Empty));

                        if (selected == (int)CMD_CUSTOM.ExpandCollapse)
                        {
                            if (hitTest.Node.IsExpanded)
                            {
                                hitTest.Node.Collapse(true);
                            }
                            else
                            {
                                hitTest.Node.Expand();
                            }
                        }
                        else if (selected >= ShellAPI.CMD_FIRST)
                        {
                            string command = ContextMenuHelper.GetCommandString(
                                iContextMenu,
                                selected - ShellAPI.CMD_FIRST,
                                true);

                            if (command == "rename")
                            {
                                br.FolderView.LabelEdit = true;
                                hitTest.Node.BeginEdit();
                            }
                            else
                            {
                                ContextMenuHelper.InvokeCommand(
                                    iContextMenu,
                                    selected - ShellAPI.CMD_FIRST,
                                    (item.ParentItem != null) ?
                                    ShellItem.GetRealPath(item.ParentItem) : ShellItem.GetRealPath(item),
                                    ptInvoke);
                            }
                        }
                    }
                }
                #endregion
                catch (Exception) { }
                #region Finally
                finally
                {
                    if (iContextMenu != null)
                    {
                        Marshal.ReleaseComObject(iContextMenu);
                        iContextMenu = null;
                    }

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

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

                    if (contextMenu != null)
                    {
                        ShellAPI.DestroyMenu(contextMenu);
                    }

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

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

                    if (iContextMenuPtr3 != IntPtr.Zero)
                    {
                        Marshal.Release(iContextMenuPtr3);
                    }
                }
                #endregion
            }
            contextMenuVisible = false;
        }
 internal static extern HRESULT SHCreateItemWithParent(
     IntPtr pidParent,
     [In][MarshalAs(UnmanagedType.Interface)] IShellFolder psfParent,
     [In] IntPtr pidl,
     [In][MarshalAs(UnmanagedType.LPStruct)] Guid riid,
     [Out][MarshalAs(UnmanagedType.Interface, IidParameterIndex = 3)] out IShellItem ppvItem);
        public void ShowContextMenu()
        {
            IntPtr contextMenu      = IntPtr.Zero,
                   iContextMenuPtr  = IntPtr.Zero,
                   iContextMenuPtr2 = IntPtr.Zero,
                   iContextMenuPtr3 = IntPtr.Zero;

            try
            {
                if (ShellFolders.GetIContextMenu(parentShellFolder, pidls, out iContextMenuPtr, out iContextMenu))
                {
                    // get some properties about our file(s)
                    bool allFolders  = true;
                    bool allInStacks = true;
                    foreach (SystemFile file in paths)
                    {
                        if (!file.IsDirectory)
                        {
                            allFolders = false;
                        }
                        else
                        {
                            bool contains = false;
                            foreach (SystemDirectory dir in StacksManager.Instance.StackLocations)
                            {
                                if (dir.Equals(file.FullName))
                                {
                                    contains = true;
                                    break;
                                }
                            }

                            if (!contains)
                            {
                                allInStacks = false;
                            }
                        }
                    }

                    contextMenu = ShellFolders.CreatePopupMenu();

                    uint numPrepended = prependItems(contextMenu, allFolders, allInStacks);

                    iContextMenu.QueryContextMenu(
                        contextMenu,
                        numPrepended,
                        ShellFolders.CMD_FIRST,
                        ShellFolders.CMD_LAST,
                        ShellFolders.CMF.EXPLORE |
                        ShellFolders.CMF.CANRENAME |
                        ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellFolders.CMF.EXTENDEDVERBS : 0));

                    appendItems(contextMenu, allFolders, allInStacks);

                    Marshal.QueryInterface(iContextMenuPtr, ref ShellFolders.IID_IContextMenu2, out iContextMenuPtr2);
                    Marshal.QueryInterface(iContextMenuPtr, ref ShellFolders.IID_IContextMenu3, out iContextMenuPtr3);

                    try
                    {
                        iContextMenu2 =
                            (IContextMenu2)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr2, typeof(IContextMenu2));

                        iContextMenu3 =
                            (IContextMenu3)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr3, typeof(IContextMenu3));
                    }
                    catch (Exception) { }

                    uint selected = ShellFolders.TrackPopupMenuEx(
                        contextMenu,
                        ShellFolders.TPM.RETURNCMD,
                        this.x,
                        this.y,
                        this.Handle,
                        IntPtr.Zero);


                    if (selected >= ShellFolders.CMD_FIRST)
                    {
                        string command = ShellFolders.GetCommandString(iContextMenu, selected - ShellFolders.CMD_FIRST, true);

                        // set action strings for us to use in our custom execution function
                        switch ((CairoContextMenuItem)selected)
                        {
                        case CairoContextMenuItem.AddToStacks:
                            command = "addStack";
                            break;

                        case CairoContextMenuItem.RemoveFromStacks:
                            command = "removeStack";
                            break;

                        case CairoContextMenuItem.OpenInNewWindow:
                            command = "openWithShell";
                            break;

                        default:
                            if (command == "open" && allFolders)
                            {
                                // suppress running system code
                                command = "openFolder";
                            }
                            else
                            {
                                ShellFolders.InvokeCommand(
                                    iContextMenu,
                                    selected - ShellFolders.CMD_FIRST,
                                    parent,
                                    new Point(this.x, this.y));
                            }
                            break;
                        }

                        if (this.itemSelected != null)
                        {
                            itemSelected(command, paths[0].FullName, sender);
                        }
                    }
                }
                else
                {
                    CairoLogger.Instance.Debug("Error retrieving IContextMenu");
                }
            }
            catch (Exception) { }
            finally
            {
                if (iContextMenu != null)
                {
                    Marshal.FinalReleaseComObject(iContextMenu);
                    iContextMenu = null;
                }

                if (iContextMenu2 != null)
                {
                    Marshal.FinalReleaseComObject(iContextMenu2);
                    iContextMenu2 = null;
                }

                if (iContextMenu3 != null)
                {
                    Marshal.FinalReleaseComObject(iContextMenu3);
                    iContextMenu3 = null;
                }

                if (parentShellFolder != null)
                {
                    Marshal.FinalReleaseComObject(parentShellFolder);
                    parentShellFolder = null;
                }

                if (contextMenu != null)
                {
                    ShellFolders.DestroyMenu(contextMenu);
                }

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

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

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

                for (int i = 0; i < pidls.Length; i++)
                {
                    Marshal.FreeCoTaskMem(pidls[i]);
                    pidls[i] = IntPtr.Zero;
                }
            }
        }
Exemple #23
0
 static extern int SHGetDesktopFolder_([MarshalAs(UnmanagedType.Interface)] out IShellFolder ppshf);
Exemple #24
0
        public Bitmap LoadImageFromExtractIcon(string filename)
        {
            if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename))
            {
                return(null);
            }

            Bitmap bm1     = null;
            IntPtr hbitmap = IntPtr.Zero;

            IShellFolder desktop     = null;
            IntPtr       pidlParent  = IntPtr.Zero;
            IShellFolder shellFolder = null;
            IUnknown     iunk        = null;
            IExtractIcon extractIcon = null;

            try
            {
                Debug.Write("Trying IExtractIcon\r\n");

                Shell32.SHGetDesktopFolder(ref desktop);

                int    cParsed   = 0;
                int    pdwAttrib = 0;
                string filePath  = filename;
                if (System.IO.File.Exists(filename))
                {
                    filePath = System.IO.Path.GetDirectoryName(filename);
                }
                desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib);

                Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
                desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder);

                Guid uuidExtractIcon = new Guid("000214eb-0000-0000-c000-000000000046");
                iunk = GetUIObject(shellFolder, filename, uuidExtractIcon);

                if (iunk == null)
                {
                    Debug.Write("    IExtractIcon not supported\r\n");
                    return(null);
                }

                int hres = 0;
                extractIcon = (IExtractIcon)iunk;
                StringBuilder location  = new StringBuilder(260, 260);
                int           iconIndex = 0;
                GILFLAGS2     flags     = GILFLAGS2.GIL_None;
                hres = extractIcon.GetIconLocation(0, location, location.Capacity, out iconIndex, out flags);
                IntPtr iconLarge = IntPtr.Zero;
                IntPtr iconSmall = IntPtr.Zero;
                uint   iconSize  = (16 | (48 << 16));
                hres = extractIcon.Extract(location, iconIndex, out iconLarge, out iconSmall, iconSize);
                Icon ico = Icon.FromHandle(iconLarge);
                bm1 = ico.ToBitmap();
            }
            catch (Exception e)
            {
                Debug.Write("    Exception extracting image from IExtractIcon: " + e.Message + "\r\n");
            }
            finally
            {
                if (extractIcon != null)
                {
                    Marshal.ReleaseComObject(extractIcon);
                    extractIcon = null;
                }
                if (iunk != null)
                {
                    Marshal.ReleaseComObject(iunk);
                    iunk = null;
                }
                if (shellFolder != null)
                {
                    Marshal.ReleaseComObject(shellFolder);
                    shellFolder = null;
                }
                if (pidlParent != IntPtr.Zero)
                {
                    Allocator.Free(pidlParent);
                    pidlParent = IntPtr.Zero;
                }
                if (desktop != null)
                {
                    Marshal.ReleaseComObject(desktop);
                    desktop = null;
                }
                if (_allocator != null)
                {
                    Marshal.ReleaseComObject(_allocator);
                    _allocator = null;
                }
            }
            return(bm1);
        }
Exemple #25
0
        public IExtractIconPWFlags GetIconType()
        {
            if (this.Extension == ".exe" || this.Extension == ".com" || this.Extension == ".bat" || this.Extension == ".msi")
            {
                return(IExtractIconPWFlags.GIL_PERINSTANCE);
            }

            if (this.Parent == null)
            {
                return(0);
            }

            if (this.IsFolder)
            {
                IExtractIcon  iextract     = null;
                IShellFolder  ishellfolder = null;
                StringBuilder str          = null;
                IntPtr        result;

                try {
                    var  guid = new Guid("000214fa-0000-0000-c000-000000000046");
                    uint res  = 0;
                    ishellfolder = this.Parent.GetIShellFolder();
                    var pidls = new IntPtr[1] {
                        Shell32.ILFindLastID(this.Pidl)
                    };
                    //pidls[0] = Shell32.ILFindLastID(this.Pidl);
                    ishellfolder.GetUIObjectOf(
                        IntPtr.Zero,
                        1,
                        pidls,
                        ref guid,
                        res,
                        out result
                        );

                    if (result == IntPtr.Zero)
                    {
                        pidls = null;
                        //Marshal.ReleaseComObject(ishellfolder);
                        return(IExtractIconPWFlags.GIL_PERCLASS);
                    }
                    iextract = (IExtractIcon)Marshal.GetTypedObjectForIUnknown(result, typeof(IExtractIcon));
                    str      = new StringBuilder(512);
                    int index = -1;
                    IExtractIconPWFlags flags;
                    iextract.GetIconLocation(IExtractIconUFlags.GIL_ASYNC, str, 512, out index, out flags);
                    pidls = null;
                    //Marshal.ReleaseComObject(ishellfolder);
                    //Marshal.ReleaseComObject(iextract);
                    ishellfolder = null;
                    iextract     = null;
                    str          = null;
                    return(flags);
                } catch (Exception) {
                    //if (ishellfolder != null)
                    //Marshal.ReleaseComObject(ishellfolder);
                    //if (iextract != null)
                    //	Marshal.ReleaseComObject(iextract);
                    return(0);
                }
            }
            else
            {
                return(IExtractIconPWFlags.GIL_PERCLASS);
            }
        }
Exemple #26
0
        public Bitmap LoadThumbnailFromImageFactory(string filename, Size requestedSize, bool onlyThumbnail)
        {
            if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename))
            {
                return(null);
            }

            Bitmap bm1     = null;
            IntPtr hbitmap = IntPtr.Zero;
            SIZE   sz      = new SIZE(requestedSize.Width, requestedSize.Height);

            IShellFolder           desktop   = null;
            IntPtr                 pidlMain  = IntPtr.Zero;
            IShellItem             shellItem = null;
            IShellItemImageFactory ppsiShellItemImageFactory = null;

            try
            {
                Shell32.SHGetDesktopFolder(ref desktop);

                int cParsed   = 0;
                int pdwAttrib = 0;
                desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filename, out cParsed, out pidlMain, out pdwAttrib);

                //Shell32.SHCreateItemFromIDList(pidlMain, uuidIShellItem, out shellItem);
                Shell32.SHCreateShellItem(IntPtr.Zero, null, pidlMain, out shellItem);

                ppsiShellItemImageFactory = (IShellItemImageFactory)shellItem;
                if (onlyThumbnail)
                {
                    ppsiShellItemImageFactory.GetImage(sz, SIIGBF.SIIGBF_BIGGERSIZEOK | SIIGBF.SIIGBF_THUMBNAILONLY, out hbitmap);
                }
                else
                {
                    ppsiShellItemImageFactory.GetImage(sz, SIIGBF.SIIGBF_BIGGERSIZEOK, out hbitmap);
                }

                bm1 = GetBitmapFromHbitmap(hbitmap);
            }
            catch (InvalidCastException)
            {
                Debug.Write("    Did not support IShellItemImageFactory\r\n");
            }
            catch (Exception e)
            {
                Debug.Write("    Exception extracting image from IShellItemImageFactory: " + e.Message + "\r\n");
            }
            finally
            {
                if (ppsiShellItemImageFactory != null)
                {
                    Marshal.ReleaseComObject(ppsiShellItemImageFactory);
                    ppsiShellItemImageFactory = null;
                }
                if (shellItem != null)
                {
                    Marshal.ReleaseComObject(shellItem);
                    shellItem = null;
                }
                if (pidlMain != IntPtr.Zero)
                {
                    Allocator.Free(pidlMain);
                    pidlMain = IntPtr.Zero;
                }
                if (desktop != null)
                {
                    Marshal.ReleaseComObject(desktop);
                    desktop = null;
                }
                if (_allocator != null)
                {
                    Marshal.ReleaseComObject(_allocator);
                    _allocator = null;
                }
            }

            return(bm1);
        }
Exemple #27
0
        public Bitmap LoadThumbnailFromExtractImage(string filename, Size requestedSize)
        {
            if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename))
            {
                return(null);
            }

            Bitmap bm1     = null;
            IntPtr hbitmap = IntPtr.Zero;
            SIZE   sz      = new SIZE(requestedSize.Width, requestedSize.Height);

            IShellFolder  desktop      = null;
            IntPtr        pidlParent   = IntPtr.Zero;
            IShellFolder  shellFolder  = null;
            IUnknown      iunk         = null;
            IExtractImage extractImage = null;

            try
            {
                Debug.Write("Trying IExtractImage\r\n");

                Shell32.SHGetDesktopFolder(ref desktop);

                int    cParsed   = 0;
                int    pdwAttrib = 0;
                string filePath  = filename;
                if (System.IO.File.Exists(filename))
                {
                    filePath = System.IO.Path.GetDirectoryName(filename);
                }
                desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib);

                Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
                desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder);

                Guid uuidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                iunk = GetUIObject(shellFolder, filename, uuidExtractImage);

                if (iunk == null)
                {
                    Debug.Write("    IExtractImage not supported\r\n");
                    return(null);
                }

                extractImage = (IExtractImage)iunk;
                StringBuilder location = new StringBuilder(260, 260);
                int           priority = 0;
                EIEIFLAG      flags    = EIEIFLAG.IEIFLAG_ORIGSIZE;
                int           uFlags   = (int)flags;
                extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, 32, ref uFlags);
                extractImage.Extract(out hbitmap);
                bm1 = GetBitmapFromHbitmap(hbitmap);
            }
            catch (Exception e)
            {
                Debug.Write("    Exception extracting image from IExtractImage: " + e.Message + "\r\n");
            }
            finally
            {
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                    extractImage = null;
                }
                if (iunk != null)
                {
                    Marshal.ReleaseComObject(iunk);
                    iunk = null;
                }
                if (shellFolder != null)
                {
                    Marshal.ReleaseComObject(shellFolder);
                    shellFolder = null;
                }
                if (pidlParent != IntPtr.Zero)
                {
                    Allocator.Free(pidlParent);
                    pidlParent = IntPtr.Zero;
                }
                if (desktop != null)
                {
                    Marshal.ReleaseComObject(desktop);
                    desktop = null;
                }
                if (_allocator != null)
                {
                    Marshal.ReleaseComObject(_allocator);
                    _allocator = null;
                }
            }
            return(bm1);
        }
Exemple #28
0
 public static extern Int32 SHGetDesktopFolder(ref IShellFolder ppshf);
Exemple #29
0
 public ShellItem(IntPtr PIDL, IShellFolder ShellFolder, ShellItem ParentItem)
 {
     m_PIDL        = PIDL;
     m_ShellFolder = ShellFolder;
     m_ParentItem  = ParentItem;
 }
Exemple #30
0
 public ShellItem(IntPtr PIDL, IShellFolder ShellFolder)
 {
     m_PIDL        = PIDL;
     m_ShellFolder = ShellFolder;
     m_ParentItem  = null;
 }
        /// <summary>Gets the interfaces to the context menu</summary>
        /// <param name="oParentFolder">Parent folder</param>
        /// <param name="arrPIDLs">PIDLs</param>
        /// <returns>true if it got the interfaces, otherwise false</returns>
        private bool GetContextMenuInterfaces(IShellFolder oParentFolder, IntPtr[] arrPIDLs)
        {
            IntPtr pUnknownContextMenu = IntPtr.Zero;

            int nResult = oParentFolder.GetUIObjectOf(
                IntPtr.Zero,
                (uint)arrPIDLs.Length,
                arrPIDLs,
                ref IID_IContextMenu,
                IntPtr.Zero,
                out pUnknownContextMenu);

            if (S_OK == nResult)
            {
                _oContextMenu = (IContextMenu)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu, typeof(IContextMenu));

                IntPtr pUnknownContextMenu2 = IntPtr.Zero;
                if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu2, out pUnknownContextMenu2))
                {
                    _oContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu2, typeof(IContextMenu2));
                }
                IntPtr pUnknownContextMenu3 = IntPtr.Zero;
                if (S_OK == Marshal.QueryInterface(pUnknownContextMenu, ref IID_IContextMenu3, out pUnknownContextMenu3))
                {
                    _oContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(pUnknownContextMenu3, typeof(IContextMenu3));
                }

                return true;
            }
            else
            {
                return false;
            }
        }
Exemple #32
0
 internal static extern int SHCreateShellItem(
     IntPtr pidlParent,
     [In, MarshalAs(UnmanagedType.Interface)] IShellFolder psfParent,
     IntPtr pidl,
     [MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi
     );
 static IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl)
 {
     IShellFolder folder;
     var result = parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder);
     Marshal.ThrowExceptionForHR((int)result);
     return folder;
 }
Exemple #34
0
 public static extern IShellItem SHCreateItemWithParent(
     [In] IntPtr pidlParent,
     [In] IShellFolder psfParent,
     [In] IntPtr pidl,
     [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
 internal abstract IShellView CreateShellView(IShellFolder folder);
Exemple #36
0
 public static extern int SHGetDesktopFolder(out IShellFolder ppshf);
Exemple #37
0
        /// <summary>
        /// 根据路径获取 IShellFolder
        /// </summary>
        public static IShellFolder GetShellFolder(IShellFolder desktop, string path)
        {
            IntPtr Pidl;

            return(GetShellFolder(desktop, path, out Pidl));
        }
Exemple #38
0
 /// <summary>
 /// Creates the shell view.
 /// </summary>
 /// <param name="folder">The folder.</param>
 /// <returns>A shell view.</returns>
 internal override IShellView CreateShellView(IShellFolder folder)
 {
     return(new ShellViewHost(customView));
 }
Exemple #39
0
 public static extern int SHGetDataFromIDList(IShellFolder psf, IntPtr pidl, SHGDFIL nFormat, out WIN32_FIND_DATA pv, int cb);
Exemple #40
0
 public static extern int SHBindToParent(IntPtr pidl, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IShellFolder ppv, out IntPtr ppidlLast);
 public static extern int SHBindToObject(
     IShellFolder psf,
     IntPtr pidl,
     [MarshalAs(UnmanagedType.IUnknown)] object pbc,
     ref Guid riid,
     out IShellFolder ppv);
Exemple #42
0
        /// <summary>
        /// Gets the desktop folder
        /// </summary>
        /// <returns>IShellFolder for desktop folder</returns>
        private IShellFolder GetDesktopFolder()
        {
            IntPtr pUnkownDesktopFolder = IntPtr.Zero;

            if (null == _oDesktopFolder)
            {
                // Get desktop IShellFolder
                int nResult = SHGetDesktopFolder(out pUnkownDesktopFolder);
                if (S_OK != nResult)
                {
                    throw new ShellContextMenuException("Failed to get the desktop shell folder");
                }
                _oDesktopFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnkownDesktopFolder, typeof(IShellFolder));
            }

            return _oDesktopFolder;
        }
 internal static extern HRESULT SHGetDesktopFolder(
     [MarshalAs(UnmanagedType.Interface)] out IShellFolder ppshf);
Exemple #44
0
 /// <summary>
 /// Release all allocated interfaces, PIDLs 
 /// </summary>
 private void ReleaseAll()
 {
     if (null != _oContextMenu)
     {
         Marshal.ReleaseComObject(_oContextMenu);
         _oContextMenu = null;
     }
     if (null != _oContextMenu2)
     {
         Marshal.ReleaseComObject(_oContextMenu2);
         _oContextMenu2 = null;
     }
     if (null != _oContextMenu3)
     {
         Marshal.ReleaseComObject(_oContextMenu3);
         _oContextMenu3 = null;
     }
     if (null != _oDesktopFolder)
     {
         Marshal.ReleaseComObject(_oDesktopFolder);
         _oDesktopFolder = null;
     }
     if (null != _oParentFolder)
     {
         Marshal.ReleaseComObject(_oParentFolder);
         _oParentFolder = null;
     }
     if (null != _arrPIDLs)
     {
         FreePIDLs(_arrPIDLs);
         _arrPIDLs = null;
     }
 }
        /// <summary>
        /// Retrieves the IContextMenu for specific items
        /// </summary>
        /// <param name="parent">the parent IShellFolder which contains the items</param>
        /// <param name="pidls">the pidls of the items for which to retrieve the IContextMenu</param>
        /// <param name="icontextMenuPtr">the pointer to the IContextMenu</param>
        /// <param name="iContextMenu">the IContextMenu for the items</param>
        /// <returns>true if the IContextMenu has been retrieved succesfully, false otherwise</returns>
        public static bool GetIContextMenu(
            IShellFolder parent,
            IntPtr[] pidls,
            out IntPtr iContextMenuPtr,
            out IContextMenu iContextMenu)
        {
            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        (uint)pidls.Length,
                        pidls,
                        ref ShellAPI.IID_IContextMenu,
                        IntPtr.Zero,
                        out iContextMenuPtr) == ShellAPI.S_OK)
            {
                iContextMenu =
                    (IContextMenu)Marshal.GetTypedObjectForIUnknown(
                        iContextMenuPtr, typeof(IContextMenu));

                return true;
            }
            else
            {
                iContextMenuPtr = IntPtr.Zero;
                iContextMenu = null;

                return false;
            }
        }
        private void DisposeShellItem()
        {
            disposed = true;

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

            if (shellFolderPtr != IntPtr.Zero)
            {
                try
                {
                    Marshal.Release(shellFolderPtr);
                }
                catch (Exception)
                {
                }
                finally
                {
                    shellFolderPtr = IntPtr.Zero;
                }
            }

            PIDLRel.Free();
        }
 public static bool GetShellFolder(IntPtr pIDLFolder, out IShellFolder shellFolder) {
     shellFolder = null;
     if(PInvoke.ILGetSize(pIDLFolder) == 2) {
         return (0 == PInvoke.SHGetDesktopFolder(out shellFolder));
     }
     IShellFolder ppshf = null;
     Guid riid = ExplorerGUIDs.IID_IShellFolder;
     try {
         if(PInvoke.SHGetDesktopFolder(out ppshf) == 0) {
             return (0 == ppshf.BindToObject(pIDLFolder, IntPtr.Zero, ref riid, out shellFolder));
         }
     }
     catch {
     }
     finally {
         if(ppshf != null) {
             Marshal.ReleaseComObject(ppshf);
         }
     }
     return false;
 }
 public static NativeShellFolder ToNative(this IShellFolder @this)
 {
     return(@this != null ? new NativeShellFolder(@this) : null);
 }
        static IntPtr GetShellFolderChildrenRelativePIDL(IShellFolder parentFolder, string displayName)
        {
            var bindCtx = NativeMethods.CreateBindCtx();

            uint pchEaten;
            uint pdwAttributes = 0;
            IntPtr ppidl;
            parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out pchEaten, out ppidl, ref pdwAttributes);

            return ppidl;
        }
Exemple #50
0
        private bool _getThumbNail(string file, IntPtr pidl, IShellFolder item)
        {
            IntPtr        hBmp         = IntPtr.Zero;
            IExtractImage extractImage = null;

            try
            {
                string pidlPath = PathFromPidl(pidl);
                if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
                {
                    IUnknown iunk            = null;
                    int      prgf            = 0;
                    Guid     iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1");
                    item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, ref prgf, ref iunk);
                    extractImage = (IExtractImage)iunk;
                    if (extractImage != null)
                    {
                        Console.WriteLine("Got an IExtractImage object!");
                        SIZE sz = new SIZE();
                        sz.cx = DesiredSize.Width;
                        sz.cy = DesiredSize.Height;
                        StringBuilder location             = new StringBuilder(260, 260);
                        int           priority             = 0;
                        int           requestedColourDepth = 32;
                        EIEIFLAG      flags  = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN;
                        int           uFlags = (int)flags;
                        try
                        {
                            extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);
                            extractImage.Extract(ref hBmp);
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                        }
                        if (hBmp != IntPtr.Zero)
                        {
                            _thumbNail = Bitmap.FromHbitmap(hBmp);
                        }
                        Marshal.ReleaseComObject(extractImage);
                        extractImage = null;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (hBmp != IntPtr.Zero)
                {
                    UnmanagedMethods.DeleteObject(hBmp);
                }
                if (extractImage != null)
                {
                    Marshal.ReleaseComObject(extractImage);
                }
                throw ex;
            }
        }
 public static extern Int32 SHGetRealIDL(IShellFolder psf, IntPtr pidlSimple, out IntPtr ppidlReal);
Exemple #52
0
 public static extern int SHCreateShellItem(IntPtr pidlParent, IShellFolder psfParent, IntPtr pidl, out IShellItem ppsi);
Exemple #53
0
        /// <summary>
        /// Gets the parent folder
        /// </summary>
        /// <param name="folderName">Folder path</param>
        /// <returns>IShellFolder for the folder (relative from the desktop)</returns>
        private IShellFolder GetParentFolder(string folderName)
        {
            if (null == _oParentFolder)
            {
                IShellFolder oDesktopFolder = GetDesktopFolder();
                if (null == oDesktopFolder)
                {
                    return null;
                }

                // Get the PIDL for the folder file is in
                IntPtr pPIDL = IntPtr.Zero;
                uint pchEaten = 0;
                SFGAO pdwAttributes = 0;
                int nResult = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref pchEaten, out pPIDL, ref pdwAttributes);
                if (S_OK != nResult)
                {
                    return null;
                }

                IntPtr pStrRet = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);
                Marshal.WriteInt32(pStrRet, 0, 0);
                nResult = _oDesktopFolder.GetDisplayNameOf(pPIDL, SHGNO.FORPARSING, pStrRet);
                StringBuilder strFolder = new StringBuilder(MAX_PATH);
                StrRetToBuf(pStrRet, pPIDL, strFolder, MAX_PATH);
                Marshal.FreeCoTaskMem(pStrRet);
                pStrRet = IntPtr.Zero;
                _strParentFolder = strFolder.ToString();

                // Get the IShellFolder for folder
                IntPtr pUnknownParentFolder = IntPtr.Zero;
                nResult = oDesktopFolder.BindToObject(pPIDL, IntPtr.Zero, ref IID_IShellFolder, out pUnknownParentFolder);
                // Free the PIDL first
                Marshal.FreeCoTaskMem(pPIDL);
                if (S_OK != nResult)
                {
                    return null;
                }
                _oParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnknownParentFolder, typeof(IShellFolder));
            }

            return _oParentFolder;
        }
Exemple #54
0
        private static ImageData LoadThumbnail2(string path, DateTime dtLastWriteTime, out Size sizeRaw, out Size sizeActual, out string toolTipText, out bool fCached)
        {
            sizeRaw     = sizeActual = Size.Empty;
            toolTipText = null;
            fCached     = false;
            IntPtr       zero = IntPtr.Zero;
            IShellFolder ppv  = null;
            object       obj2 = null;

            try {
                IntPtr ptr3;
                zero = PInvoke.ILCreateFromPath(path);
                if ((zero != IntPtr.Zero) && (PInvoke.SHBindToParent(zero, ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr3) == 0))
                {
                    uint     rgfReserved = 0;
                    Guid     riid        = ExplorerGUIDs.IID_IExtractImage;
                    IntPtr[] apidl       = new IntPtr[] { ptr3 };
                    if (ppv.GetUIObjectOf(IntPtr.Zero, 1, apidl, ref riid, ref rgfReserved, out obj2) == 0)
                    {
                        IntPtr        ptr2;
                        IExtractImage image         = (IExtractImage)obj2;
                        StringBuilder pszPathBuffer = new StringBuilder(260);
                        int           pdwPriority   = 0;
                        Size          prgSize       = new Size(QTUtility.PreviewMaxWidth, QTUtility.PreviewMaxHeight);
                        int           pdwFlags      = 0x60;
                        if (((image.GetLocation(pszPathBuffer, pszPathBuffer.Capacity, ref pdwPriority, ref prgSize, 0x18, ref pdwFlags) == 0) && (image.Extract(out ptr2) == 0)) && (ptr2 != IntPtr.Zero))
                        {
                            Bitmap bmp  = Image.FromHbitmap(ptr2);
                            Size   size = bmp.Size;
                            sizeRaw = sizeActual = size;
                            ImageData data = new ImageData(bmp, null, path, dtLastWriteTime, size, size);
                            data.Thumbnail = true;
                            try {
                                toolTipText = data.TooltipText = ShellMethods.GetShellInfoTipText(zero, false);
                            }
                            catch {
                            }
                            return(data);
                        }
                    }
                }
            }
            catch (Exception exception) {
                QTUtility2.MakeErrorLog(exception, null);
            }
            finally {
                if (zero != IntPtr.Zero)
                {
                    PInvoke.CoTaskMemFree(zero);
                }
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                }
                if (obj2 != null)
                {
                    Marshal.ReleaseComObject(obj2);
                }
            }
            return(null);
        }
 private static bool CheckDisplayName(IShellFolder shellFolder, IntPtr pIDLLast, Regex re) {
     if(pIDLLast != IntPtr.Zero) {
         STRRET strret;
         uint uFlags = 0;
         StringBuilder pszBuf = new StringBuilder(260);
         if(shellFolder.GetDisplayNameOf(pIDLLast, uFlags, out strret) == 0) {
             PInvoke.StrRetToBuf(ref strret, pIDLLast, pszBuf, pszBuf.Capacity);
         }
         if(pszBuf.Length > 0) {
             return re.IsMatch(pszBuf.ToString());
         }
     }
     return false;
 }
Exemple #56
0
 internal extern static int SHGetDesktopFolder(ref IShellFolder ppshf);
Exemple #57
0
        public Bitmap GetThumbnail(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(null);
            }

            if (!File.Exists(fileName) && !Directory.Exists(fileName))
            {
                throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName);
            }
            if (_thumbNail != null)
            {
                _thumbNail.Dispose();
                _thumbNail = null;
            }
            IShellFolder folder = null;

            try
            {
                folder = getDesktopFolder;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (folder != null)
            {
                IntPtr pidlMain = IntPtr.Zero;
                try
                {
                    int    cParsed   = 0;
                    int    pdwAttrib = 0;
                    string filePath  = Path.GetDirectoryName(fileName);
                    folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref cParsed, ref pidlMain, ref pdwAttrib);
                }
                catch (Exception ex)
                {
                    Marshal.ReleaseComObject(folder);
                    throw ex;
                }
                if (pidlMain != IntPtr.Zero)
                {
                    Guid         iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
                    IShellFolder item           = null;
                    try
                    {
                        folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
                    }
                    catch (Exception ex)
                    {
                        Marshal.ReleaseComObject(folder);
                        Allocator.Free(pidlMain);
                        throw ex;
                    }
                    if (item != null)
                    {
                        IEnumIDList idEnum = null;
                        try
                        {
                            item.EnumObjects(IntPtr.Zero, (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS), ref idEnum);
                        }
                        catch (Exception ex)
                        {
                            Marshal.ReleaseComObject(folder);
                            Allocator.Free(pidlMain);
                            throw ex;
                        }
                        if (idEnum != null)
                        {
                            int    hRes     = 0;
                            IntPtr pidl     = IntPtr.Zero;
                            int    fetched  = 0;
                            bool   complete = false;
                            while (!complete)
                            {
                                hRes = idEnum.Next(1, ref pidl, ref fetched);
                                if (hRes != 0)
                                {
                                    pidl     = IntPtr.Zero;
                                    complete = true;
                                }
                                else
                                {
                                    if (_getThumbNail(fileName, pidl, item))
                                    {
                                        complete = true;
                                    }
                                }
                                if (pidl != IntPtr.Zero)
                                {
                                    Allocator.Free(pidl);
                                }
                            }
                            Marshal.ReleaseComObject(idEnum);
                        }
                        Marshal.ReleaseComObject(item);
                    }
                    Allocator.Free(pidlMain);
                }
                Marshal.ReleaseComObject(folder);
            }
            return(ThumbNail);
        }
Exemple #58
0
 internal static extern HResult SHCreateItemWithParent(IntPtr pidlParaent, IShellFolder psfParent, IntPtr pidl, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] ref object ppvItem);
        public void Update(IntPtr newPidlFull, ShellItemUpdateType changeType)
        {
            Browser.UpdateCondition.ContinueUpdate = false;

            lock (Browser)
            {
                #region Change Pidl and ShellFolder

                if (newPidlFull != IntPtr.Zero)
                {
                    IntPtr tempPidl = Pidl.ILClone(Pidl.ILFindLastID(newPidlFull)), newPidlRel, newShellFolderPtr;
                    Shell32.SHGetRealIDL(ParentItem.ShellFolder, tempPidl, out newPidlRel);

                    if (IsFolder && ParentItem.ShellFolder.BindToObject(newPidlRel, IntPtr.Zero, ref ShellGuids.IShellFolder, out newShellFolderPtr) == 0)
                    {
                        Marshal.ReleaseComObject(shellFolder);
                        Marshal.Release(shellFolderPtr);
                        PIDLRel.Free();

                        shellFolderPtr = newShellFolderPtr;
                        shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder));
                        PIDLRel = new Pidl(newPidlRel, false);

                        foreach (ShellNode child in SubFolders)
                        {
                            UpdateShellFolders(child);
                        }
                    }
                    else
                    {
                        PIDLRel.Free();
                        PIDLRel = new Pidl(newPidlRel, false);
                    }

                    Marshal.FreeCoTaskMem(tempPidl);
                    Marshal.FreeCoTaskMem(newPidlFull);
                }

                #endregion

                #region Make Other Changes

                switch (changeType)
                {
                    case ShellItemUpdateType.Renamed:
                        SetText(this);
                        SetPath(this);
                        break;

                    case ShellItemUpdateType.Updated:
                        if (IsFolder)
                        {
                            SetAttributesFolder(this);
                        }
                        else
                        {
                            SetAttributesFile(this);
                        }
                        break;

                    case ShellItemUpdateType.MediaChange:
                        SetInfo(this);
                        Clear(true, true);
                        break;

                    case ShellItemUpdateType.IconChange:
                        SetInfo(this);
                        break;
                }

                #endregion
            }

            Browser.OnShellItemUpdate(ParentItem, new ShellItemUpdateEventArgs(this, this, changeType));
        }
        public static List <ShellItem> GetItems(Guid FolderID) //Получает список элементов каталога по GUID
        {
            IntPtr           p       = IntPtr.Zero;
            IShellFolder     pFolder = null;
            IEnumIDList      pEnum   = null;
            IntPtr           pItem   = IntPtr.Zero;
            IntPtr           lpStr   = IntPtr.Zero;
            STRRET           strret;
            Guid             guid  = typeof(IShellFolder).GUID;
            List <ShellItem> items = new List <ShellItem>();
            ShellItem        si;

            try
            {
                int hr = NativeMethods.SHGetKnownFolderIDList(ref FolderID, 0, IntPtr.Zero, out p);
                if (hr != 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }

                hr = NativeMethods.SHBindToObject(null, p, null, ref guid, out pFolder);
                if (hr != 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }

                pFolder.EnumObjects(IntPtr.Zero, NativeMethods.SHCONTF_FOLDERS | NativeMethods.SHCONTF_NONFOLDERS, out pEnum);

                while (true)
                {
                    pItem = IntPtr.Zero;
                    uint res = pEnum.Next(1, out pItem, IntPtr.Zero);
                    if (res != 0)
                    {
                        break;
                    }
                    si = new ShellItem();

                    //display name
                    lpStr  = IntPtr.Zero;
                    strret = new STRRET();
                    pFolder.GetDisplayNameOf(pItem, NativeMethods.SHGDN_NORMAL, out strret);
                    hr = NativeMethods.StrRetToStr(ref strret, pItem, out lpStr);
                    if (hr != 0)
                    {
                        throw Marshal.GetExceptionForHR(hr);
                    }
                    string s = Marshal.PtrToStringUni(lpStr);
                    si.DisplayName = s;
                    NativeMethods.CoTaskMemFree(lpStr);

                    //path
                    lpStr  = IntPtr.Zero;
                    strret = new STRRET();
                    pFolder.GetDisplayNameOf(pItem, NativeMethods.SHGDN_FORPARSING, out strret);
                    hr = NativeMethods.StrRetToStr(ref strret, pItem, out lpStr);
                    if (hr != 0)
                    {
                        throw Marshal.GetExceptionForHR(hr);
                    }
                    s = Marshal.PtrToStringUni(lpStr);
                    try { si.Path = new Uri(s); }
                    catch (UriFormatException) { si.Path = new Uri("file://localhost/" + s); }
                    NativeMethods.CoTaskMemFree(lpStr);

                    //icon
                    try
                    {
                        Guid         iid_IIExtractIcon = typeof(IExtractIcon).GUID;
                        IExtractIcon pExtract;
                        pFolder.GetUIObjectOf(IntPtr.Zero, 1, new IntPtr[] { pItem }, ref iid_IIExtractIcon, 0, out pExtract);

                        StringBuilder sbIcon = new StringBuilder(260);
                        int           index  = 0;
                        uint          flags;
                        hr = pExtract.GetIconLocation(NativeMethods.GIL_FORSHELL, sbIcon, 260, out index, out flags);
                        if (hr == 0)
                        {
                            IntPtr hIconSmall = IntPtr.Zero, hIconLarge = IntPtr.Zero;
                            hr = pExtract.Extract(sbIcon.ToString(), (uint)index, out hIconLarge, out hIconSmall, 0x00140014);
                            if (hr == 0 && hIconSmall != IntPtr.Zero)
                            {
                                var icon   = System.Drawing.Icon.FromHandle(hIconSmall);
                                var bitmap = icon.ToBitmap();

                                using (bitmap)
                                {
                                    MemoryStream ms = new MemoryStream();
                                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                    si.Image = ms.ToArray();
                                }

                                NativeMethods.DestroyIcon(hIconSmall);
                                NativeMethods.DestroyIcon(hIconLarge);
                            }
                            else
                            {
                                si.Image = new byte[0];
                            }
                        }
                        else
                        {
                            si.Image = new byte[0];
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                        si.Image = new byte[0];
                    }
                    items.Add(si);
                    NativeMethods.CoTaskMemFree(pItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), ex.GetType().ToString());
            }
            finally
            {
                if (p != IntPtr.Zero)
                {
                    NativeMethods.CoTaskMemFree(p);
                }
                if (pFolder != null)
                {
                    Marshal.ReleaseComObject(pFolder);
                }
                if (pEnum != null)
                {
                    Marshal.ReleaseComObject(pEnum);
                }
            }
            return(items);
        }