private ShellFolder2 getIShellFolder() { if (this.FullName.Equals(IOTools.IID_Desktop)) { return(getDesktopShellFolder()); } else { ShellFolder2 retVal = getIShellFolderFromParent(); if (retVal != null) { return(retVal); } return(this.RequestPIDL((pidl, relPIDL) => { using (ShellFolder2 parentShellFolder = getParentIShellFolder(pidl, out relPIDL)) { IntPtr ptrShellFolder; int hr = parentShellFolder.BindToObject(relPIDL.Ptr, IntPtr.Zero, ref ShellAPI.IID_IShellFolder2, out ptrShellFolder); if (ptrShellFolder == IntPtr.Zero || hr != ShellAPI.S_OK) { Marshal.ThrowExceptionForHR(hr); } return new ShellFolder2(ptrShellFolder); } })); } }
//internal static string getParentParseName(PIDL pidl) //{ // PIDL relPIDL; // PIDL parentPIDL = getParentPIDL(pidl, out relPIDL); // IShellFolder sf = getParentIShellFolder(parentPIDL, out relPIDL); // if (relPIDL.Size == 0) // return IOTools.IID_Desktop; // return loadName(sf, relPIDL, ShellAPI.SHGNO.FORPARSING); //} internal static ShellFolder2 getParentIShellFolder(PIDL pidl, out PIDL relPIDL) { int hr; IntPtr ptrShellFolder = IntPtr.Zero; if (pidl.Size == 0 || PIDL.ILFindLastID(pidl.Ptr) == pidl.Ptr || //is root or parent is root PIDLToPath(pidl) == Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) { hr = ShellAPI.SHGetDesktopFolder(out ptrShellFolder); relPIDL = new PIDL(pidl, true); } else { PIDL parentPIDL = getParentPIDL(pidl, out relPIDL); //Console.WriteLine("ParentPIDL.Size = {0}", parentPIDL.Size); Guid guid = ShellAPI.IID_IShellFolder2; using (ShellFolder2 _desktopShellFolder = getDesktopShellFolder()) hr = _desktopShellFolder.BindToObject(parentPIDL.Ptr, IntPtr.Zero, ref guid, out ptrShellFolder); if (parentPIDL != null) { parentPIDL.Free(); } } if (hr == ShellAPI.S_OK && ptrShellFolder != IntPtr.Zero) { return(new ShellFolder2(ptrShellFolder)); } Marshal.ThrowExceptionForHR(hr); return(null); //mute error. }
private ShellFolder2 getIShellFolder() { if (this.FullName.Equals(IOTools.IID_Desktop)) { return(getDesktopShellFolder()); } else { ShellFolder2 retVal = getIShellFolderFromParent(); if (retVal != null) { return(retVal); } PIDL pidlLookup = PIDL; PIDL relPIDL = this.PIDLRel; try { using (ShellFolder2 parentShellFolder = getParentIShellFolder(pidlLookup, out relPIDL)) { IntPtr ptrShellFolder; int hr = parentShellFolder.BindToObject(relPIDL.Ptr, IntPtr.Zero, ref ShellAPI.IID_IShellFolder2, out ptrShellFolder); if (ptrShellFolder == IntPtr.Zero || hr != ShellAPI.S_OK) { Marshal.ThrowExceptionForHR(hr); } return(new ShellFolder2(ptrShellFolder)); } } finally { if (pidlLookup != null) { pidlLookup.Free(); } if (relPIDL != null) { relPIDL.Free(); } relPIDL = null; pidlLookup = null; } } }
private ShellFolder2 getIShellFolderFromParent() { if (_parent != null) { using (ShellFolder2 parentShellFolder = _parent.ShellFolder) { IntPtr ptrShellFolder; PIDL relPIDL = this.PIDLRel; int hr = parentShellFolder.BindToObject(relPIDL.Ptr, IntPtr.Zero, ref ShellAPI.IID_IShellFolder2, out ptrShellFolder); if (ptrShellFolder != IntPtr.Zero && hr == ShellAPI.S_OK) { return(new ShellFolder2(ptrShellFolder)); } } } return(null); }