Example #1
0
        public void CanBouncePidl()
        {
            IntPtr pidl = default(IntPtr);

            try
            {
                NativeMethods.SHGetKnownFolderIDList(KnownFolderGuids.FOLDERID_Documents,
                                                     KNOWN_FOLDER_FLAG.KF_NO_FLAGS,
                                                     IntPtr.Zero, out pidl);

                var idList = PidlManager.PidlToIdlist(pidl);  // Convert pidl to idlist

                NativeMethods.ILFree(pidl);
                pidl = default(IntPtr);

                pidl = PidlManager.IdListToPidl(idList);     // Convert idlist to pidl

                var displayName = PidlManager.GetPidlDisplayName(pidl);
                Assert.AreEqual(displayName, "Documents");
            }
            finally
            {
                if (pidl != default(IntPtr))
                {
                    NativeMethods.ILFree(pidl);
                }
            }
        }
Example #2
0
        public void CanFullRoundTripPidl()
        {
            IntPtr pidl  = default(IntPtr);
            IntPtr pidl2 = default(IntPtr);

            try
            {
                NativeMethods.SHGetKnownFolderIDList(KnownFolderGuids.FOLDERID_Downloads,
                                                     KNOWN_FOLDER_FLAG.KF_NO_FLAGS,
                                                     IntPtr.Zero, out pidl);

                var idList = PidlManager.PidlToIdlist(pidl);
                pidl2 = PidlManager.IdListToPidl(idList);
                var idList2 = PidlManager.PidlToIdlist(pidl2);

                Assert.IsTrue(idList.Matches(idList2));
            }
            finally
            {
                if (pidl != default(IntPtr))
                {
                    NativeMethods.ILFree(pidl);
                }

                if (pidl2 != default(IntPtr))
                {
                    NativeMethods.ILFree(pidl2);
                }
            }
        }
        /// <summary>
        /// Retrieves an IShellFolder object for a subfolder.
        /// Return value: error code, if any
        /// </summary>
        /// <param name="pidl">Address of an ITEMIDLIST structure (PIDL) that identifies the subfolder.</param>
        /// <param name="pbc">Optional address of an IBindCtx interface on a bind context object to be used during this operation.</param>
        /// <param name="riid">Identifier of the interface to return. </param>
        /// <param name="ppv">Address that receives the interface pointer.</param>
        /// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
        int IShellFolder.BindToObject(IntPtr pidl, IntPtr pbc, ref Guid riid, out IntPtr ppv)
        {
            //  Have we been asked to bind to a folder?
            if (riid == typeof(IShellFolder).GUID || riid == typeof(IShellFolder2).GUID)
            {
                //  Get the child item.
                var idList    = PidlManager.PidlToIdlist(pidl);
                var childItem = GetChildItem(idList);

                //  If the item is a folder, we can create a proxy for it and return the proxy.
                var subFolder = childItem as IShellNamespaceFolder;
                if (subFolder != null)
                {
                    var folderProxy = new ShellFolderImpl(namespaceExtension, subFolder);
                    ppv = Marshal.GetComInterfaceForObject(folderProxy, typeof(IShellFolder2));
                    return(WinError.S_OK);
                }
            }

            //  Note: We are also asked to bind to IPropertyStore IPropertyStoreFactory and IPropertyStoreCache.

            //  If we cannot return the required interface, we must return no interface.
            ppv = IntPtr.Zero;
            return(WinError.E_NOINTERFACE);
        }
Example #4
0
        public void CanIdentifyIdListLength()
        {
            IntPtr pidl;

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

            Assert.That(idList.Ids.Count, Is.GreaterThan(1));
        }
Example #5
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);
        }
Example #6
0
        public void CanFullRoundTripPidl()
        {
            IntPtr pidl;

            Shell32.SHGetKnownFolderIDList(KnownFolders.FOLDERID_Downloads, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero,
                                           out pidl);
            var idList  = PidlManager.PidlToIdlist(pidl);
            var pidl2   = PidlManager.IdListToPidl(idList);
            var idList2 = PidlManager.PidlToIdlist(pidl2);

            Assert.IsTrue(idList.Matches(idList2));
        }
        private string GetItemColumnValue(IntPtr pidl, PROPERTYKEY propertyKey)
        {
            //  Get the value for the property key.
            var item   = GetChildItem(PidlManager.PidlToIdlist(pidl));
            var column = ((DefaultNamespaceFolderView)lazyFolderView.Value).Columns.FirstOrDefault(c =>
            {
                var key = c.PropertyKey.CreateShellPropertyKey();
                return(key.fmtid == propertyKey.fmtid && key.pid == propertyKey.pid);
            });
            var detail = ((DefaultNamespaceFolderView)lazyFolderView.Value).GetItemDetail(item, column);

            return(detail.ToString());
        }
Example #8
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");
        }
        /// <summary>
        /// Retrieves the display name for the specified file object or subfolder.
        /// Return value: error code, if any
        /// </summary>
        /// <param name="pidl">Address of an ITEMIDLIST structure (PIDL)  that uniquely identifies the file  object or subfolder relative to the parent  folder.</param>
        /// <param name="uFlags">Flags used to request the type of display name to return. For a list of possible values.</param>
        /// <param name="pName">Address of a STRRET structure in which to return the display name.</param>
        /// <returns>
        /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
        /// </returns>
        int IShellFolder.GetDisplayNameOf(IntPtr pidl, SHGDNF uFlags, out STRRET pName)
        {
            //  If we have an invalid PIDL, we must fail.
            if (pidl == IntPtr.Zero)
            {
                pName = new STRRET();
                return(WinError.E_INVALIDARG);
            }

            //  Create an idlist from the pidl.
            var idlist = PidlManager.PidlToIdlist(pidl);

            //  Get the shell item.
            //  TODO; handle errors
            var shellItem = GetChildItem(idlist);

            //  If the flags are normal only, we're asking for the display name only.
            if (uFlags == SHGDNF.SHGDN_NORMAL)
            {
                //  We only need the display name.
                pName = STRRET.CreateUnicode(shellItem.GetDisplayName(DisplayNameContext.OutOfFolder));
                return(WinError.S_OK);
            }

            //  If the flags are in folder, we're asking for the standard display name.
            if (uFlags == SHGDNF.SHGDN_INFOLDER || uFlags == SHGDNF.SHGDN_FORADDRESSBAR)
            {
                pName = STRRET.CreateUnicode(shellItem.GetDisplayName(DisplayNameContext.Normal));
                return(WinError.S_OK);
            }

            //  If the flags indicate parsing mode, we need to construct a name
            //  that'll let us bounce from PIDL <-> name. We do this, rather than
            //  the implementor.
            if (uFlags.HasFlag(SHGDNF.SHGDN_FORPARSING))
            {
                //  It's either relative (INFOLDER) or fully qualified.
                var str = uFlags.HasFlag(SHGDNF.SHGDN_INFOLDER)
                    ? idlist.ToParsingString()
                    : /* TODO start with my id list */ idlist.ToParsingString();
                pName = STRRET.CreateUnicode(str);
                return(WinError.S_OK);
            }

            pName = STRRET.CreateUnicode(string.Empty);
            return(WinError.S_OK);
        }
Example #10
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);
        }
Example #11
0
        public void CanDecodeFilesystemPidl()
        {
            IntPtr pidl = default(IntPtr);

            try
            {
                NativeMethods.SHGetKnownFolderIDList(KnownFolderGuids.FOLDERID_Documents,
                                                     KNOWN_FOLDER_FLAG.KF_NO_FLAGS,
                                                     IntPtr.Zero, out pidl);

                var idList = PidlManager.PidlToIdlist(pidl);  // Convert PIDL to IdList
                Assert.IsTrue(idList != null);
            }
            finally
            {
                if (pidl != default(IntPtr))
                {
                    NativeMethods.ILFree(pidl);
                }
            }
        }
Example #12
0
        public void CanIdentifyIdListLength()
        {
            IntPtr pidl = default(IntPtr);

            try
            {
                NativeMethods.SHGetKnownFolderIDList(KnownFolderGuids.FOLDERID_Downloads,
                                                     KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero, out pidl);

                var idList = PidlManager.PidlToIdlist(pidl);

                Assert.IsTrue(idList.Ids.Count > 1);
                //Assert.That(idList.Ids.Count, Is.GreaterThan(1));
            }
            finally
            {
                if (pidl != default(IntPtr))
                {
                    NativeMethods.ILFree(pidl);
                }
            }
        }
Example #13
0
        public static void OpenSystemMenu(ShellItem itemHit, Control ctrl, int x, int y)
        {
            //  The shell folder we use to get the UI object is either the folder itself if the
            //  item is a folder, or the parent folder otherwise.
            var shellFolder = itemHit.IsFolder ? itemHit.ShellFolderInterface : itemHit.ParentItem.ShellFolderInterface;

            //  The item pidl is either the folder if the item is a folder, or the combined pidl otherwise.
            var fullIdList = itemHit.IsFolder
                ? PidlManager.PidlToIdlist(itemHit.PIDL)
                : PidlManager.Combine(
                PidlManager.PidlToIdlist(itemHit.ParentItem.PIDL),
                PidlManager.PidlToIdlist(itemHit.RelativePIDL));


            //  Get the UI object of the context menu.
            IntPtr[] apidl = new IntPtr[] { PidlManager.IdListToPidl(fullIdList) };
            // PidlManager.PidlsToAPidl(new IntPtr[] { PidlManager.IdListToPidl(fullIdList) });

            IntPtr ppv = IntPtr.Zero;

            shellFolder.GetUIObjectOf(ctrl.Handle, 1,
                                      apidl,
                                      Shell32.IID_IContextMenu, 0,
                                      out ppv);

            //  If we have an item, cast it.
            if (ppv != IntPtr.Zero)
            {
                // desktop menu
                var contextMenu = Marshal.GetObjectForIUnknown(ppv) as IContextMenu; // IContextMenuSharp

                var popupMenu = new ContextMenu();
                contextMenu.QueryContextMenu(popupMenu.Handle, 0, 0, 65525, CMF.CMF_EXPLORE);
                popupMenu.Show(ctrl, new Point(x, y));
                return;
            }
        }
Example #14
0
        private void OpenItemContextMenu(ShellItem itemHit, int x, int y)
        {
            //  TODO: we need a min and max for the menu items.

            //  see http://www.codeproject.com/Articles/4025/Use-Shell-ContextMenu-in-your-applications for more

            //  The shell folder we use to get the UI object is either the folder itself if the
            //  item is a folder, or the parent folder otherwise.
            var shellFolder = itemHit.IsFolder ? itemHit.ShellFolderInterface : itemHit.ParentItem.ShellFolderInterface;

            //  The item pidl is either the folder if the item is a folder, or the combined pidl otherwise.
            var fullIdList = itemHit.IsFolder
                ? PidlManager.PidlToIdlist(itemHit.PIDL)
                : PidlManager.Combine(PidlManager.PidlToIdlist(itemHit.ParentItem.PIDL),
                                      PidlManager.PidlToIdlist(itemHit.RelativePIDL));


            //  Get the UI object of the context menu.
            IntPtr apidl = PidlManager.PidlsToAPidl(new IntPtr[] { PidlManager.IdListToPidl(fullIdList) });


            IntPtr ppv = IntPtr.Zero;

            shellFolder.GetUIObjectOf(Handle, 1, apidl, Shell32.IID_IContextMenu, 0,
                                      out ppv);

            //  If we have an item, cast it.
            if (ppv != IntPtr.Zero)
            {
                IContextMenu contextMenu = (IContextMenu)Marshal.GetObjectForIUnknown(ppv);

                var popupMenu = new ContextMenu();
                contextMenu.QueryContextMenu(popupMenu.Handle, 0, 0, 65525, CMF.CMF_EXPLORE);
                popupMenu.Show(this, new Point(x, y));
            }
        }
 int IPersistFolder.Initialize(IntPtr pidl)
 {
     //  Store the folder absolute pidl.
     idListAbsolute = PidlManager.PidlToIdlist(pidl);
     return(WinError.S_OK);
 }