IShellFolder GetIShellFolder() { IShellFolder desktop = null; Shell32.SHGetDesktopFolder(out desktop); IntPtr desktopPidl = IntPtr.Zero; Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, CSIDL.CSIDL_DESKTOP, ref desktopPidl);; if (Shell32.ILIsEqual(_pidl, desktopPidl)) { return(desktop); } else { IntPtr result; desktop.BindToObject(_pidl, IntPtr.Zero, ref Shell32.IID_IShellFolder, out result); var folder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(result, typeof(IShellFolder)); return(folder); } }
public static bool GetShellFolder(IntPtr pIDLFolder, out IShellFolder shellFolder) { shellFolder = null; if (PInvoke.ILGetSize(pIDLFolder) == 2) { return(0 == PInvoke.SHGetDesktopFolder(out shellFolder)); } IShellFolder ppshf = null; Guid riid = ExplorerGUIDs.IID_IShellFolder; try { if (PInvoke.SHGetDesktopFolder(out ppshf) == 0) { return(0 == ppshf.BindToObject(pIDLFolder, IntPtr.Zero, ref riid, out shellFolder)); } } catch { } finally { if (ppshf != null) { Marshal.ReleaseComObject(ppshf); } } return(false); }
private static IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl) { IShellFolder folder; Marshal.ThrowExceptionForHR(parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder)); return(folder); }
public static IShellFolder GetIShellFolder(this ShellItem item) { var _pidl = item.PIDL; // Absolute PIDL IShellFolder desktop = DesktopFolder; IntPtr desktopPidl = IntPtr.Zero; Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, CSIDL.CSIDL_DESKTOP, ref desktopPidl);; if (Shell32.ILIsEqual(_pidl, desktopPidl)) { return((IShellFolder)desktop); } else { IntPtr result; desktop.BindToObject(_pidl, IntPtr.Zero, ref Shell32.IID_IShellFolder, out result); var folder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(result, typeof(IShellFolder)); return(folder); } }
private static IShellFolder PidlToShellFolder(IShellFolder parent, IntPtr pidl) { var result = parent.BindToObject(pidl, null, ref _iidIShellFolder, out var folder); Marshal.ThrowExceptionForHR(result); return(folder); }
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); }
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) { #region 判断节点是否已经展开 if (e.Node.Nodes.Count != 1) { return; } else { if (e.Node.FirstNode.Text != "...") { return; } } e.Node.Nodes.Clear(); #endregion try // 因为文件夹安全设置和设备准备问题,可能导致失败。 { ShellItem sItem = (ShellItem)e.Node.Tag; IShellFolder root = sItem.ShellFolder; //循环查找子项 IEnumIDList Enum = null; IntPtr EnumPtr = IntPtr.Zero; IntPtr pidlSub; int celtFetched; if (root.EnumObjects(this.Handle, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN, out EnumPtr) == API.S_OK) { Enum = (IEnumIDList)Marshal.GetObjectForIUnknown(EnumPtr); while (Enum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE) { string name = API.GetNameByIShell(root, pidlSub); IShellFolder iSub; try { root.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub); } catch (Exception ex) { MessageBox.Show(ex.Message + " : " + name); continue; } ShellItem shellItem = new ShellItem(pidlSub, iSub, sItem); int imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr); TreeNode nodeSub = new TreeNode(name, imgIndex, imgIndex); nodeSub.Tag = shellItem; nodeSub.Nodes.Add("..."); e.Node.Nodes.Add(nodeSub); } } }catch (Exception ex) { MessageBox.Show(ex.Message); } }
static IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl) { IShellFolder folder; var result = parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder); Marshal.ThrowExceptionForHR((int)result); return(folder); }
/// <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); }
static IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl) { IShellFolder folder; int result = parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder); if (result != 0) { Marshal.ThrowExceptionForHR(result); } return(folder); }
/// <summary> /// 指定された IShellFolder とその子の IDL から、子のアイテムに対応した IShellFolder を作成する。 /// </summary> public static IShellFolder BindToIShellFolder(IShellFolder parent, IntPtr idl) { IShellFolder sf; uint hRes = parent.BindToObject(idl, IntPtr.Zero, ref IID_IShellFolder, out sf); if (hRes != 0) { Marshal.ThrowExceptionForHR((int)hRes); } return(sf); }
/***************************************************************************** * * GetSpecialFolder * * Create an IShellFolder for the specified special folder. * * It is the responsibility of the caller to Release() the * IShellFolder that is returned. * *****************************************************************************/ static IShellFolder GetSpecialFolder(HWND hwnd, CSIDL idFolder) { if (SHGetFolderLocation(hwnd, idFolder, ppidl: out var pidl).Succeeded) { using (pidl) if (g_psfDesktop.BindToObject(pidl, default, typeof(IShellFolder).GUID, out var ppv).Succeeded) { return((IShellFolder)ppv); } } return(null); }
private IShellFolder GetShellFolder(IntPtr folderPidl) { IShellFolder desktop = GetShellFolder(); Guid guid = typeof(IShellFolder).GUID; if (desktop.BindToObject(folderPidl, IntPtr.Zero, ref guid, out _shellFolderPtr) == NativeMethods.S_OK) { Marshal.ReleaseComObject(desktop); return((IShellFolder)Marshal.GetTypedObjectForIUnknown(_shellFolderPtr, typeof(IShellFolder))); } ShellLogger.Error($"ShellFolder: Unable to bind IShellFolder for {folderPidl}"); return(null); }
/// <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); }
public static IShellFolder GetPidlCloneFolder(PidlClone pidlClone) { int hr = 0; IShellFolder desktopFolder = PidlFolderData.DesktopFolder; var pidlTmp = pidlClone.Handle; IntPtr folderTmpPtr = IntPtr.Zero; if ((hr = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero, ref Shell32.IID_IShellFolder, out folderTmpPtr)) != WinError.S_OK) { return(null); } return(Marshal.GetObjectForIUnknown(folderTmpPtr) as IShellFolder); }
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); }
private IShellFolder GetIShellFolder() { IShellFolder desktop = Shell32.SHGetDesktopFolder(); IntPtr desktopPidl; Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, CSIDL.DESKTOP, out desktopPidl);; if (Shell32.ILIsEqual(Pidl, desktopPidl)) { return(desktop); } else { IntPtr result; desktop.BindToObject(Pidl, IntPtr.Zero, typeof(IShellFolder).GUID, out result); return((IShellFolder)Marshal.GetTypedObjectForIUnknown(result, typeof(IShellFolder))); } }
/// <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); }
//========================================================== //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); }
IShellFolder IFolder(IntPtr pidl, IShellFolder pShellFolderInterface, ref int result) { if (Shell32.ILIsEqual(pidl, Desktop.PIDL)) { result = WinError.S_OK; return((IShellFolder)Desktop.ShellFolderInterface); } IntPtr ppv = IntPtr.Zero; result = pShellFolderInterface.BindToObject(pidl, IntPtr.Zero, ref Shell32.IID_IShellFolder, out ppv); if (ppv == IntPtr.Zero) { // SBSP_ABSOLUTE - pidl is an absolute pidl (relative from desktop) var pidlTmp = Shell32.ILClone(pidl); IntPtr folderTmpPtr = IntPtr.Zero; IShellFolder desktopFolder = NativePidl.DesktopFolder; var hr = desktopFolder.BindToObject(pidlTmp, IntPtr.Zero, ref Shell32.IID_IShellFolder, out folderTmpPtr); if (hr != WinError.S_OK) { return(null); } var folderTmp = Marshal.GetObjectForIUnknown(folderTmpPtr) as IShellFolder; if (folderTmp == null) { Shell32.ILFree(pidlTmp); return(null);; // WinError.E_FAIL } return(folderTmp); } var shellFolderInterface = Marshal.GetObjectForIUnknown(ppv) as IShellFolder; return(shellFolderInterface); }
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); }
private IShellFolder getParentShellFolder(string path) { IntPtr parentPidl = pathToFullPidl(parent); IntPtr desktopFolderPtr; ShellFolders.SHGetDesktopFolder(out desktopFolderPtr); IShellFolder desktop = (IShellFolder)Marshal.GetTypedObjectForIUnknown(desktopFolderPtr, typeof(IShellFolder)); IntPtr parentPtr; if (desktop.BindToObject(parentPidl, IntPtr.Zero, ref ShellFolders.IID_IShellFolder, out parentPtr) == ShellFolders.S_OK) { Marshal.FinalReleaseComObject(desktop); return((IShellFolder)Marshal.GetTypedObjectForIUnknown(parentPtr, typeof(IShellFolder))); } else { CairoLogger.Instance.Debug("Unable to bind IShellFolder"); return(null); } }
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 IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl) { IShellFolder folder; var result = parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder); Marshal.ThrowExceptionForHR((int)result); return folder; }
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 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 }
public Bitmap LoadImageFromExtractIcon(string filename) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } Bitmap bm1 = null; IntPtr hbitmap = IntPtr.Zero; IShellFolder desktop = null; IntPtr pidlParent = IntPtr.Zero; IShellFolder shellFolder = null; IUnknown iunk = null; IExtractIcon extractIcon = null; try { Debug.Write("Trying IExtractIcon\r\n"); Shell32.SHGetDesktopFolder(ref desktop); int cParsed = 0; int pdwAttrib = 0; string filePath = filename; if (System.IO.File.Exists(filename)) { filePath = System.IO.Path.GetDirectoryName(filename); } desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib); Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder); Guid uuidExtractIcon = new Guid("000214eb-0000-0000-c000-000000000046"); iunk = GetUIObject(shellFolder, filename, uuidExtractIcon); if (iunk == null) { Debug.Write(" IExtractIcon not supported\r\n"); return(null); } int hres = 0; extractIcon = (IExtractIcon)iunk; StringBuilder location = new StringBuilder(260, 260); int iconIndex = 0; GILFLAGS2 flags = GILFLAGS2.GIL_None; hres = extractIcon.GetIconLocation(0, location, location.Capacity, out iconIndex, out flags); IntPtr iconLarge = IntPtr.Zero; IntPtr iconSmall = IntPtr.Zero; uint iconSize = (16 | (48 << 16)); hres = extractIcon.Extract(location, iconIndex, out iconLarge, out iconSmall, iconSize); Icon ico = Icon.FromHandle(iconLarge); bm1 = ico.ToBitmap(); } catch (Exception e) { Debug.Write(" Exception extracting image from IExtractIcon: " + e.Message + "\r\n"); } finally { if (extractIcon != null) { Marshal.ReleaseComObject(extractIcon); extractIcon = null; } if (iunk != null) { Marshal.ReleaseComObject(iunk); iunk = null; } if (shellFolder != null) { Marshal.ReleaseComObject(shellFolder); shellFolder = null; } if (pidlParent != IntPtr.Zero) { Allocator.Free(pidlParent); pidlParent = IntPtr.Zero; } if (desktop != null) { Marshal.ReleaseComObject(desktop); desktop = null; } if (_allocator != null) { Marshal.ReleaseComObject(_allocator); _allocator = null; } } return(bm1); }
public Bitmap GetThumbnail(string fileName) { if (!File.Exists(fileName) && !Directory.Exists(fileName)) { throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName); } if (this.thumbNail != null) { this.thumbNail.Dispose(); this.thumbNail = null; } IShellFolder folder = null; try { folder = this.DesktopFolder; } catch (Exception ex) { throw ex; } if (folder != null) { IntPtr pidlMain = IntPtr.Zero; try { int parsed = 0; int pdwAttrib = 0; string filePath = Path.GetDirectoryName(fileName); folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref parsed, ref pidlMain, ref pdwAttrib); } catch (Exception ex) { Marshal.ReleaseComObject(folder); throw ex; } if (pidlMain != IntPtr.Zero) { 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); this.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); this.Allocator.Free(pidlMain); throw ex; } if (idEnum != null) { int res = 0; IntPtr pidl = IntPtr.Zero; int fetched = 0; bool complete = false; while (!complete) { res = idEnum.Next(1, ref pidl, ref fetched); if (res != 0) { pidl = IntPtr.Zero; complete = true; } else { if (this.GetThumbnailHelper(fileName, pidl, item)) { complete = true; } } if (pidl != IntPtr.Zero) { this.Allocator.Free(pidl); } } Marshal.ReleaseComObject(idEnum); } Marshal.ReleaseComObject(item); } this.Allocator.Free(pidlMain); } Marshal.ReleaseComObject(folder); } return(this.ThumbNail); }
private static IShellItem ToIShellItem(IShellFolder folder, IntPtr pidl) { IntPtr p; IShellItem result = null; folder.BindToObject(pidl, IntPtr.Zero, ref ShellGuids.IShellItem, out p); if (p != IntPtr.Zero) { result = (IShellItem) Marshal.GetTypedObjectForIUnknown(p, typeof (IShellItem)); Marshal.Release(p); } return result; }
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); }
/// <summary> /// Retrieves a handler, typically the Shell folder object that implements IShellFolder for a particular item. Optional parameters /// that control the construction of the handler are passed in the bind context. /// </summary> /// <typeparam name="T">Type of the interface to get, usually IShellFolder or IStream.</typeparam> /// <param name="relativePidl"> /// A relative PIDL that identifies the subfolder. This value can refer to an item at any level below the parent folder in the /// namespace hierarchy. /// </param> /// <param name="bindCtx"> /// A pointer to an IBindCtx interface on a bind context object that can be used to pass parameters to the construction of the /// handler. If this parameter is not used, set it to <see langword="null"/>. Because support for this parameter is optional for /// folder object implementations, some folders may not support the use of bind contexts. /// <para> /// Information that can be provided in the bind context includes a BIND_OPTS structure that includes a grfMode member that /// indicates the access mode when binding to a stream handler. Other parameters can be set and discovered using /// IBindCtx::RegisterObjectParam and IBindCtx::GetObjectParam. /// </para> /// </param> /// <returns>Receives the interface pointer requested in <typeparamref name="T"/>.</returns> public T BindToObject <T>(PIDL relativePidl, IBindCtx bindCtx = null) where T : class => iShellFolder.BindToObject <T>(relativePidl, bindCtx);