/// <summary> /// Initializes a new instance of the <see cref="ShellItem"/> class. /// </summary> /// /// <remarks> /// Creates a ShellItem which is a named child of <paramref name="parent"/>. /// </remarks> /// /// <param name="parent"> /// The parent folder of the item. /// </param> /// /// <param name="name"> /// The name of the child item. /// </param> public ShellItem(ShellItem parent, string name) { if (parent.IsFileSystem) { // If the parent folder is in the file system, our best // chance of success is to use the FileSystemPath to // create the new item. Folders other than Desktop don't // seem to implement ParseDisplayName properly. m_ComInterface = CreateItemFromParsingName( Path.Combine(parent.FileSystemPath, name)); } else { IShellFolder folder = parent.GetIShellFolder(); uint eaten; IntPtr pidl; uint attributes = 0; folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, name, out eaten, out pidl, ref attributes); try { m_ComInterface = CreateItemFromIDList(pidl); } finally { Shell32.ILFree(pidl); } } }
private IntPtr pathToRelPidl(string path) { IntPtr pidl; uint pchEaten = 0; ShellFolders.SFGAO pdwAttributes = 0; string file = Path.GetFileName(path); if (parentShellFolder != null) { int result = parentShellFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, file, ref pchEaten, out pidl, ref pdwAttributes); if (pidl == IntPtr.Zero) { CairoLogger.Instance.Debug("HRESULT " + result + " retrieving pidl for " + path); } return(pidl); } else { CairoLogger.Instance.Debug("Parent IShellFolder for " + path + " is null"); return(IntPtr.Zero); } }
public void GetContextMenus(string path) { IntPtr desktopPtr; IShellFolder desktop = ShellAPI.GetDesktopFolder(out desktopPtr); IntPtr ownerHwnd = IntPtr.Zero; IShellFolder Root; string FolderPath = Directory.GetParent(path).FullName; IntPtr Pidl = IntPtr.Zero; IShellFolder parent; uint i, j = 0; desktop.ParseDisplayName(ownerHwnd, IntPtr.Zero, FolderPath, out i, out Pidl, ref j); desktop.BindToObject(Pidl, IntPtr.Zero, ref Guids.IID_IShellFolder, out Root); Marshal.ReleaseComObject(desktop); IEnumIDList fileEnum = null; IEnumIDList folderEnum = null; IntPtr fileEnumPtr = IntPtr.Zero; IntPtr folderEnumPtr = IntPtr.Zero; IntPtr pidlSub; int celtFetched; if (Root.EnumObjects(ownerHwnd, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN, out fileEnumPtr) == ShellAPI.S_OK) { fileEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(fileEnumPtr); while (fileEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE) { string name = ShellAPI.GetNameByPIDL(pidlSub); } } if (Root.EnumObjects(ownerHwnd, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out folderEnumPtr) == ShellAPI.S_OK) { folderEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(folderEnumPtr); while (folderEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE) { string name = ShellAPI.GetNameByPIDL(pidlSub); if (Path.GetFileName(path) == name) { IntPtr PIDL = pidlSub; IShellFolder IParent = Root; IntPtr[] pidls = new IntPtr[1]; pidls[0] = PIDL; //get IContextMenu interface IntPtr iContextMenuPtr = IntPtr.Zero; iContextMenuPtr = IParent.GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length, pidls, ref Guids.IID_IContextMenu, out iContextMenuPtr); IContextMenu iContextMenu = (IContextMenu)Marshal.GetObjectForIUnknown(iContextMenuPtr); IntPtr contextMenu = ShellAPI.CreatePopupMenu(); iContextMenu.QueryContextMenu(contextMenu, 0, ShellAPI.CMD_FIRST, ShellAPI.CMD_LAST, CMF.NORMAL | CMF.EXPLORE); ParseMenu(contextMenu); } } } Marshal.ReleaseComObject(Root); }
/// <summary> /// Get the PIDLs /// </summary> /// <param name="arrayOfFileInfo">Array of FileInfo</param> /// <returns>Array of PIDLs</returns> protected IntPtr[] GetPIDL(FileInfo[] arrayOfFileInfo) { if (null == arrayOfFileInfo || 0 == arrayOfFileInfo.Length) { return(null); } IShellFolder oParentFolder = GetParentFolder(arrayOfFileInfo[0].DirectoryName); if (null == oParentFolder) { return(null); } var arrPIDL = new IntPtr[arrayOfFileInfo.Length]; int n = 0; foreach (FileInfo fi in arrayOfFileInfo) { // Get the file relative to folder uint pchEaten = 0; ShellAPI.SFGAO pdwAttributes = 0; IntPtr pPIDL; int nResult = oParentFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, fi.Name, ref pchEaten, out pPIDL, ref pdwAttributes); if (S_OK != nResult) { FreePIDL(arrPIDL); return(null); } arrPIDL[n] = pPIDL; n++; } return(arrPIDL); }
/// <summary> /// Get the PIDLs /// </summary> /// <param name="arrFI">Array of FileInfo</param> /// <returns>Array of PIDLs</returns> /// protected IntPtr[] GetPIDLs(FileInfo[] arrFI) { if (null == arrFI || 0 == arrFI.Length) { return(null); } IntPtr[] arrPIDLs = new IntPtr[arrFI.Length]; IShellFolder oParentFolder = GetParentFolder(arrFI[0].DirectoryName); if (null == oParentFolder) { return(null); } int n = 0; foreach (FileInfo fi in arrFI) { // Get the file relative to folder uint pchEaten = 0; SFGAO pdwAttributes = 0; IntPtr pPIDL = IntPtr.Zero; int nResult = oParentFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, fi.Name.Length == 0 ? fi.FullName : fi.Name, ref pchEaten, out pPIDL, ref pdwAttributes); if (S_OK != nResult) { FreePIDLs(arrPIDLs); return(null); } arrPIDLs[n] = pPIDL; n++; } return(arrPIDLs); }
private static IntPtr GetShellFolderChildrenRelativePidl(IShellFolder parentFolder, string displayName) { uint pdwAttributes = 0; parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out _, out var ppidl, ref pdwAttributes); return(ppidl); }
private static IntPtr GetShellFolderChildrenRelativePidl(IShellFolder parentFolder, string displayName) { Marshal.ThrowExceptionForHR(NativeMethods.CreateBindCtx(0, out _)); uint pdwAttributes = 0; parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out _, out var ppidl, ref pdwAttributes); return(ppidl); }
public static IntPtr GetPIDLByPath(IShellFolder deskfolder, string path) { IntPtr Pidl = IntPtr.Zero; uint i, j = 0; deskfolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, path, out i, out Pidl, ref j); return(Pidl); }
private static IntPtr GetShellFolderChildrenRelativePIDL(IShellFolder parentFolder, string displayName) { uint num; IntPtr ptr; NativeMethods.CreateBindCtx(); parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out num, out ptr, 0); return(ptr); }
static IntPtr GetShellFolderChildrenRelativePIDL(IShellFolder parentFolder, string displayName) { uint pchEaten; uint pdwAttributes = 0; IntPtr ppidl; parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out pchEaten, out ppidl, ref pdwAttributes); return(ppidl); }
/// <summary> 根据路径获取 IShellFolder 和 PIDL </summary> public static IShellFolder GetShellFolder(IShellFolder desktop, string path, out IntPtr Pidl) { IShellFolder IFolder; uint i, j = 0; desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, path, out i, out Pidl, ref j); desktop.BindToObject(Pidl, IntPtr.Zero, ref Guids.IID_IShellFolder, out IFolder); return(IFolder); }
public static IntPtr GetShellFolderChildrenPIDL(IShellFolder parentFolder, string displayName) { var bindCtx = PtrCreateBindCtx(); uint pchEaten = 0; SFGAO pdwAttributes = SFGAO.NONE; IntPtr ppidl; parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, ref pchEaten, out ppidl, ref pdwAttributes); return(ppidl); }
private static IntPtr GetShellFolderChildrenRelativePIDL(IntPtr hwnd, IShellFolder parentFolder, string displayName) { IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); uint pchEaten; uint pdwAttributes = 0; IntPtr ppidl; parentFolder.ParseDisplayName(hwnd, bindCtx, displayName, out pchEaten, out ppidl, ref pdwAttributes); return(ppidl); }
/// <summary> /// Gets the parent folder /// </summary> /// <param name="folderName">Folder path</param> /// <returns>IShellFolder for the folder (relative from the desktop)</returns> private IShellFolder GetParentFolder(string folderName) { if (null == _oParentFolder) { IShellFolder oDesktopFolder = GetDesktopFolder(); if (null == oDesktopFolder) { return(null); } // Get the PIDL for the folder file is in IntPtr pPIDL = IntPtr.Zero; uint pchEaten = 0; SFGAO pdwAttributes = 0; if (folderName == null) { folderName = "::{" + IID_MyComputer.ToString() + "}"; } int nResult = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref pchEaten, out pPIDL, ref pdwAttributes); if (S_OK != nResult) { return(null); } IntPtr pStrRet = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4); Marshal.WriteInt32(pStrRet, 0, 0); nResult = _oDesktopFolder.GetDisplayNameOf(pPIDL, SHGNO.FORPARSING, pStrRet); StringBuilder strFolder = new StringBuilder(MAX_PATH); StrRetToBuf(pStrRet, pPIDL, strFolder, MAX_PATH); Marshal.FreeCoTaskMem(pStrRet); pStrRet = IntPtr.Zero; _strParentFolder = strFolder.ToString(); // Get the IShellFolder for folder IntPtr pUnknownParentFolder = IntPtr.Zero; nResult = oDesktopFolder.BindToObject(pPIDL, IntPtr.Zero, ref IID_IShellFolder, out pUnknownParentFolder); // Free the PIDL first Marshal.FreeCoTaskMem(pPIDL); if (S_OK != nResult) { return(null); } _oParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnknownParentFolder, typeof(IShellFolder)); } return(_oParentFolder); }
private IShellFolder GetParentFolder(string path) { if (null == _ParentFolder) { IShellFolder oDesktopFolder = GetDesktopFolder(); if (null == oDesktopFolder) { return(null); } // Get the PIDL for the folder file is in IntPtr PIDL = IntPtr.Zero; uint eaten = 0; SFGAO attributes = 0; int result = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, path, ref eaten, out PIDL, ref attributes); if (S_OK != result) { return(null); } IntPtr ret = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4); Marshal.WriteInt32(ret, 0, 0); result = _DesktopFolder.GetDisplayNameOf(PIDL, SHGNO.FORPARSING, ret); StringBuilder folder = new StringBuilder(MAX_PATH); StrRetToBuf(ret, PIDL, folder, MAX_PATH); Marshal.FreeCoTaskMem(ret); ret = IntPtr.Zero; _ParentFolderPath = folder.ToString(); // Get the IShellFolder for folder IntPtr unknownParentFolder = IntPtr.Zero; result = oDesktopFolder.BindToObject(PIDL, IntPtr.Zero, ref IID_IShellFolder, out unknownParentFolder); // Free the PIDL first Marshal.FreeCoTaskMem(PIDL); if (S_OK != result) { return(null); } _ParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(unknownParentFolder, typeof(IShellFolder)); } return(_ParentFolder); }
//========================================================== //renvoie un IShellFolder correspondant au dossier parent de szFileName //========================================================== //IN szFileName : nom de fichier complet //OUT pidl : pointeur vers un ITEMLIST contenant le nom de fichier de szFilename //renvoie une instance de IShellFolder //========================================================== private IShellFolder GetShellFolder(string szFileName, ref IntPtr pidl) { IShellFolder folder = null; IShellFolder item = null; string szFile; string szPath; int cEaten = 0; IntPtr abspidl; // Initialize IShellFolder guid Guid uuidIShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); // Retrieve the desktop folder SHGetDesktopFolder(out folder); //si c'est un lecteur seul, on le base sur "My Computer" (enfin, son guid) if (szFileName.Length == 3) { szFileName = Path.GetFullPath(szFileName); szPath = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"; szFile = szFileName; //sinon, on sépare dossier parent et nom de fichier } else { szPath = Path.GetDirectoryName(szFileName); szFile = Path.GetFileName(szFileName); } // Parse the parent folder name folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, szPath, out cEaten, out pidl, IntPtr.Zero); //on crée un objet séparé (du bureau) pour le dossier parent item = folder.BindToObject(pidl, IntPtr.Zero, ref uuidIShellFolder); ILFree(pidl); // Calculate the ITEMLIST of the file's folder (without the parent folder name) abspidl = ILCreateFromPath(szFileName); pidl = ILFindLastID(abspidl); pidl = ILClone(pidl); ILFree(abspidl); return(item); }
/// <summary> /// Get PIDL and parent shellfolder for given file path /// </summary> /// <param name="filePath"></param> /// <returns></returns> private static PIDLShellFolder GetPIDLAndParentIshellFolderForFile(string filePath) { //get desktopPtr first IntPtr desktopPtr; IShellFolder desktop = GetDesktopFolder(out desktopPtr); string fileName = Path.GetFileName(filePath); IShellFolder parentShellFolder; string FolderPath = Directory.GetParent(filePath).FullName; IntPtr Pidl = IntPtr.Zero; uint i, j = 0; desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, FolderPath, out i, out Pidl, ref j); desktop.BindToObject(Pidl, IntPtr.Zero, ref Guids.IID_IShellFolder, out parentShellFolder); Marshal.ReleaseComObject(desktop); IEnumIDList fileEnum = null; IEnumIDList folderEnum = null; IntPtr fileEnumPtr = IntPtr.Zero; IntPtr folderEnumPtr = IntPtr.Zero; IntPtr pidlSub; int celtFetched; if (parentShellFolder.EnumObjects(IntPtr.Zero, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out folderEnumPtr) == ShellAPI.S_OK) { folderEnum = (IEnumIDList)Marshal.GetObjectForIUnknown(folderEnumPtr); while (folderEnum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == ShellAPI.S_FALSE) { string name = ShellAPI.GetNameByPIDL(pidlSub); if (name == fileName) { PIDLShellFolder ps = new PIDLShellFolder { PIDL = pidlSub, ShellFolder = parentShellFolder }; Marshal.ReleaseComObject(parentShellFolder); return(ps); } } } Marshal.ReleaseComObject(parentShellFolder); return(null); }
private void GenerateIExtractImage(string path) { try { // we get the desktop shell then the PIDL for the file's folder. // Once we have the PIDL then get a reference to the folder (BindToObject) then we can get the PIDL for the file. //Now get the IExtractImage interface (GETUIObjectOf) from which we can cast to the IExtractImage object SHGetDesktopFolder(out _desktopFolder); int pdwAttributes = 0; // not required int pchEaten = 0; // not required _hResult = _desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetDirectoryName(path), out pchEaten, out _folderPidl, out pdwAttributes); if (_hResult != S_OK) { Marshal.ThrowExceptionForHR(_hResult); } _hResult = _desktopFolder.BindToObject(_folderPidl, IntPtr.Zero, ref _iidShellFolder, ref _folder); if (_hResult != S_OK) { Marshal.ThrowExceptionForHR(_hResult); } _hResult = _folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(path), out pchEaten, out _filePidl, out pdwAttributes); if (_hResult != S_OK) { Marshal.ThrowExceptionForHR(_hResult); } IUnknown unk = null; _hResult = _folder.GetUIObjectOf(IntPtr.Zero, 1, ref _filePidl, ref _iidExtractImage, out _reserved, ref unk); if (_hResult != S_OK) { Marshal.ThrowExceptionForHR(_hResult); } // Now cast the unknown as the extractImage object _extractImage = (IExtractImage)unk; } catch (FileNotFoundException) { } catch (COMException) { } catch (Exception) { } }
/// <summary> /// Gets the parent folder. /// </summary> /// <param name="folderName">Folder path.</param> /// <returns>IShellFolder for the folder (relative from the desktop).</returns> private IShellFolder GetParentFolder(string folderName) { if (oParentFolder == null) { IShellFolder oDesktopFolder = GetDesktopFolder(); if (oDesktopFolder == null) { return(null); } // Get the PIDL for the folder file is in uint pchEaten = 0; SFGAO pdwAttributes = 0; int nResult = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref pchEaten, out IntPtr pPIDL, ref pdwAttributes); if (nResult != ResultOK) { return(null); } IntPtr pStrRet = Marshal.AllocCoTaskMem((MaxPath * 2) + 4); Marshal.WriteInt32(pStrRet, 0, 0); _ = this.oDesktopFolder.GetDisplayNameOf(pPIDL, SHGNO.FORPARSING, pStrRet); StringBuilder strFolder = new StringBuilder(MaxPath); _ = DllImports.NativeMethods.ShlwapiStrRetToBuf(pStrRet, pPIDL, strFolder, MaxPath); Marshal.FreeCoTaskMem(pStrRet); strParentFolder = strFolder.ToString(); // Get the IShellFolder for folder nResult = oDesktopFolder.BindToObject(pPIDL, IntPtr.Zero, ref iidIShellFolder, out IntPtr pUnknownParentFolder); // Free the PIDL first Marshal.FreeCoTaskMem(pPIDL); if (nResult != ResultOK) { return(null); } oParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnknownParentFolder, typeof(IShellFolder)); } return(oParentFolder); }
static IShellItem CreateItemFromParsingName(string path) { if (RunningVista) { return(Shell32.SHCreateItemFromParsingName(path, IntPtr.Zero, typeof(IShellItem).GUID)); } else { IShellFolder desktop = Desktop.GetIShellFolder(); uint attributes = 0; uint eaten; IntPtr pidl; desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, path, out eaten, out pidl, ref attributes); return(new Interop.VistaBridge.ShellItemImpl( pidl, true)); } }
private IUnknown GetUIObject(IShellFolder shellFolder, string filename, Guid uuid) { IUnknown iunk = null; if (System.IO.File.Exists(filename)) { IntPtr pidl = IntPtr.Zero; int cParsed = 0; int pdwAttrib = 0; shellFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(filename), out cParsed, out pidl, out pdwAttrib); if (pidl != IntPtr.Zero) { int prgf = 0; shellFolder.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref uuid, out prgf, ref iunk); } } else { shellFolder.CreateViewObject(IntPtr.Zero, ref uuid, ref iunk); } return(iunk); }
/// <summary>Gets the <see cref="ShellItem"/> with the specified child name.</summary> /// <value>The <see cref="ShellItem"/> instance matching <paramref name="childName"/>.</value> /// <param name="childName">Name of the child item.</param> /// <returns>The <see cref="ShellItem"/> instance matching <paramref name="childName"/>, if it exists.</returns> public ShellItem this[string childName] { get { if (string.IsNullOrEmpty(childName)) { throw new ArgumentNullException(nameof(childName)); } object ppv; if (IsMinVista) { SHCreateItemFromRelativeName(iShellItem, childName, BindContext, typeof(IShellItem).GUID, out ppv).ThrowIfFailed(); } else { SFGAO attr = 0; iShellFolder.ParseDisplayName(IntPtr.Zero, null, childName, out uint _, out var tempPidl, ref attr); ppv = new ShellItemImpl(PIDL.Combine(PIDL, tempPidl), false); } return(Open((IShellItem)ppv)); } }
/// <summary> /// Get the PIDLs. /// </summary> /// <param name="arrFI">Array of DirectoryInfo.</param> /// <returns>Array of PIDLs.</returns> protected IntPtr[] GetPIDLs(DirectoryInfo[] arrFI) { if (arrFI == null || arrFI.Length == 0 || arrFI[0].Parent == null) { return(null); } IShellFolder oParentFolder = GetParentFolder(arrFI[0].Parent.FullName); if (oParentFolder == null) { return(null); } IntPtr[] arrPIDLs = new IntPtr[arrFI.Length]; int n = 0; foreach (DirectoryInfo fi in arrFI) { // Get the file relative to folder uint pchEaten = 0; SFGAO pdwAttributes = 0; IntPtr pPIDL = IntPtr.Zero; int nResult = oParentFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, fi.Name, ref pchEaten, out pPIDL, ref pdwAttributes); if (nResult != ResultOK) { FreePIDLs(arrPIDLs); return(null); } arrPIDLs[n] = pPIDL; n++; } return(arrPIDLs); }
public ShellItem(IShellFolder folder, IntPtr hwndOwner, string relativeName) { if (folder == null) { throw new ArgumentNullException("folder"); } if (relativeName == null) { throw new ArgumentNullException("relativeName"); } if (relativeName == string.Empty) { throw new ArgumentException("relativeName is empty"); } IntPtr relativePidl = folder.ParseDisplayName(hwndOwner, relativeName); try { this.FAbsolutePidl = GetAbsolutePidl(folder, relativePidl); } finally { Marshal.FreeCoTaskMem(relativePidl); } }
public System.Drawing.Bitmap GetThumbnail(string file, int width, int height) { if ((!File.Exists(file)) && (!Directory.Exists(file))) { throw new FileNotFoundException( String.Format("The file '{0}' does not exist", file), file); } if (thumbNail != null) { thumbNail.Dispose(); thumbNail = null; } IShellFolder folder = null; try { folder = GetDesktopFolder; } catch (Exception ex) { throw ex; } if (folder != null) { IntPtr pidlMain = IntPtr.Zero; try { int cParsed = 0; int pdwAttrib = 0; string filePath = Path.GetDirectoryName(file); pidlMain = IntPtr.Zero; folder.ParseDisplayName( IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlMain, out pdwAttrib); } catch (Exception ex) { Marshal.ReleaseComObject(folder); throw ex; } if (pidlMain != IntPtr.Zero) { // IShellFolder: Guid iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IShellFolder item = null; try { folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item); } catch (Exception ex) { Marshal.ReleaseComObject(folder); UnmanagedMethods.CoTaskMemFree(pidlMain); throw ex; } if (item != null) { IEnumIDList idEnum = null; try { item.EnumObjects( IntPtr.Zero, (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS), ref idEnum); } catch (Exception ex) { Marshal.ReleaseComObject(folder); UnmanagedMethods.CoTaskMemFree(pidlMain); throw ex; } if (idEnum != null) { int hRes = 0; IntPtr pidl = IntPtr.Zero; int fetched = 0; bool complete = false; while (!complete) { hRes = idEnum.Next(1, ref pidl, out fetched); if (hRes != 0) { pidl = IntPtr.Zero; complete = true; } else { if (GetThumbnail(file, pidl, item, width, height)) { complete = true; } } if (pidl != IntPtr.Zero) { UnmanagedMethods.CoTaskMemFree(pidl); } } Marshal.ReleaseComObject(idEnum); } Marshal.ReleaseComObject(item); } UnmanagedMethods.CoTaskMemFree(pidlMain); } Marshal.ReleaseComObject(folder); } return(thumbNail); }
public System.Drawing.Bitmap GetThumbnail(string file) { if (!Util.Utils.FileExistsInCache(file)) { throw new FileNotFoundException( String.Format("The file '{0}' does not exist", file), file); } if (thumbNail != null) { thumbNail.SafeDispose(); thumbNail = null; } IShellFolder folder = null; try { folder = getDesktopFolder; } catch (Exception ex) { throw ex; } if (folder != null) { IntPtr pidlMain = IntPtr.Zero; try { int cParsed = 0; int pdwAttrib = 0; string filePath = Path.GetDirectoryName(file); pidlMain = IntPtr.Zero; folder.ParseDisplayName( IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlMain, out pdwAttrib); } catch (Exception ex) { Marshal.ReleaseComObject(folder); throw ex; } if (pidlMain != IntPtr.Zero) { // IShellFolder: Guid iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IShellFolder item = null; try { folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item); } catch (Exception ex) { Marshal.ReleaseComObject(folder); Allocator.Free(pidlMain); throw ex; } //if (item != null) //{ // // // IEnumIDList idEnum = null; // try // { // item.EnumObjects( // IntPtr.Zero, // (ESHCONTF.SHCONTF_FOLDERS | // ESHCONTF.SHCONTF_NONFOLDERS), // ref idEnum); // } // catch (Exception ex) // { // Marshal.ReleaseComObject(folder); // Allocator.Free(pidlMain); // throw ex; // } // if (idEnum != null) // { // // start reading the enum: // int hRes = 0; // IntPtr pidl = IntPtr.Zero; // int fetched = 0; // bool complete = false; // while (!complete) // { // hRes = idEnum.Next(1, ref pidl, out fetched); // if (hRes != 0) // { // pidl = IntPtr.Zero; // complete = true; // } // else // { // if (getThumbnail(file, pidl, item)) // { // complete = true; // } // } // if (pidl != IntPtr.Zero) // { // Allocator.Free(pidl); // } // } // Marshal.ReleaseComObject(idEnum); // } // Marshal.ReleaseComObject(item); //} if (item != null) { string sFileName = Path.GetFileName(file); IntPtr pidlFile = IntPtr.Zero; try { int cParsed = 0; int pdwAttrib = 0; item.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, sFileName, out cParsed, out pidlFile, out pdwAttrib); if (pidlFile != IntPtr.Zero) { getThumbnail(file, pidlFile, item); Allocator.Free(pidlFile); } } catch (Exception ex) { Marshal.ReleaseComObject(folder); Allocator.Free(pidlMain); throw ex; } Marshal.ReleaseComObject(item); } Allocator.Free(pidlMain); } Marshal.ReleaseComObject(folder); } return(thumbNail); }
static IntPtr GetShellFolderChildrenRelativePIDL(IShellFolder parentFolder, string displayName) { var bindCtx = NativeMethods.CreateBindCtx(); uint pchEaten; uint pdwAttributes = 0; IntPtr ppidl; parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out pchEaten, out ppidl, ref pdwAttributes); return ppidl; }
Bitmap GetThumbnailFromIExtractImage(string fileName, int colorDepth) { IShellFolder desktopFolder; IShellFolder someFolder = null; IExtractImage extract = null; IntPtr pidl; IntPtr filePidl; // Manually define the IIDs for IShellFolder and IExtractImage Guid IID_IShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); Guid IID_IExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); //Divide the file name into a path and file name string folderName = Path.GetDirectoryName(fileName); string shortFileName = Path.GetFileName(fileName); //Get the desktop IShellFolder desktopFolder = getDesktopFolder; //Get the parent folder IShellFolder int cParsed = 0; int pdwAttrib = 0; desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, out cParsed, out pidl, out pdwAttrib); desktopFolder.BindToObject(pidl, IntPtr.Zero, ref IID_IShellFolder, ref someFolder); //Get the file//s IExtractImage someFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, shortFileName, out cParsed, out filePidl, out pdwAttrib); object iunk = null; IntPtr[] pidl_array = new IntPtr[] { filePidl }; someFolder.GetUIObjectOf(IntPtr.Zero, 1, pidl_array, ref IID_IExtractImage, IntPtr.Zero, out iunk); extract = (IExtractImage)iunk; //Set the size SIZE size; size.cx = DesiredSize.Width; size.cy = DesiredSize.Height; StringBuilder location = new StringBuilder(260, 260); int priority = 0; int requestedColourDepth = 32; // The IEIFLAG_ORIGSIZE flag tells it to use the original aspect // ratio for the image size. The IEIFLAG_QUALITY flag tells the // interface we want the image to be the best possible quality. EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_QUALITY; // EIEIFLAG.IEIFLAG_ASPECT;// EIEIFLAG.IEIFLAG_SCREEN; //EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_QUALITY; int uFlags = (int)flags; IntPtr bmp; //Interop will throw an exception if one of these calls fail. try { extract.GetLocation(location, location.Capacity, ref priority, ref size, requestedColourDepth, ref uFlags); extract.Extract(out bmp); if (!bmp.Equals(IntPtr.Zero)) { return(Image.FromHbitmap(bmp)); } else { return(null); } } catch { return(null); } finally { //Free the pidls. The Runtime Callable Wrappers should automatically release the COM objects Marshal.FreeCoTaskMem(pidl); Marshal.FreeCoTaskMem(filePidl); } }
private static IContextMenu GetContextMenu(IWin32Window owner, IShellFolder parent, string[] fileNames) { IContextMenu menu; IntPtr[] apidl = new IntPtr[fileNames.Length]; try { for (int i = 0; i < fileNames.Length; i++) { apidl[i] = parent.ParseDisplayName(owner.Handle, fileNames[i]); } menu = parent.GetUIObjectOf<IContextMenu>(owner.Handle, apidl); } finally { foreach (IntPtr ptr in apidl) { if (ptr != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptr); } } } return menu; }
public static void ShowContextMenu(IntPtr handle, IShellFolder desktop, string parent, List <FileSystemInfo> lst, double x, double y, ref IContextMenu2 newContextMenu2, ref IntPtr newSubmenuPtr) { if (lst == null) { ShowContextMenuFolder(handle, desktop, parent, lst, x, y, ref newContextMenu2, ref newSubmenuPtr); return; } IntPtr PPopup = IntPtr.Zero, PIDLParent = IntPtr.Zero, PSHParent = IntPtr.Zero; IntPtr PContext = IntPtr.Zero, PContext2 = IntPtr.Zero, PContext3 = IntPtr.Zero; List <IntPtr> ChildrenList = null; IContextMenu CContext = null; IContextMenu2 CContext2 = null; IContextMenu3 CContext3 = null; IShellFolder SHParent = null; try { //親フォルダの PIDL を取得する uint fcharcnt = 0; SFGAO fattr = SFGAO.BROWSABLE; if (desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, parent, ref fcharcnt, out PIDLParent, ref fattr) == Shell32Wrapper.S_OK) { //親フォルダのシェルフォルダのポインタを取得する if (desktop.BindToObject(PIDLParent, IntPtr.Zero, GUIDs.IID_IShellFolder, out PSHParent) == Shell32Wrapper.S_OK) { //親フォルダのIShellFolder を取得する SHParent = (IShellFolder)Marshal.GetTypedObjectForIUnknown(PSHParent, typeof(IShellFolder)); ChildrenList = new List <IntPtr>(); //対象ファイルの PIDL (親のシェルフォルダからの相対 PIDL)を取得する foreach (var files in lst) { IntPtr PFile; if (SHParent.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, files.Name, ref fcharcnt, out PFile, ref fattr) == Shell32Wrapper.S_OK) { ChildrenList.Add(PFile); } } //対象ファイルの IContextMenu へのポインタを取得する IntPtr[] children = ChildrenList.ToArray(); if (SHParent.GetUIObjectOf(IntPtr.Zero, (uint)children.Length, children, GUIDs.IID_IContextMenu, IntPtr.Zero, out PContext) == Shell32Wrapper.S_OK) { //対象ファイルの IContextMenu を取得する CContext = (IContextMenu)Marshal.GetTypedObjectForIUnknown(PContext, typeof(IContextMenu)); //対象ファイルの IContextMenu2, IContextMenu3 のポインタを取得する Marshal.QueryInterface(PContext, ref GUIDs.IID_IContextMenu2, out PContext2); Marshal.QueryInterface(PContext, ref GUIDs.IID_IContextMenu3, out PContext3); CContext2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(PContext2, typeof(IContextMenu2)); CContext3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(PContext3, typeof(IContextMenu3)); //ポップアップメニューを作成する PPopup = User32Wrapper.CreatePopupMenu(); //ポップアップメニューに、コンテキストメニュー IContextMenu を追加する CMF ContextMenuFlag = CMF.EXPLORE | CMF.CANRENAME; CContext.QueryContextMenu(PPopup, 0, Shell32Wrapper.CMD_FIRST, Shell32Wrapper.CMD_LAST, ContextMenuFlag); //ポップアップメニューを表示する //呼び出しをブロックします uint selected = User32Wrapper.TrackPopupMenuEx(PPopup, TPM.RETURNCMD, (int)x, (int)y, handle, IntPtr.Zero); if (selected >= Shell32Wrapper.CMD_FIRST) { uint cmdidx = selected - Shell32Wrapper.CMD_FIRST; Helper.InvokeCommand(CContext, cmdidx, parent, new Point(x, y)); } } } } } #region finally finally { if (PPopup != null) { User32Wrapper.DestroyMenu(PPopup); } if (CContext3 != null) { Marshal.FinalReleaseComObject(CContext3); CContext3 = null; } if (CContext2 != null) { Marshal.FinalReleaseComObject(CContext2); CContext2 = null; } if (CContext != null) { Marshal.FinalReleaseComObject(CContext); CContext = null; } if (ChildrenList != null) { foreach (var child in ChildrenList) { Marshal.FreeCoTaskMem(child); } ChildrenList = null; } if (SHParent != null) { Marshal.FinalReleaseComObject(SHParent); SHParent = null; } if (PIDLParent != IntPtr.Zero) { Marshal.FreeCoTaskMem(PIDLParent); } if (PSHParent != IntPtr.Zero) { Marshal.Release(PSHParent); } if (PContext != IntPtr.Zero) { Marshal.Release(PContext); } if (PContext2 != IntPtr.Zero) { Marshal.Release(PContext2); } if (PContext3 != IntPtr.Zero) { Marshal.Release(PContext3); } } #endregion }
private static void ShowContextMenuFolder(IntPtr handle, IShellFolder desktop, string parent, List <FileSystemInfo> lst, double x, double y, ref IContextMenu2 FolderContextMenu, ref IntPtr FolderContextPtr) { IntPtr PopupPtr = IntPtr.Zero; IntPtr newContextMenuPtr = IntPtr.Zero, newContextMenuPtr2 = IntPtr.Zero; IContextMenu newContextMenu = null; IntPtr PIDLParent = IntPtr.Zero; try { uint fcharcnt = 0; SFGAO fattr = SFGAO.BROWSABLE; //親フォルダのパス文字列からPIDLを取得 desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, parent, ref fcharcnt, out PIDLParent, ref fattr); //メニューを作成 PopupPtr = User32Wrapper.CreatePopupMenu(); #region gomi //コンテキストメニューに項目追加 //不必要なのでやらない //viewSubMenu = User32Wrapper.CreatePopupMenu(); //MENUITEMINFO itemInfo = new MENUITEMINFO("View"); //itemInfo.cbSize = Shell32Wrapper.cbMenuItemInfo; //itemInfo.fMask = MIIM.SUBMENU | MIIM.STRING; //itemInfo.hSubMenu = viewSubMenu; //User32Wrapper.InsertMenuItem(PopupPtr, 0, true, ref itemInfo); //MFT rCheck = MFT.RADIOCHECK | MFT.CHECKED; //User32Wrapper.AppendMenu(viewSubMenu, // rCheck, (uint)0, "Tiles"); //User32Wrapper.AppendMenu(viewSubMenu, // rCheck, (uint)1, "Icons"); //User32Wrapper.AppendMenu(viewSubMenu, // rCheck, (uint)2, "List"); //User32Wrapper.AppendMenu(viewSubMenu, // rCheck, (uint)3, "Details"); #endregion #region New Submenu //IContextMenuを作成する if (GetNewContextMenu(PIDLParent, out newContextMenuPtr, out newContextMenu)) { //セパレータ //User32Wrapper.AppendMenu(PopupPtr, MFT.SEPARATOR, 0, string.Empty); //作成したIContextMenuをメニュー(ここではcontextMenu)に追加する newContextMenu.QueryContextMenu(PopupPtr, 0, Shell32Wrapper.CMD_FIRST, Shell32Wrapper.CMD_LAST, CMF.NORMAL); //サブメニューを取得(ここでは追加したIContextMenu? //WndProcで使用される FolderContextPtr = User32Wrapper.GetSubMenu(PopupPtr, 0); Marshal.QueryInterface(newContextMenuPtr, ref GUIDs.IID_IContextMenu2, out newContextMenuPtr2); FolderContextMenu = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(newContextMenuPtr2, typeof(IContextMenu2)); } #endregion uint selected = User32Wrapper.TrackPopupMenuEx(PopupPtr, TPM.RETURNCMD, (int)x, (int)y, handle, IntPtr.Zero); if (selected >= Shell32Wrapper.CMD_FIRST) { uint cmdidx = selected - Shell32Wrapper.CMD_FIRST; Helper.InvokeCommand(newContextMenu, cmdidx, parent, new Point(x, y)); } } #region finally finally { if (PopupPtr != IntPtr.Zero) { User32Wrapper.DestroyMenu(PopupPtr); } if (newContextMenuPtr != IntPtr.Zero) { Marshal.Release(newContextMenuPtr); newContextMenuPtr = IntPtr.Zero; } if (newContextMenuPtr2 != IntPtr.Zero) { Marshal.Release(newContextMenuPtr2); newContextMenuPtr2 = IntPtr.Zero; } if (newContextMenu != null) { Marshal.FinalReleaseComObject(newContextMenu); newContextMenu = null; } if (FolderContextMenu != null) { Marshal.FinalReleaseComObject(FolderContextMenu); FolderContextMenu = null; } } #endregion }
// TODO: Clean public bool TrySetSelection(Address[] addresses, string pathToFocus, bool fDeselectOthers) { if (addresses != null) { IShellFolder ppshf = null; IShellView ppshv = null; try { if (shellBrowser.QueryActiveShellView(out ppshv) == 0) { IntPtr ptr3; if (PInvoke.SHGetDesktopFolder(out ppshf) != 0) { return(false); } bool flag = true; bool flag2 = false; bool flag3 = !string.IsNullOrEmpty(pathToFocus); uint pchEaten = 0; uint pdwAttributes = 0; if (fDeselectOthers) { ((IFolderView)ppshv).SelectItem(0, 4); } foreach (Address address in addresses) { IntPtr zero = IntPtr.Zero; if ((address.ITEMIDLIST != null) && (address.ITEMIDLIST.Length > 0)) { zero = ShellMethods.CreateIDL(address.ITEMIDLIST); } if ((((zero != IntPtr.Zero) || (address.Path == null)) || ((address.Path.Length <= 0) || (ppshf.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, address.Path, ref pchEaten, out zero, ref pdwAttributes) == 0))) && (zero != IntPtr.Zero)) { IntPtr pidlItem = PInvoke.ILFindLastID(zero); uint uFlags = 1; if (flag) { uFlags |= 8; if (!flag3) { flag2 = true; uFlags |= 0x10; } if (fDeselectOthers) { uFlags |= 4; } flag = false; } if ((!flag2 && flag3) && (address.Path == pathToFocus)) { flag2 = true; uFlags |= 0x10; } ppshv.SelectItem(pidlItem, uFlags); PInvoke.CoTaskMemFree(zero); } } if ((!flag2 && flag3) && (ppshf.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, pathToFocus, ref pchEaten, out ptr3, ref pdwAttributes) == 0)) { IntPtr ptr4 = PInvoke.ILFindLastID(ptr3); ppshv.SelectItem(ptr4, 0x18); PInvoke.CoTaskMemFree(ptr3); } return(true); } } catch (Exception exception) { QTUtility2.MakeErrorLog(exception); } finally { if (ppshv != null) { Marshal.ReleaseComObject(ppshv); } if (ppshf != null) { Marshal.ReleaseComObject(ppshf); } } } return(false); }
public static int ShowContextMenu(IntPtr hwnd, string[] strFiles, int intX, int intY, bool blnErrorBeep) { CMINVOKECOMMANDINFO CI = new CMINVOKECOMMANDINFO(); DirectoryInfo dInfo; FileInfo fInfo; IContextMenu CM; // LPCONTEXTMENU int cmdID; // LongBool int pchEaten = 0; // DWORD pchEaten; int dwAttributes = new int(); // ULONG int intZero = 0; int intCount; IntPtr ipParent = new IntPtr(); // LPITEMIDLIST IntPtr ipChild = new IntPtr(); // LPITEMIDLIST IntPtr hMenu = new IntPtr(); // HMENU //IntPtr ipPath = new IntPtr(); //IntPtr ipFileName = new IntPtr(); IShellFolder DesktopFolder = null; // IShellFolder IShellFolder ParentFolder = null; // IShellfolder ParentFolder Rect mRect; REFIID IID_IShellFolder = new REFIID("000214E6-0000-0000-c000-000000000046"); REFIID IID_IContextMenu = new REFIID("000214E4-0000-0000-c000-000000000046"); string strFullName = ""; string strFilePath = ""; string strFileName = ""; string strFQName = ""; m_pidlCount = 0; for (intCount = 0; intCount <= strFiles.GetUpperBound(0); intCount++) { strFullName = strFiles[intCount]; fInfo = new FileInfo(strFullName); if (fInfo.Exists) { strFilePath = fInfo.DirectoryName; strFileName = fInfo.Name; strFQName = fInfo.FullName; } else { dInfo = new DirectoryInfo(strFullName); if (dInfo.Exists) { try { strFileName = dInfo.Name; strFilePath = dInfo.Parent.FullName; } catch { strFilePath = ""; } } } SHGetDesktopFolder(ref DesktopFolder); // ParseDisplayName - parent // Translates a file object's or folder's display name into an item identifier list pchEaten = 1; dwAttributes = 0; ipParent = new IntPtr(); DesktopFolder.ParseDisplayName(hwnd, IntPtr.Zero, strFilePath, ref pchEaten, ref ipParent, ref dwAttributes); // BindToObject // Retrieves an IShellFolder object for a subfolder ParentFolder = null; DesktopFolder.BindToObject(ipParent, IntPtr.Zero, ref IID_IShellFolder, ref ParentFolder); // ParseDisplayName - child // Translates a file object's or folder's display name into an item identifier list pchEaten = 1; dwAttributes = 0; ipChild = new IntPtr(); ParentFolder.ParseDisplayName(hwnd, IntPtr.Zero, strFileName, ref pchEaten, ref ipChild, ref dwAttributes); JAddItemToIDList(ipChild); } CM = null; int intReturn = ParentFolder.GetUIObjectOf(hwnd, m_pidlCount, ref m_ipList[0], ref IID_IContextMenu, out intZero, ref CM); if (CM != null) { hMenu = CreatePopupMenu(); CM.QueryContextMenu(hMenu, 0, MIN_SHELL_ID, MAX_SHELL_ID, QueryContextMenuFlags.CMF_EXPLORE); cmdID = TrackPopupMenu(hMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), intX, intY, 0, hwnd, out mRect); if (cmdID != 0) { CI.cbSize = Marshal.SizeOf(CI); CI.hwnd = hwnd; CI.lpVerb = (IntPtr)MAKEINTRESOURCE(cmdID - 1); CI.lpParameters = IntPtr.Zero; CI.lpDirectory = IntPtr.Zero; CI.nShow = SW_SHOWNORMAL; CM.InvokeCommand(ref CI); } } else { if (blnErrorBeep) { MessageBeep(-1); } } return(0); }