public void GetPCChildItems() { int iCnt = 0; foreach (var item in Browser.GetChildItems(KF_IID.ID_FOLDERID_ComputerFolder)) { // First item is null if we are looking at an item under the desktop item var fullIdList = (item.ParentIdList != null ? PidlManager.Combine(item.ParentIdList, item.ChildIdList) : item.ChildIdList); IKnownFolderProperties props = null; using (var kf = KnownFolderHelper.FromPIDL(fullIdList)) { if (kf != null) { props = KnownFolderHelper.GetFolderProperties(kf.Obj); Assert.IsTrue(props != null); } } Assert.IsTrue(item != null); iCnt++; } Assert.IsTrue(iCnt > 0); }
/// <summary> /// Gets a <see cref="KnownFolderNative"/> object from an <see cref="IdList"/> /// based PIDL if this represents a knownfolder, or otherwise, null. /// </summary> /// <param name="ashellListId"></param> /// <returns></returns> internal static KnownFolderNative FromPIDL(IdList ashellListId) { bool isDesktop = true; if (ashellListId != null) { if (ashellListId.Size > 0) { isDesktop = false; } } if (isDesktop == true) { return(KnownFolderHelper.FromPath(KF_IID.ID_FOLDERID_Desktop, true)); } IntPtr pidl = default(IntPtr); try { pidl = PidlManager.IdListToPidl(ashellListId); KnownFolderManagerClass knownFolderManager = new KnownFolderManagerClass(); IKnownFolderNative iknownFolder; HRESULT hr = knownFolderManager.FindFolderFromIDList(pidl, out iknownFolder); return((hr == HRESULT.S_OK) ? new KnownFolderNative(iknownFolder) : null); } finally { pidl = PidlManager.ILFree(pidl); } }
/// <summary> /// Gets the ResourceId (libararyName, index) of a shell icon /// based on an <see cref="IdList"/> object /// to support IconExtaction and display in UI layer. /// </summary> /// <param name="ilPidl"></param> /// <param name="isDirectory"></param> /// <param name="forceLoadFromDisk"></param> /// <param name="size"></param> /// <param name="iconState"></param> /// <returns></returns> public static string FromPidl(IdList ilPidl, bool isDirectory, bool forceLoadFromDisk, IconSize size = IconSize.large, ShellIconStateConstants iconState = ShellIconStateConstants.ShellIconStateNormal) { IntPtr ptrPidl = default(IntPtr); try { if ((ptrPidl = PidlManager.IdListToPidl(ilPidl)) != default(IntPtr)) { return(IconHelper.FromPidl(ptrPidl, isDirectory, forceLoadFromDisk, size, iconState)); } } finally { if (ptrPidl != default(IntPtr)) { ptrPidl = PidlManager.ILFree(ptrPidl); } } return(null); }
public void TestPCPath() { // Get test browser object and generate path list of idListPidls var testitem = Browser.Create(KF_IID.ID_FOLDERID_ComputerFolder); var pathItems = Browser.PathItemsAsIdList(testitem); // Should contain the fullpidl to 'This PC' Assert.IsTrue(pathItems.Count == 1); foreach (var item in pathItems) { string displayName = PidlManager.IdListFullToName(item, SHGDNF.SHGDN_NORMAL); string parseName = PidlManager.IdListFullToName(item, SHGDNF.SHGDN_FORPARSING); Console.WriteLine("Display Name '{0}' Parse Name '{1}'", displayName, parseName); Assert.IsFalse(string.IsNullOrEmpty(parseName)); Console.WriteLine(parseName); var browserItem = Browser.Create(parseName); Assert.IsTrue(browserItem != null); Assert.IsTrue(browserItem.EqualsParseName(parseName)); var browserItem1 = Browser.Create(item); Assert.IsTrue(browserItem1 != null); // Object from PIDL and ParseName should realy describe same location Assert.IsTrue(browserItem1.Equals(browserItem)); } }
/// <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); }
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); }
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); } } }
public void GetPidlForSpecialFolderPath() { string originalPath = null; IntPtr pidl = default(IntPtr), ptrPath = default(IntPtr); try { using (var kfn = KnownFolderHelper.FromKnownFolderGuid(new Guid(KF_ID.ID_FOLDERID_Windows))) { pidl = kfn.KnownFolderToPIDL(); kfn.Obj.GetPath(0, out ptrPath); Assert.IsTrue(ptrPath != default(IntPtr)); originalPath = Marshal.PtrToStringUni(ptrPath); } Assert.IsFalse(string.IsNullOrEmpty(originalPath)); string logicalPath = PidlManager.GetPathFromPIDL(pidl); string physStorePath = PidlManager.GetPathFromPIDL(pidl, TypOfPath.PhysicalStoragePath); Assert.IsTrue(string.Equals(originalPath, physStorePath, StringComparison.InvariantCultureIgnoreCase)); Assert.IsFalse(string.Equals(logicalPath, physStorePath)); ////Console.WriteLine("Path Retrieval via PIDL:'{0}' ->\nLogical Path: '{1}', Physical Path: '{2}'", //// originalPath, logicalPath, physStorePath); } finally { pidl = PidlManager.ILFree(pidl); ptrPath = PidlManager.FreeCoTaskMem(ptrPath); } }
public void GetPidlForDrivePath() { IntPtr pidl = default(IntPtr); try { // Get the default drive's path var drive = new DirectoryInfo(Environment.SystemDirectory).Root.Name; Assert.IsFalse(string.IsNullOrEmpty(drive)); pidl = PidlManager.GetPIDLFromPath(drive); Assert.IsFalse(pidl == default(IntPtr)); // Logical and physical path representation should be the same for drives string logicalPath = PidlManager.GetPathFromPIDL(pidl); string physStorePath = PidlManager.GetPathFromPIDL(pidl, TypOfPath.PhysicalStoragePath); Assert.IsTrue(string.Equals(drive, physStorePath, StringComparison.CurrentCulture)); Assert.IsTrue(string.Equals(logicalPath, physStorePath, StringComparison.CurrentCulture)); ////Console.WriteLine("Path Retrieval via PIDL: '{0}' -> Logical Path: '{1}', Physical Path: '{2}'", //// drive, logicalPath, physStorePath); } finally { pidl = PidlManager.ILFree(pidl); } }
/// <summary> /// Retrieves the physical path of a known folder as a string or null /// if the special folder has no physical representation in the file system. /// /// https://msdn.microsoft.com/en-us/library/windows/desktop/bb761762(v=vs.85).aspx /// </summary> public string GetPath() { if (Obj == null) { throw new System.ArgumentException("Native IKnownFolder cannot be null."); } IntPtr ptrPath = default(IntPtr); try { Obj.GetPath(0, out ptrPath); return(Marshal.PtrToStringUni(ptrPath)); } catch { // Some special folders may not have a file system path return(null); } finally { ptrPath = PidlManager.FreeCoTaskMem(ptrPath); } }
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); } } }
public void GetLogicalLocationForDesktopParentOf() { var testitem = KF_IID.ID_FOLDERID_ComputerFolder; IdList parentIdList = null, relativeChild = null; var retVal = PidlManager.GetParentIdListFromPath(testitem, out parentIdList, out relativeChild); Assert.IsTrue(relativeChild != null); Assert.IsTrue(relativeChild.Size == 1); Assert.IsTrue(retVal == true); Assert.IsFalse(parentIdList == null); Assert.IsTrue(parentIdList.Size == 0); // An empty list represents the desktop IntPtr parentPIDL = PidlManager.IdListToPidl(IdList.Create(parentIdList.Ids)); try { Assert.IsFalse(parentPIDL == default(IntPtr)); string path = PidlManager.GetPathFromPIDL(parentPIDL); Assert.IsFalse(string.IsNullOrEmpty(path)); Assert.IsFalse(testitem.Equals(path, StringComparison.InvariantCultureIgnoreCase)); // The expected parent of 'This PC' is the 'Desktop' Assert.IsTrue(path.Equals(KF_IID.ID_FOLDERID_Desktop, StringComparison.InvariantCultureIgnoreCase)); // this works for display name return PidlManager.GetPidlDisplayName(parentPIDL); } finally { parentPIDL = PidlManager.FreeCoTaskMem(parentPIDL); } }
public void Initialise(IntPtr pidl, string path) { this.PIDL = pidl; this.RelativePIDL = ILShell32.ILFindLastID(pidl); IntPtr folderPidl = PIDL; SHFILEINFO?info = PidlManager.GetShFileInfo(pidl); var dir = System.IO.Path.GetDirectoryName(path); if ((info.Value.Attributes & SFGAO.SFGAO_FOLDER) == 0) { // Item folder var handle = PidlManager.FromPath(dir); folderPidl = handle.pidl; SHFILEINFO?info2 = PidlManager.GetShFileInfo(folderPidl); if (!info2.HasValue) { return; // new DirectoryNotFoundException } ParentItem = new ShellItem(); ParentItem.PIDL = folderPidl; ParentItem.RelativePIDL = ILShell32.ILFindLastID(folderPidl); ParentItem.DisplayName = info2.Value.szDisplayName; ParentItem.Attributes = (SFGAO)info2.Value.Attributes; ParentItem.IsFolder = (Attributes & SFGAO.SFGAO_FOLDER) != 0; ParentItem.IsFileSystem = (Attributes & SFGAO.SFGAO_FILESYSTEM) != 0; } //IShellItem i = ILShell32.SHCreateItemFromParsingName(path, IntPtr.Zero, // typeof(IShellItem).GUID); int hr = 0; IShellFolder pShellFolderInterface = IFolder(folderPidl, desktopShellFolder.Value.ShellFolderInterface, ref hr); if (pShellFolderInterface == null) { throw new NullReferenceException("SheelObject ShellFolderInterface error"); } DisplayName = info.Value.szDisplayName; Attributes = (SFGAO)info.Value.Attributes; IsFolder = (Attributes & SFGAO.SFGAO_FOLDER) != 0; IsFileSystem = (Attributes & SFGAO.SFGAO_FILESYSTEM) != 0; if ((info.Value.Attributes & SFGAO.SFGAO_FOLDER) == 0) { this.ParentItem.InitFolder(folderPidl, dir, pShellFolderInterface, false); } else if (dir == null && path.EndsWith(@":\")) { this.InitFolder(PIDL, path, pShellFolderInterface, false); } else { this.InitFolder(PIDL, dir, pShellFolderInterface, false); } }
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); }
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)); }
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); }
public void GetStorageLocationForSpecialFolderParent() { string originalPath = null; IntPtr ptrPath = default(IntPtr); try { using (var kfn = KnownFolderHelper.FromKnownFolderGuid(new Guid(KF_ID.ID_FOLDERID_Windows))) { kfn.Obj.GetPath(0, out ptrPath); Assert.IsTrue(ptrPath != default(IntPtr)); originalPath = Marshal.PtrToStringUni(ptrPath); } Assert.IsFalse(string.IsNullOrEmpty(originalPath)); string testFolder = System.IO.Path.Combine(originalPath, "System32"); Assert.IsTrue(System.IO.Directory.Exists(testFolder)); IdList parentIdList = null, relativeChild = null; var retVal = PidlManager.GetParentIdListFromPath(testFolder, out parentIdList, out relativeChild); Assert.IsTrue(relativeChild != null); Assert.IsTrue(relativeChild.Size == 1); IntPtr parentPIDL = PidlManager.IdListToPidl(IdList.Create(parentIdList.Ids)); try { Assert.IsFalse(parentPIDL == default(IntPtr)); string path = PidlManager.GetPathFromPIDL(parentPIDL); Assert.IsTrue(retVal == true); Assert.IsFalse(parentIdList == null); // Expectation: Should display a path like 'C:\' or special folder path Assert.IsFalse(string.IsNullOrEmpty(testFolder)); Assert.IsFalse(testFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase)); // The expected parent of a drive is 'This PC' Assert.IsTrue(path.Equals(KF_IID.ID_FOLDERID_Windows, StringComparison.InvariantCultureIgnoreCase)); } finally { parentPIDL = PidlManager.FreeCoTaskMem(parentPIDL); } } finally { ptrPath = PidlManager.FreeCoTaskMem(ptrPath); } }
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"); }
private string LoadIconResourceId() { lock (resolvePropsLock) { if (_KnownFolderIsInitialized == false) { _KnownFolderIsInitialized = true; _KnownFolder = LoadKnownFolder(); } } if (KnownFolder == null) { return(null); } bool isKFIconResourceIdValid = false; if (KnownFolder != null) { isKFIconResourceIdValid = KnownFolder.IsIconResourceIdValid(); } if (isKFIconResourceIdValid == false) { IdList pidl = null; LoadPidls(); if (ChildIdList != null || ParentIdList != null) { pidl = PidlManager.CombineParentChild(ParentIdList, ChildIdList); } else { pidl = IdList.Create(); } string filename; int index; if (IconHelper.GetIconResourceId(pidl, out filename, out index)) { // Store filename and index for Desktop Root ResourceId return(string.Format("{0},{1}", filename, index)); } } else { return(KnownFolder.IconResourceId); } return(null); }
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)); }
/// <summary> /// Gets the ITEMIDLIST for the folder object. /// </summary> /// <param name="ppidl">The address of an ITEMIDLIST pointer. This PIDL represents the absolute location of the folder and must be relative to the desktop. This is typically a copy of the PIDL passed to Initialize.</param> /// <returns> /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// </returns> /// <remarks> /// If the folder object has not been initialized, this method returns S_FALSE and ppidl is set to NULL. /// </remarks> int IPersistFolder2.GetCurFolder(out IntPtr ppidl) { // Return null if we're not initialised. if (idListAbsolute == null) { ppidl = IntPtr.Zero; return(WinError.S_FALSE); } // Return the pidl. ppidl = PidlManager.IdListToPidl(idListAbsolute); return(WinError.S_OK); }
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); }
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()); }
public void GetLogicalLocationForParentOfDesktop() { var testitem = KF_IID.ID_FOLDERID_Desktop; IdList parentIdList = null, relativeChild = null; var retVal = PidlManager.GetParentIdListFromPath(testitem, out parentIdList, out relativeChild); Assert.IsTrue(relativeChild == null); Assert.IsTrue(retVal == true); // Desktop item is expected to have no parent Assert.IsTrue(parentIdList == null); }
public void GetLogicalLocationForThisPCParentOf() { // Get the default drive's path var drive = new DirectoryInfo(Environment.SystemDirectory).Root.Name; Assert.IsFalse(string.IsNullOrEmpty(drive)); var list = new List <string>(); list.Add(drive); list.Add(KF_IID.ID_FOLDERID_Documents); list.Add(KF_IID.ID_FOLDERID_Music); list.Add(KF_IID.ID_FOLDERID_Downloads); list.Add(KF_IID.ID_FOLDERID_Pictures); list.Add(KF_IID.ID_FOLDERID_Videos); foreach (var testFolder in list) { IdList parentIdList = null, relativeChild = null; var retVal = PidlManager.GetParentIdListFromPath(testFolder, out parentIdList, out relativeChild); Assert.IsTrue(relativeChild != null); Assert.IsTrue(relativeChild.Size == 1); Assert.IsTrue(retVal == true); Assert.IsFalse(parentIdList == null); IntPtr parentPIDL = PidlManager.IdListToPidl(IdList.Create(parentIdList.Ids)); try { Assert.IsFalse(parentPIDL == default(IntPtr)); string path = PidlManager.GetPathFromPIDL(parentPIDL); // Expectation: Should display a path like 'C:\' or special folder path Assert.IsFalse(string.IsNullOrEmpty(path)); Assert.IsFalse(testFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase)); // The expected parent of a drive is 'This PC' Assert.IsTrue(path.Equals(KF_IID.ID_FOLDERID_ComputerFolder, StringComparison.InvariantCultureIgnoreCase)); // this works for display name return PidlManager.GetPidlDisplayName(parentPIDL); } finally { parentPIDL = PidlManager.FreeCoTaskMem(parentPIDL); } } }
/// <summary> /// Retrieves the specified number of item identifiers in the /// enumeration sequence and advances the current position by /// the number of items retrieved. /// </summary> /// <param name="celt">Number of elements in the array pointed to by the rgelt parameter.</param> /// <param name="rgelt">Address of an array of ITEMIDLIST pointers that receives the item identifiers. The implementation must allocate these item identifiers /// using the Shell's allocator (retrieved by the SHGetMalloc function). The calling application is responsible for freeing the item /// identifiers using the Shell's allocator.</param> /// <param name="pceltFetched">Address of a value that receives a count of the item identifiers actually returned in rgelt. The count can be smaller than the value /// specified in the celt parameter. This parameter can be NULL only if celt is one.</param> /// <returns></returns> public int Next(uint celt, IntPtr rgelt, out uint pceltFetched) { // Request the children from the extension. As this is an abstract call, we always // use an exception handler. var items = new List <IShellNamespaceItem>(); try { // TODO: We may want to improve on the public api here so that we don't have to enumerate // everything to get certain items. // Enumerate the children, adding them to the items collection and moving the index forwards // by the number of items we've enumerated. items.AddRange( shellNamespaceFolder .GetChildren(_shellNamespaceEnumerationFlags) .Skip((int)currentIndex) .Take((int)celt)); currentIndex += (uint)items.Count; } catch (Exception exception) { // Log the exception, but continue as if we've enumerated nothing. Diagnostics.Logging.Error(string.Format("An unhandled exception occured enumerating {0} items from the {1} namespace extension.", celt, shellNamespaceFolder.GetDisplayName(DisplayNameContext.Normal)), exception); } // If we've not enumerated anything, we can return now. if (items.Any() == false && celt > 0) { pceltFetched = 0; return(WinError.S_FALSE); } // For every item enumerated, use the PIDL manager to create a shell allocated PIDL. // These PIDLs must not be relative, so using the value returned by the GetShellId // function is enough. var pidlArray = items.Select( iid => PidlManager.IdListToPidl(IdList.Create(new List <ShellId> { iid.GetShellId() }))).ToArray(); // Copy the data to the provided array. Marshal.Copy(pidlArray, 0, rgelt, pidlArray.Length); pceltFetched = (uint)items.Count; // We're done. We return OK if we've got more to enumerate and false otherwise. return(pceltFetched == celt ? WinError.S_OK : WinError.S_FALSE); }
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"); }
private void OnOpen(object sender, EventArgs eventArgs) { SHELLEXECUTEINFO sei = new SHELLEXECUTEINFO(); sei.cbSize = Marshal.SizeOf(sei); sei.fMask = SEE.SEE_MASK_IDLIST | SEE.SEE_MASK_CLASSNAME; var fullPidl = PidlManager.IdListToPidl(PidlManager.Combine(_folderIdList, _folderItemIdLists[0])); sei.lpIDList = fullPidl; sei.lpClass = "folder"; sei.hwnd = CurrentInvokeCommandInfo.WindowHandle; sei.nShow = CurrentInvokeCommandInfo.ShowCommand; sei.lpVerb = "open"; // todo parameter open. Shell32.ShellExecuteEx(ref sei); PidlManager.DeletePidl(fullPidl); }
/// <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); }
/// <summary> /// Translates the display name of a file object or a folder into an item identifier list. /// </summary> /// <param name="hwnd">A window handle. The client should provide a window handle if it displays a dialog or message box. Otherwise set hwnd to NULL.</param> /// <param name="pbc">Optional. A pointer to a bind context used to pass parameters as inputs and outputs to the parsing function.</param> /// <param name="pszDisplayName">A null-terminated Unicode string with the display name.</param> /// <param name="pchEaten">A pointer to a ULONG value that receives the number of characters of the display name that was parsed. If your application does not need this information, set pchEaten to NULL, and no value will be returned.</param> /// <param name="ppidl">When this method returns, contains a pointer to the PIDL for the object.</param> /// <param name="pdwAttributes">The value used to query for file attributes. If not used, it should be set to NULL.</param> /// <returns> /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// </returns> /// <exception cref="System.NotImplementedException"></exception> int IShellFolder.ParseDisplayName(IntPtr hwnd, IntPtr pbc, string pszDisplayName, ref uint pchEaten, out IntPtr ppidl, ref SFGAO pdwAttributes) { // First we can decode the pidl from the display name. var idList = IdList.FromParsingString(pszDisplayName); ppidl = PidlManager.IdListToPidl(idList); // We always eat the entire display string for SharpShell PIDL/DisplayName parsing. pchEaten = (uint)pszDisplayName.Length; // In theory, we should understand the pidl. var item = GetChildItem(idList); var name = item.GetDisplayName(DisplayNameContext.Normal); // TODO: We may be asked to get the attributes at the same time. If so, we must set them here. return(WinError.S_OK); }