Example #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);
        }
Example #2
0
        /// <summary>
        /// Initialises the ShellItem, from its PIDL and parent.
        /// </summary>
        /// <param name="pidl">The pidl.</param>
        /// <param name="parentFolder">The parent folder.</param>
        private void Initialise(IntPtr pidl, ShellItem parentFolder)
        {
            //  Set the parent item and relative pidl.
            ParentItem   = parentFolder;
            RelativePIDL = pidl;

            //  Create the fully qualified PIDL.
            PIDL = Shell32.ILCombine(parentFolder.PIDL, pidl);

            //  Use the desktop folder to get attributes.
            var flags = SFGAO.SFGAO_FOLDER | SFGAO.SFGAO_HASSUBFOLDER | SFGAO.SFGAO_BROWSABLE | SFGAO.SFGAO_FILESYSTEM;
            //todo was this parentFolder.ShellFolderInterface.GetAttributesOf(1, ref pidl, ref flags);

            var apidl = Marshal.AllocCoTaskMem(IntPtr.Size * 1);

            Marshal.Copy(new IntPtr[] { pidl }, 0, apidl, 1);

            parentFolder.ShellFolderInterface.GetAttributesOf(1, apidl, ref flags);

            IsFolder      = (flags & SFGAO.SFGAO_FOLDER) != 0;
            HasSubFolders = (flags & SFGAO.SFGAO_HASSUBFOLDER) != 0;

            //  Get the file info.
            var fileInfo = new SHFILEINFO();

            Shell32.SHGetFileInfo(PIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo),
                                  SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_PIDL | SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_ATTRIBUTES);

            //  Set extended attributes.
            DisplayName = fileInfo.szDisplayName;
            Attributes  = (SFGAO)fileInfo.dwAttributes;
            TypeName    = fileInfo.szTypeName;
            IconIndex   = fileInfo.iIcon;

            //  Are we a folder?
            if (IsFolder)
            {
                //  Bind the shell folder interface.
                IShellFolder shellFolderInterface;
                IntPtr       ppv    = IntPtr.Zero;
                var          result = parentFolder.ShellFolderInterface.BindToObject(pidl, IntPtr.Zero, ref Shell32.IID_IShellFolder,
                                                                                     out ppv);//out shellFolderInterface);
                shellFolderInterface = ((IShellFolder)Marshal.GetObjectForIUnknown(ppv));
                ShellFolderInterface = shellFolderInterface;

                //  Validate the result.
                if (result != 0)
                {
                    //  Throw the failure as an exception.
                    Marshal.ThrowExceptionForHR((int)result);
                }
            }
        }
Example #3
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);
        }
Example #4
0
        private static IShellItem CreateItemWithParent(ShellItem parent, IntPtr pidl)
        {
            if (RunningVista)
            {
                return(Shell32.SHCreateItemWithParent(IntPtr.Zero,
                                                      parent.GetIShellFolder(), pidl, typeof(IShellItem).GUID));
            }
            var impl =
                (ShellItemImpl)parent.ComInterface;

            return(new ShellItemImpl(
                       Shell32.ILCombine(impl.Pidl, pidl), true));
        }
Example #5
0
        public ShellItem(ShellItem parent, IntPtr pidl)
        {
            if (RunningVista)
            {
                ComInterface = Shell32.SHCreateItemWithParent(IntPtr.Zero, parent.GetIShellFolder(), pidl, typeof(IShellItem).GUID);
            }
            else
            {
                Interop.VistaBridge.ShellItemImpl impl = (Interop.VistaBridge.ShellItemImpl)parent.ComInterface;
                ComInterface = new Interop.VistaBridge.ShellItemImpl(Shell32.ILCombine(impl.Pidl, pidl), true);
            }

            Constructor_Helper();
        }
Example #6
0
        public List <Uri> GetResources(Uri uri)
        {
            var list = new List <Uri>();

            var item = GetItem(uri);

            IntPtr pidl = Shell32.SHGetIDListFromObject(item);

            try{
                var psf = Shell32.SHBindToObject <IShellFolder>(null, pidl, null);
                try{
                    IEnumIDList peidl = psf.EnumObjects(OwnerHwnd, EnumConst);

                    if (peidl == null)
                    {
                        return(list);
                    }
                    try{
                        while (true)
                        {
                            IntPtr pidl2;
                            int    num;
                            peidl.Next(1, out pidl2, out num);
                            if (num == 0)
                            {
                                break;
                            }
                            try{
                                IntPtr pidl3 = Shell32.ILCombine(pidl, pidl2);
                                list.Add(GetShellUri(pidl3, true));
                            }finally{
                                Marshal.FreeCoTaskMem(pidl2);
                            }
                        }
                    }finally{
                        Marshal.FinalReleaseComObject(peidl);
                    }
                }finally{
                    Marshal.FinalReleaseComObject(psf);
                }
            }finally{
                Marshal.FreeCoTaskMem(pidl);
            }

            return(list);
        }
Example #7
0
        /// <summary>
        /// Initialises the ShellItem, from its PIDL and parent.
        /// </summary>
        /// <param name="pidl">The pidl.</param>
        /// <param name="parentFolder">The parent folder.</param>
        internal void Initialise(IntPtr pidl, ShellItem parentFolder)
        {
            //  Set the parent item and relative pidl.
            ParentItem   = parentFolder;
            RelativePIDL = pidl;

            //  Create the fully qualified PIDL.
            PIDL = Shell32.ILCombine(parentFolder.PIDL, pidl);

            //  Use the desktop folder to get attributes.
            var flags = SFGAO.SFGAO_FOLDER | SFGAO.SFGAO_HASSUBFOLDER | SFGAO.SFGAO_BROWSABLE | SFGAO.SFGAO_FILESYSTEM;
            //todo was this parentFolder.ShellFolderInterface.GetAttributesOf(1, ref pidl, ref flags);

            var apidl = Marshal.AllocCoTaskMem(IntPtr.Size * 1);

            Marshal.Copy(new IntPtr[] { pidl }, 0, apidl, 1);

            parentFolder.ShellFolderInterface.GetAttributesOf(1, apidl, ref flags);

            IsFolder      = (flags & SFGAO.SFGAO_FOLDER) != 0;
            IsFileSystem  = (flags & SFGAO.SFGAO_FILESYSTEM) != 0;
            HasSubFolders = (flags & SFGAO.SFGAO_HASSUBFOLDER) != 0;

            //  Get the file info.
            var fileInfo = new SHFILEINFO();

            Shell32.SHGetFileInfo(PIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo),
                                  SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_PIDL
                                  | SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_TYPENAME
                                  | SHGFI.SHGFI_ATTRIBUTES);

            //  Set extended attributes.
            DisplayName = fileInfo.szDisplayName;
            Attributes  = (SFGAO)fileInfo.dwAttributes;
            TypeName    = fileInfo.szTypeName;
            IconIndex   = fileInfo.iIcon;

            //  Are we a folder?
            if (IsFolder)
            {
                InitFolder(pidl, this.Path, parentFolder.ShellFolderInterface);
            }
        }
Example #8
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 #9
0
            public override List <ResourceInfo> GetResources()
            {
                var list = new List <ResourceInfo>();

                var psf = Shell32.SHBindToObject <IShellFolder>(null, pidl, null);

                try{
                    IEnumIDList peidl = psf.EnumObjects(fs.OwnerHwnd, EnumConst);

                    if (peidl == null)
                    {
                        return(list);
                    }
                    try{
                        while (true)
                        {
                            IntPtr pidl2;
                            int    num;
                            peidl.Next(1, out pidl2, out num);
                            if (num == 0)
                            {
                                break;
                            }
                            try{
                                IntPtr pidl3 = Shell32.ILCombine(pidl, pidl2);
                                list.Add(new ShellFileHandle(pidl3, fs, true));
                            }finally{
                                Marshal.FreeCoTaskMem(pidl2);
                            }
                        }
                    }finally{
                        Marshal.FinalReleaseComObject(peidl);
                    }
                }finally{
                    Marshal.FinalReleaseComObject(psf);
                }

                return(list);
            }
        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);
        }
Example #11
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);
        }
    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);
    }