Exemplo n.º 1
0
        private static List <string> ProcessCIDA(IntPtr p)
        {
            List <string> lFiles = new List <string>();
            // Get number of items.
            UInt32 cidl = (UInt32)Marshal.ReadInt32(p);
            // Get parent folder.
            int           offset     = sizeof(UInt32);
            IntPtr        parentpidl = (IntPtr)((int)p + (UInt32)Marshal.ReadInt32(p, offset));
            StringBuilder path       = new StringBuilder(256);
            bool          br         = User32.SHGetPathFromIDListW(parentpidl, path);
            string        sParrent   = PidlManager.GetPidlDisplayName(parentpidl);

            // Get subitems.
            for (int i = 1; i <= cidl; ++i)
            {
                offset += sizeof(UInt32);
                IntPtr relpidl = (IntPtr)((int)p + (UInt32)Marshal.ReadInt32(p, offset));
                IntPtr abspidl = Shell32.ILCombine(parentpidl, relpidl);
                bool   br1     = User32.SHGetPathFromIDListW(abspidl, path);
                string sFile   = PidlManager.GetPidlDisplayName(abspidl);

                Shell32.ILFree(abspidl);
                if (br1)
                {
                    lFiles.Add(path.ToString());
                }
                else
                {
                    lFiles.Add(string.Format("{0}\\{1}", sParrent, sFile));
                }
            }
            return(lFiles);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns an enumerator detailing the child items of the
        /// <see cref="ShellItem"/>.
        /// </summary>
        ///
        /// <param name="filter">
        /// A filter describing the types of child items to be included.
        /// </param>
        ///
        /// <returns>
        /// An enumerator over all child items.
        /// </returns>
        public IEnumerator <ShellItem> GetEnumerator(SHCONTF filter)
        {
            IShellFolder folder = GetIShellFolder();
            IEnumIDList  enumId = GetIEnumIDList(folder, filter);
            uint         count;
            IntPtr       pidl;
            HResult      result;

            if (enumId == null)
            {
                yield break;
            }

            result = enumId.Next(1, out pidl, out count);
            while (result == HResult.S_OK)
            {
                yield return(new ShellItem(this, pidl));

                Shell32.ILFree(pidl);
                result = enumId.Next(1, out pidl, out count);
            }

            if (result != HResult.S_FALSE)
            {
                Marshal.ThrowExceptionForHR((int)result);
            }

            yield break;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShellItem"/> class.
        /// </summary>
        ///
        /// <remarks>
        /// Takes an <see cref="Environment.SpecialFolder"/> containing the
        /// location of the folder.
        /// </remarks>
        ///
        /// <param name="folder">
        /// An <see cref="Environment.SpecialFolder"/> containing the
        /// location of the folder.
        /// </param>
        public ShellItem(Environment.SpecialFolder folder)
        {
            IntPtr pidl;

            if (Shell32.SHGetSpecialFolderLocation(IntPtr.Zero,
                                                   (CSIDL)folder, out pidl) == HResult.S_OK)
            {
                try
                {
                    m_ComInterface = CreateItemFromIDList(pidl);
                }
                finally
                {
                    Shell32.ILFree(pidl);
                }
            }
            else
            {
                // SHGetSpecialFolderLocation does not support many common
                // CSIDL values on Windows 98, but SHGetFolderPath in
                // ShFolder.dll does, so fall back to it if necessary. We
                // try SHGetSpecialFolderLocation first because it returns
                // a PIDL which is preferable to a path as it can express
                // virtual folder locations.
                StringBuilder path = new StringBuilder();
                Marshal.ThrowExceptionForHR((int)Shell32.SHGetFolderPath(
                                                IntPtr.Zero, (CSIDL)folder, IntPtr.Zero, 0, path));
                m_ComInterface = CreateItemFromParsingName(path.ToString());
            }
        }
        public static string GetVideosFolder()
        {
            string path = null;
            IntPtr ppidl;

            int ret = Shell32.SHGetKnownFolderIDList(ref FOLDERID_Videos, 0, IntPtr.Zero, out ppidl);

            if (ret == 0)
            {
                StringBuilder sb = new StringBuilder(Kernel32.MAX_PATH + 1);

                if (Shell32.SHGetPathFromIDList(ppidl, sb))
                {
                    path = sb.ToString();
                }
            }

            if (ppidl != IntPtr.Zero)
            {
                Shell32.ILFree(ppidl);
            }


            return(path);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShellItem"/> class.
        /// </summary>
        ///
        /// <remarks>
        /// Creates a ShellItem which is a named child of <paramref name="parent"/>.
        /// </remarks>
        ///
        /// <param name="parent">
        /// The parent folder of the item.
        /// </param>
        ///
        /// <param name="name">
        /// The name of the child item.
        /// </param>
        public ShellItem(ShellItem parent, string name)
        {
            if (parent.IsFileSystem)
            {
                // If the parent folder is in the file system, our best
                // chance of success is to use the FileSystemPath to
                // create the new item. Folders other than Desktop don't
                // seem to implement ParseDisplayName properly.
                m_ComInterface = CreateItemFromParsingName(
                    Path.Combine(parent.FileSystemPath, name));
            }
            else
            {
                IShellFolder folder = parent.GetIShellFolder();
                uint         eaten;
                IntPtr       pidl;
                uint         attributes = 0;

                folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero,
                                        name, out eaten, out pidl, ref attributes);

                try
                {
                    m_ComInterface = CreateItemFromIDList(pidl);
                }
                finally
                {
                    Shell32.ILFree(pidl);
                }
            }
        }
Exemplo n.º 6
0
        public void CanGetAndFreeKnownFolderIdList()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Cookies, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero, out pidl);
            Assert.IsTrue(pidl != IntPtr.Zero);
            Assert.DoesNotThrow(() => Shell32.ILFree(pidl));
        }
Exemplo n.º 7
0
 public void ILFree()
 {
     if (!hClone.IsEmpty)
     {
         Shell32.ILFree(hClone.pidl);
     }
     hClone = PidlHandle.Empty;
 }
Exemplo n.º 8
0
        public void CanDecodePidl()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Cookies, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero, out pidl);
            var idList = PidlManager.Decode(pidl);

            Shell32.ILFree(pidl);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Gets the desktop idlist.
 /// </summary>
 /// <returns>The desktop idlist.</returns>
 public static IdList GetDesktop()
 {
     IntPtr pidl;
     Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Desktop, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero,
         out pidl);
     var idlist = IdList.Create(Decode(pidl));
     Shell32.ILFree(pidl);
     return idlist;
 }
        /// <summary>
        /// Returns a known folder given its shell namespace parsing name, such as
        /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
        /// </summary>
        /// <param name="parsingName">The parsing name (or path) for the requested known folder.</param>
        /// <returns>A known folder representing the specified name.</returns>
        /// <exception cref="System.ArgumentException">Thrown if the given parsing name is invalid.</exception>
        public static IKnownFolder FromParsingName(string parsingName)
        {
            if (parsingName == null)
            {
                throw new ArgumentNullException("parsingName");
            }

            IntPtr pidl  = IntPtr.Zero;
            IntPtr pidl2 = IntPtr.Zero;

            try
            {
                pidl = Shell32.PidlFromParsingName(parsingName);

                if (pidl == IntPtr.Zero)
                {
                    throw new ArgumentException("Parsing Name", "parsingName");
                }

                // It's probably a special folder, try to get it
                IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl);
                if (knownFolderNative != null)
                {
                    IKnownFolder kf = KnownFolderHelper.GetKnownFolder(knownFolderNative);
                    if (kf == null)
                    {
                        throw new ArgumentException("Parsing Name", "parsingName");
                    }
                    return(kf);
                }

                // No physical storage was found for this known folder
                // We'll try again with a different name

                // try one more time with a trailing \0
                pidl2 = Shell32.PidlFromParsingName(parsingName.PadRight(1, '\0'));

                if (pidl2 == IntPtr.Zero)
                {
                    throw new ArgumentException("Parsing Name", "parsingName");
                }

                IKnownFolder kf2 = KnownFolderHelper.GetKnownFolder(KnownFolderHelper.FromPIDL(pidl));
                if (kf2 == null)
                {
                    throw new ArgumentException("Parsing Name", "parsingName");
                }

                return(kf2);
            }
            finally
            {
                Shell32.ILFree(pidl);
                Shell32.ILFree(pidl2);
            }
        }
Exemplo n.º 11
0
        IListItemEx[] IListItemEx.GetSubItems(bool isEnumHidden)
        {
            TaskScheduler taskScheduler = null;

            try {
                taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            }
            catch (InvalidOperationException) {
                SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
                taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            }

            Task <FileSystemListItem[]> taskk = Task.Factory.StartNew(() => {
                var fsiList         = new List <FileSystemListItem>();
                IShellFolder folder = this.GetIShellFolder();
                HResult navRes;
                IEnumIDList enumId = ShellItem.GetIEnumIDList(folder, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS |
                                                              SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC, out navRes);
                this.NavigationStatus = navRes;
                uint count;
                IntPtr pidl;

                //if (enumId == null) {
                //  break;
                //}
                System.Windows.Forms.Application.DoEvents();
                //Thread.Sleep(1);
                //HResult result = enumId.Next(1, out pidl, out count);
                var i          = 0;
                var parentItem = this.IsSearchFolder ? this._Item : new ShellItem(this.ParsingName.ToShellParsingName());

                while (enumId.Next(1, out pidl, out count) == HResult.S_OK)
                {
                    var fsi = new FileSystemListItem();
                    fsi.InitializeWithParent(parentItem, this.ParentHandle, pidl, i++);
                    fsiList.Add(fsi);
                    Shell32.ILFree(pidl);
                    //if (this.IsSearchFolder)
                    //	Thread.Sleep(1);
                    //System.Windows.Forms.Application.DoEvents();
                    //result = enumId.Next(1, out pidl, out count);
                }

                //if (result != HResult.S_FALSE) {
                //Marshal.ThrowExceptionForHR((int)result);
                //}

                parentItem.Dispose();
                return(fsiList.ToArray());
                //yield break;
            }, CancellationToken.None, TaskCreationOptions.None, taskScheduler);

            //taskk.Start(TaskScheduler.FromCurrentSynchronizationContext());
            taskk.Wait();
            return(taskk.Result);
        }
Exemplo n.º 12
0
        public void CanDecodeFilesystemPidl()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Documents, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero,
                                           out pidl);
            var idList = PidlManager.PidlToIdlist(pidl);

            Shell32.ILFree(pidl);
        }
Exemplo n.º 13
0
        public void CanGetPidlDisplayName()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Documents, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero,
                                           out pidl);
            var displayName = PidlManager.GetPidlDisplayName(pidl);

            Shell32.ILFree(pidl);
            Assert.AreEqual(displayName, "Documents");
        }
Exemplo n.º 14
0
        public static IShellFolder GetFolderImpl(this IntPtr pidl,
                                                 ref PidlFolderData data, SBSP wFlags)
        {
            IShellFolder result = null;

            int    hr                  = 0;
            IntPtr pidlTmp             = IntPtr.Zero;
            var    folderTmp           = data.folderTmp;
            var    currentAbsolutePidl = data.currentAbsolutePidl;
            var    folderTmpPtr        = data.folderTmpPtr;
            var    currentFolder       = data.currentFolder;

            IShellFolder desktopFolder     = PidlFolderData.DesktopFolder;
            var          desktopFolderPidl = PidlFolderData.DesktopFolderPidl;

            //  Check to see if we have a desktop pidl, relative pidl or absolite pidl.
            if (Shell32.ILIsEqual(pidl, desktopFolderPidl))
            {
                //  The provided PIDL is the desktop folder.
                pidlTmp   = Shell32.ILClone(desktopFolderPidl);
                folderTmp = desktopFolder;
            }
            else if ((wFlags & SBSP.SBSP_RELATIVE) != 0)
            {
                // SBSP_RELATIVE - pidl is relative from the current folder
                if ((hr = currentFolder.BindToObject(pidl, IntPtr.Zero,
                                                     ref Shell32.IID_IShellFolder,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(result); // hr;
                }
                pidlTmp = Shell32.ILCombine(currentAbsolutePidl, pidl);

                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }
            else
            {
                // SBSP_ABSOLUTE - pidl is an absolute pidl (relative from desktop)
                PidlClone pidlClone = PidlClone.Of(pidl);
                folderTmp = GetPidlCloneFolder(pidlClone);

                pidlTmp = pidlClone.Handle;
            }

            if (folderTmp == null)
            {
                Shell32.ILFree(pidlTmp);
                return(result); // WinError.E_FAIL;
            }

            result = folderTmp;
            return(result);
        }
Exemplo n.º 15
0
        public void CanGetDesktopFolderLocationAndPath()
        {
            //  Asserts we can get the desktop folder pidl, get a path for it and free the pidl.
            IntPtr pidl;

            Shell32.SHGetFolderLocation(IntPtr.Zero, CSIDL.CSIDL_DESKTOP, IntPtr.Zero, 0, out pidl);
            var sb = new StringBuilder(260);

            Assert.IsTrue(Shell32.SHGetPathFromIDList(pidl, sb));
            Assert.That(sb.ToString(), Is.Not.Null.Or.Empty);
            Assert.DoesNotThrow(() => Shell32.ILFree(pidl));
        }
Exemplo n.º 16
0
        public void CanGetPidlDisplayName()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Documents, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero,
                                           out pidl);
            var displayName = PidlManager.GetPidlDisplayName(pidl);

            Shell32.ILFree(pidl);
            string expectedName = GetTestKnownFolderDisplayNameForMyCulture();

            Assert.AreEqual(expectedName, displayName);
        }
Exemplo n.º 17
0
        public IEnumerator <IListItemEx> GetEnumerator()
        {
            IShellFolder folder = this.GetIShellFolder();

            if (folder == null)
            {
                yield return(null);
            }
            HResult     navRes;
            IEnumIDList enumId = ShellItem.GetIEnumIDList(folder, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS |
                                                          SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT, out navRes);

            this.NavigationStatus = navRes;
            uint   count;
            IntPtr pidl;

            if (enumId == null)
            {
                yield break;
            }

            HResult result = enumId.Next(1, out pidl, out count);
            var     i      = 0;

            //var parentItem = new ShellItem(this._Item.Pidl); //this._Item;//this.IsSearchFolder ? this._Item : new ShellItem(this.ParsingName.ToShellParsingName());
            while (result == HResult.S_OK)
            {
                var fsi = new FileSystemListItem();
                try {
                    fsi.InitializeWithParent(this._Item, this.ParentHandle, pidl, i++);
                }
                catch {
                    continue;
                }
                fsi.IsParentSearchFolder = this.IsSearchFolder;
                yield return(fsi);

                Shell32.ILFree(pidl);
                result = enumId.Next(1, out pidl, out count);
            }

            if (result != HResult.S_FALSE)
            {
                //Marshal.ThrowExceptionForHR((int)result);
            }

            //parentItem.Dispose();
            yield break;
        }
Exemplo n.º 18
0
        public void CanBouncePidl()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Documents, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero,
                                           out pidl);
            var idList = PidlManager.PidlToIdlist(pidl);

            Shell32.ILFree(pidl);
            pidl = PidlManager.IdListToPidl(idList);
            var pszPath     = new StringBuilder();
            var displayName = PidlManager.GetPidlDisplayName(pidl);

            Assert.AreEqual(displayName, "Documents");
        }
Exemplo n.º 19
0
 /// <summary>
 /// Clears up any resources associated with the SystemImageList
 /// when disposing is true.
 /// </summary>
 /// <param name="disposing">Whether the object is being disposed</param>
 public virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (ComInterface != null)
         {
             if (this.Pidl != IntPtr.Zero)
             {
                 Shell32.ILFree(this.Pidl);
             }
             Marshal.FinalReleaseComObject(ComInterface);
         }
         ComInterface = null;
     }
 }
Exemplo n.º 20
0
        public IEnumerable <IntPtr> GetItemsForCount(Boolean isEnumHidden)
        {
            var folder = this.GetIShellFolder();

            if (folder == null)
            {
                yield return(IntPtr.Zero);
            }

            HResult navRes;
            var     flags = SHCONTF.FOLDERS | SHCONTF.FASTITEMS | SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT;

            if (isEnumHidden)
            {
                flags = SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN | SHCONTF.INCLUDESUPERHIDDEN | SHCONTF.FASTITEMS | SHCONTF.NONFOLDERS | SHCONTF.ENABLE_ASYNC | SHCONTF.INIT_ON_FIRST_NEXT;
            }
            var enumId = ShellItem.GetIEnumIDList(folder, flags, out navRes);

            this.NavigationStatus = navRes;
            uint   count;
            IntPtr pidl;

            if (enumId == null)
            {
                yield break;
            }

            var result = enumId.Next(1, out pidl, out count);

            while (result == HResult.S_OK)
            {
                yield return(pidl);

                Shell32.ILFree(pidl);
                result = enumId.Next(1, out pidl, out count);
            }

            if (folder != null)
            {
                Marshal.ReleaseComObject(folder);
            }
            if (result != HResult.S_FALSE)
            {
                //Marshal.ThrowExceptionForHR((int)result);
            }
            yield break;
        }
Exemplo n.º 21
0
        public static void SelectItemInExplorer(IntPtr hwnd, string itemPath, bool edit)
        {
            if (itemPath == null)
            {
                throw new ArgumentNullException("itemPath");
            }
            IntPtr folder = PathToAbsolutePIDL(hwnd, Path.GetDirectoryName(itemPath));
            IntPtr file   = PathToAbsolutePIDL(hwnd, itemPath);

            try
            {
                SHOpenFolderAndSelectItems(folder, 1, new[] { file }, edit ? 1 : 0);
            }
            finally
            {
                Shell32.ILFree(folder);
                Shell32.ILFree(file);
            }
        }
Exemplo n.º 22
0
        IShellFolder IFolder(IntPtr pidl, IShellFolder pShellFolderInterface, ref int result)
        {
            if (Shell32.ILIsEqual(pidl, Desktop.PIDL))
            {
                result = WinError.S_OK;
                return((IShellFolder)Desktop.ShellFolderInterface);
            }

            IntPtr ppv = IntPtr.Zero;

            result = pShellFolderInterface.BindToObject(pidl, IntPtr.Zero,
                                                        ref Shell32.IID_IShellFolder,
                                                        out ppv);

            if (ppv == IntPtr.Zero)
            {
                // SBSP_ABSOLUTE - pidl is an absolute pidl (relative from desktop)
                var          pidlTmp       = Shell32.ILClone(pidl);
                IntPtr       folderTmpPtr  = IntPtr.Zero;
                IShellFolder desktopFolder = NativePidl.DesktopFolder;
                var          hr            = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero,
                                                                        ref Shell32.IID_IShellFolder,
                                                                        out folderTmpPtr);
                if (hr != WinError.S_OK)
                {
                    return(null);
                }

                var folderTmp = Marshal.GetObjectForIUnknown(folderTmpPtr) as IShellFolder;

                if (folderTmp == null)
                {
                    Shell32.ILFree(pidlTmp);
                    return(null);; // WinError.E_FAIL
                }

                return(folderTmp);
            }

            var shellFolderInterface = Marshal.GetObjectForIUnknown(ppv) as IShellFolder;

            return(shellFolderInterface);
        }
Exemplo n.º 23
0
        private static List <byte[][]> ProcessCIDA_Pidl(IntPtr p)
        {
            List <byte[][]> lsret = new List <byte[][]>();

            UInt32 cidl       = (UInt32)Marshal.ReadInt32(p);
            int    offset     = sizeof(UInt32);
            IntPtr parentpidl = (IntPtr)((int)p + (UInt32)Marshal.ReadInt32(p, offset));

            for (int i = 1; i <= cidl; ++i)
            {
                offset += sizeof(UInt32);
                IntPtr relpidl = (IntPtr)((int)p + (UInt32)Marshal.ReadInt32(p, offset));
                IntPtr abspidl = Shell32.ILCombine(parentpidl, relpidl);
                IdList idl     = PidlManager.PidlToIdlist(abspidl);
                lsret.Add(idl.Ids.Select(x => x.RawId).ToArray());
                Shell32.ILFree(abspidl);
            }

            return(lsret);
        }
Exemplo n.º 24
0
 protected void Dispose(bool dispose)
 {
     Shell32.ILFree(Pidl);
 }
    public static Dictionary <string, Icon> GetPrintersWithIcons(IntPtr hwndOwner)
    {
        Dictionary <string, Icon> result = new Dictionary <string, Icon>();

        Shell32.IShellFolder iDesktopFolder = Shell32.GetDesktopFolder();
        try
        {
            IntPtr pidlPrintersFolder;
            if (Shell32.SHGetFolderLocation(hwndOwner, (int)Shell32.CSIDL.CSIDL_PRINTERS, IntPtr.Zero, 0, out pidlPrintersFolder) == 0)
            {
                try
                {
                    StringBuilder strDisplay       = new StringBuilder(260);
                    Guid          guidIShellFolder = Shell32.IID_IShellFolder;
                    IntPtr        ptrPrintersShellFolder;
                    iDesktopFolder.BindToObject(pidlPrintersFolder, IntPtr.Zero, ref guidIShellFolder, out ptrPrintersShellFolder);
                    Object objPrintersShellFolder = Marshal.GetTypedObjectForIUnknown(ptrPrintersShellFolder, Shell32.ShellFolderType);
                    try
                    {
                        Shell32.IShellFolder printersShellFolder = (Shell32.IShellFolder)objPrintersShellFolder;
                        IntPtr ptrObjectsList;
                        printersShellFolder.EnumObjects(hwndOwner, Shell32.ESHCONTF.SHCONTF_NONFOLDERS, out ptrObjectsList);
                        Object objEnumIDList = Marshal.GetTypedObjectForIUnknown(ptrObjectsList, Shell32.EnumIDListType);
                        try
                        {
                            Shell32.IEnumIDList iEnumIDList = (Shell32.IEnumIDList)objEnumIDList;
                            IntPtr[]            rgelt       = new IntPtr[1];
                            IntPtr         pidlPrinter;
                            int            pceltFetched;
                            Shell32.STRRET ptrString;
                            while (iEnumIDList.Next(1, rgelt, out pceltFetched) == 0 && pceltFetched == 1)
                            {
                                printersShellFolder.GetDisplayNameOf(rgelt[0],
                                                                     Shell32.ESHGDN.SHGDN_NORMAL, out ptrString);
                                if (Shell32.StrRetToBuf(ref ptrString, rgelt[0], strDisplay,
                                                        (uint)strDisplay.Capacity) == 0)
                                {
                                    pidlPrinter = Shell32.ILCombine(pidlPrintersFolder, rgelt[0]);
                                    string             printerDisplayNameInPrintersFolder = strDisplay.ToString();
                                    Shell32.SHFILEINFO shinfo = new Shell32.SHFILEINFO();
                                    Shell32.SHGetFileInfo(pidlPrinter, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), Shell32.SHGFI.PIDL | Shell32.SHGFI.AddOverlays | Shell32.SHGFI.Icon);
                                    Icon printerIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
                                    Shell32.DestroyIcon(shinfo.hIcon);
                                    result.Add(printerDisplayNameInPrintersFolder, printerIcon);
                                }
                            }
                        }
                        finally
                        {
                            Marshal.ReleaseComObject(objEnumIDList);
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(objPrintersShellFolder);
                    }
                }
                finally
                {
                    Shell32.ILFree(pidlPrintersFolder);
                }
            }
        }
        finally
        {
            Marshal.ReleaseComObject(iDesktopFolder);
        }
        return(result);
    }
Exemplo n.º 26
0
        public static int BrowseObject(this ShellDebuggerModel model, IntPtr pidl, SBSP wFlags)
        {
            int          hr;
            IntPtr       folderTmpPtr;
            IShellFolder folderTmp = null;
            IntPtr       pidlTmp;

            //  We'll need the shell folder GUID.
            var shellFolderGuid     = typeof(IShellFolder).GUID;
            var desktopFolder       = model.DesktopFolder;
            var currentFolder       = model.currentFolder;
            var currentAbsolutePidl = model.currentAbsolutePidl;

            //  Check to see if we have a desktop pidl, relative pidl or absolite pidl.
            if (Shell32.ILIsEqual(pidl, model.DesktopFolderPidl))
            {
                //  The provided PIDL is the desktop folder.
                pidlTmp   = Shell32.ILClone(model.DesktopFolderPidl);
                folderTmp = model.DesktopFolder;
            }
            else if ((wFlags & SBSP.SBSP_RELATIVE) != 0)
            {
                // SBSP_RELATIVE - pidl is relative from the current folder
                if ((hr = currentFolder.BindToObject(pidl, IntPtr.Zero,
                                                     shellFolderGuid,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(WinError.E_FAIL);
                }

                pidlTmp = Shell32.ILCombine(currentAbsolutePidl, pidl);

                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }
            else
            {
                // SBSP_ABSOLUTE - pidl is an absolute pidl (relative from desktop)
                pidlTmp = Shell32.ILClone(pidl);
                if ((hr = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero,
                                                     shellFolderGuid,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(WinError.E_FAIL);
                }

                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }

            if (folderTmp == null)
            {
                Shell32.ILFree(pidlTmp);
                return(WinError.E_FAIL);
            }

            // Check that we have a new pidl
            if (Shell32.ILIsEqual(pidlTmp, currentAbsolutePidl))
            {
                Shell32.ILFree(pidlTmp);
                return(WinError.S_OK);
            }

            model.hWndListView = IntPtr.Zero;
            model.lastViewPidl = IntPtr.Zero;

            var ret = ChangeFolder(model, pidl, wFlags, folderTmp, pidlTmp);

            //var path = PidlManager.GetPidlDisplayName(pidlTmp);
            //if (path == "Control Panel" || model.lastViewPidl == IntPtr.Zero)
            //{
            //    IEnumIDList ppenumIDList = null;
            //    var hr3 = folderTmp.EnumObjects(model.Form.Handle,
            //        SHCONTF.SHCONTF_INCLUDEHIDDEN | SHCONTF.SHCONTF_INCLUDESUPERHIDDEN | SHCONTF.SHCONTF_NONFOLDERS, // | SHCONTF.SHCONTF_FOLDERS ,
            //        out ppenumIDList);

            //    if (ppenumIDList == null)       // if empty list
            //    {
            //        IntPtr iShellFolderPtr = IntPtr.Zero;
            //        var guidFolder2 = typeof(IShellFolder2).GUID;
            //        folderTmpPtr = IntPtr.Zero;

            //        if ((hr = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero,
            //            ref guidFolder2, // shellFolderGuid,
            //            out folderTmpPtr)) == WinError.S_OK)
            //        {
            //            folderTmp = (IShellFolder2)Marshal.GetObjectForIUnknown(folderTmpPtr);

            //            var hr2 = ShellObject.CreateViewObject(folderTmp, model.Form.Handle, ref guidFolder2, out iShellFolderPtr);
            //        }

            //        if (iShellFolderPtr != IntPtr.Zero)
            //        {
            //            var ShellFolder = (IShellFolder2)
            //                           Marshal.GetObjectForIUnknown(iShellFolderPtr);
            //        }
            //    }
            //}

            return(ret == WinError.S_OK ? WinError.S_OK : WinError.E_FAIL);
        }
Exemplo n.º 27
0
        int IShellBrowser.BrowseObject(IntPtr pidl, SBSP wFlags)
        {
            if (this.InvokeRequired)
            {
                AutoResetEvent theEvent = new AutoResetEvent(false);
                int            result   = WinError.E_FAIL;
                this.Invoke((Action)(() =>
                {
                    result = ((IShellBrowser)this).BrowseObject(pidl, wFlags);
                    theEvent.Set();
                }));
                theEvent.WaitOne();
                return(result);
            }

            int          hr;
            IntPtr       folderTmpPtr;
            IShellFolder folderTmp;
            IntPtr       pidlTmp;

            //  We'll need the shell folder GUID.
            var shellFolderGuid = typeof(IShellFolder).GUID;
            var shellViewGuid   = typeof(IShellView).GUID;

            //  Check to see if we have a desktop pidl, relative pidl or absolite pidl.
            if (Shell32.ILIsEqual(pidl, desktopFolderPidl))
            {
                //  The provided PIDL is the desktop folder.
                pidlTmp   = Shell32.ILClone(desktopFolderPidl);
                folderTmp = desktopFolder;
            }
            else if ((wFlags & SBSP.SBSP_RELATIVE) != 0)
            {
                // SBSP_RELATIVE - pidl is relative from the current folder
                if ((hr = currentFolder.BindToObject(pidl, IntPtr.Zero,
                                                     ref shellFolderGuid,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(hr);
                }
                pidlTmp   = Shell32.ILCombine(currentAbsolutePidl, pidl);
                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }
            else
            {
                // SBSP_ABSOLUTE - pidl is an absolute pidl (relative from desktop)
                pidlTmp = Shell32.ILClone(pidl);
                if ((hr = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero,
                                                     ref shellFolderGuid,
                                                     out folderTmpPtr)) != WinError.S_OK)
                {
                    return(hr);
                }
                folderTmp = (IShellFolder)Marshal.GetObjectForIUnknown(folderTmpPtr);
            }

            if (folderTmp == null)
            {
                Shell32.ILFree(pidlTmp);
                return(WinError.E_FAIL);
            }

            // Check that we have a new pidl
            if (Shell32.ILIsEqual(pidlTmp, currentAbsolutePidl))
            {
                Shell32.ILFree(pidlTmp);
                return(WinError.S_OK);
            }

            currentFolder = folderTmp;

            FOLDERSETTINGS fs             = new FOLDERSETTINGS();
            IShellView     lastIShellView = shellView;

            if (lastIShellView != null)
            {
                lastIShellView.GetCurrentInfo(ref fs);
            }
            // Copy the old folder settings
            else
            {
                fs          = new FOLDERSETTINGS();
                fs.fFlags   = folderFlags;
                fs.ViewMode = folderViewMode;
            }

            // Create the IShellView
            IntPtr iShellViewPtr;

            hr = folderTmp.CreateViewObject(Handle,
                                            ref shellViewGuid, out iShellViewPtr);
            if (hr == WinError.S_OK)
            {
                shellView = (IShellView)
                            Marshal.GetObjectForIUnknown(iShellViewPtr);

                hWndListView = IntPtr.Zero;
                RECT rc =
                    new RECT(0, 0,
                             ClientSize.Width,
                             ClientSize.Height);

                int res;

                try
                {
                    // Create the actual list view.
                    res = shellView.CreateViewWindow(lastIShellView, ref fs,
                                                     this, ref rc, ref hWndListView);
                }
                catch (COMException)
                {
                    return(WinError.E_FAIL);
                }

                if (res < 0)
                {
                    return(WinError.E_FAIL);
                }

                // Release the old IShellView
                if (lastIShellView != null)
                {
                    lastIShellView.GetCurrentInfo(ref fs);
                    lastIShellView.UIActivate(SVUIA_STATUS.SVUIA_DEACTIVATE);
                    lastIShellView.DestroyViewWindow();
                }

                // Set focus to the IShellView
                shellView.UIActivate(SVUIA_STATUS.SVUIA_ACTIVATE_FOCUS);
                currentAbsolutePidl = pidlTmp;
            }

            return(WinError.S_OK);
        }