Exemplo n.º 1
0
        /// <summary>
        /// Take a directory and return the IStorage PTr interface.
        /// </summary>
        internal static bool getIStorage(DirectoryInfoEx dir, out IntPtr storagePtr)
        {
            //0.19 Fixed ArgumentException when getting storage of C:\{User}\Desktop.
            if (dir.FullName.Equals(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)))
            {
                dir = DirectoryInfoEx.DesktopDirectory;
            }

            IntPtr storePtr = IntPtr.Zero;

            //0.15 : Fixed PIDL not freed correctly.
            try
            {
                return(dir.RequestRelativePIDL(dirPIDLRel =>
                {
                    int hr;
                    if (dirPIDLRel.Size == 0)
                    {
                        IntPtr pidlLast = IntPtr.Zero;
                        hr = ShellAPI.SHBindToParent(dirPIDLRel.Ptr, ShellAPI.IID_IStorage,
                                                     out storePtr, ref pidlLast);
                    }
                    else
                    {
                        //0.15: Fixed ShellFolder not freed correctly.
                        using (ShellFolder2 dirParentShellFolder = dir.Parent.ShellFolder)
                            if (dirParentShellFolder != null)
                            {
                                hr = dirParentShellFolder.BindToStorage(
                                    dirPIDLRel.Ptr, IntPtr.Zero, ref ShellAPI.IID_IStorage,
                                    out storePtr);
                            }
                            else
                            {
                                storePtr = IntPtr.Zero;
                                return false;
                            }
                    }

                    if ((hr != ShellAPI.S_OK))
                    {
                        storePtr = IntPtr.Zero;
                        Marshal.ThrowExceptionForHR(hr);
                        return false;
                    }

                    return true;
                }));
            }
            finally
            {
                storagePtr = storePtr;
            }
        }