using System; using System.Runtime.InteropServices; using Vanara.InteropServices; namespace Vanara.PInvoke { public static partial class Shell32 { /// Specifies the default save location. public enum DEFAULTSAVEFOLDERTYPE { /// /// The current user determines the save folder. If the current user is the library's owner, use the private save location /// (DSFT_PRIVATE). If the current user is not the library's owner, use the public save location (DSFT_PUBLIC). /// DSFT_DETECT = 1, /// The library's private save location, which can only be accessed by the library's owner. DSFT_PRIVATE, /// The library's public save location, which can be accessed by all users. DSFT_PUBLIC } /// Defines options for filtering folder items. public enum LIBRARYFOLDERFILTER { /// Return only file system items. LFF_FORCEFILESYSTEM = 1, /// Return items that can be bound to an IStorage object. LFF_STORAGEITEMS = 2, /// Return all items. LFF_ALLITEMS = 3 } /// Used by SHShowManageLibraryUI to define options for handling a name collision when saving a library. public enum LIBRARYMANAGEDIALOGOPTIONS { /// Show default warning UI to the user. LMD_DEFAULT = 0x00000000, /// Do not display a warning dialog to the user in collisions that concern network locations that cannot be indexed. LMD_ALLOWUNINDEXABLENETWORKLOCATIONS = 0x00000001 } /// Specifies the library options. [Flags] public enum LIBRARYOPTIONFLAGS { /// No library options are set. LOF_DEFAULT = 0x00000000, /// Pin the library to the navigation pane. LOF_PINNEDTONAVPANE = 0x00000001, /// All valid library options flags. LOF_MASK_ALL = 0x00000001 } /// Specifies the options for handling a name collision when saving a library. [Flags] public enum LIBRARYSAVEFLAGS { /// If a library with the same name already exists, the save operation fails. LSF_FAILIFTHERE = 0x00000000, /// If a library with the same name already exists, the save operation overwrites the existing library. LSF_OVERRIDEEXISTING = 0x00000001, /// If a library with the same name already exists, the save operation generates a new, unique name for the library. LSF_MAKEUNIQUENAME = 0x00000002, } /// Exposes methods for creating and managing libraries. [ComImport, Guid("11a66efa-382e-451a-9234-1e0e12ef3085"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), CoClass(typeof(CShellLibrary))] public interface IShellLibrary { /// Loads the library from a specified library definition file. /// /// An IShellItem object for the library definition file to load. An error is returned if this object is not a library. /// /// One or more STGM storage medium flags that specify access and sharing modes for the library object. void LoadLibraryFromItem([In, MarshalAs(UnmanagedType.Interface)] IShellItem library, [In] STGM grfMode); /// Loads the library that is referenced by a KNOWNFOLDERID. /// The KNOWNFOLDERID value that identifies the library to load. /// One or more STGM storage medium flags that specify access and sharing modes for the library object. void LoadLibraryFromKnownFolder(in Guid knownfidLibrary, [In] STGM grfMode); /// Adds a folder to the library. /// An IShellItem object that represents the folder to be added to the library. void AddFolder([In, MarshalAs(UnmanagedType.Interface)] IShellItem location); /// Removes a folder from the library. /// An IShellItem object that represents the folder to remove. void RemoveFolder([In, MarshalAs(UnmanagedType.Interface)] IShellItem location); /// Gets the set of child folders that are contained in the library. /// One of the following LIBRARYFOLDERFILTER values that determines the folders to get. /// /// A reference to the IID of the interface to get in ppv. This value is typically IID_IShellItemArray, but it can also be /// IID_IObjectCollection, IID_IObjectArray, or the IID of any other interface that is implemented by CShellItemArray. /// /// A pointer to the interface requested in riid. If this call fails, this value is NULL. [return: MarshalAs(UnmanagedType.Interface)] object GetFolders([In] LIBRARYFOLDERFILTER lff, in Guid riid); /// Resolves the target location of a library folder, even if the folder has been moved or renamed. /// An IShellItem object that represents the library folder to locate. /// /// The maximum time, in milliseconds, the method will attempt to locate the folder before returning. If the folder could not be /// located before the specified time elapses, an error is returned. /// /// /// A reference to the IID of the interface to get in ppv that will represent the resolved target location. This value is /// typically IID_IShellItem, but it can also be IID_IShellItem2 or the IID of any other interface that is implemented by CShellItem. /// /// A pointer to the interface requested in riid. [return: MarshalAs(UnmanagedType.Interface)] object ResolveFolder([In] IShellItem folderToResolve, [In] uint timeout, in Guid riid); /// Retrieves the default target folder that the library uses for save operations. /// The DEFAULTSAVEFOLDERTYPE value that specifies the save folder to get. /// /// A reference to the IID of the interface to get in ppv that will represent the save location. This value is typically /// IID_IShellItem, but it can also be IID_IShellItem2 or the IID of any other interface that is implemented by CShellItem. /// /// A pointer to the interface requested in riid. [return: MarshalAs(UnmanagedType.Interface)] object GetDefaultSaveFolder([In] DEFAULTSAVEFOLDERTYPE dsft, in Guid riid); /// Sets the default target folder that the library will use for save operations. /// The DEFAULTSAVEFOLDERTYPE value that specifies the default save location to set. /// /// An IShellItem object that represents the folder that to use as the default save location. The folder that this object /// represents must be a folder that is already in the library. /// void SetDefaultSaveFolder([In] DEFAULTSAVEFOLDERTYPE dsft, [In] IShellItem si); /// Gets the library's options. /// /// The library options for this library. LIBRARYOPTIONFLAGS is a bitwise enumerator, which means that more than one flag could /// be set. /// LIBRARYOPTIONFLAGS GetOptions(); /// Sets the library options. /// A bitmask that specifies the LIBRARYOPTIONFLAGS values to change in this call. /// /// A bitmask that specifies the new value of each LIBRARYOPTIONFLAGS value to change. LIBRARYOPTIONFLAGS values that are not set /// in lofMask are not changed by this call. /// void SetOptions([In] LIBRARYOPTIONFLAGS lofMask, [In] LIBRARYOPTIONFLAGS lofOptions); /// Gets the library's folder type. /// The view template that is applied to a folder, usually based on its intended use and contents. [return: MarshalAs(UnmanagedType.LPStruct)] Guid GetFolderType(); /// Sets the library's folder type. /// /// The GUID or FOLDERTYPEID that represents the view template that is applied to a folder, usually based on its intended use and contents. /// void SetFolderType(in Guid ftid); /// Gets the default icon for the library. /// /// A null-terminated Unicode string that describes the location of the default icon. The string is returned as /// ModuleFileName,ResourceIndex or ModuleFileName,-ResourceID. /// SafeCoTaskMemString GetIcon(); /// Sets the default icon for the library. /// /// A null-terminated Unicode string that describes the location of the default icon. The string must be formatted as /// ModuleFileName,ResourceIndex or ModuleFileName,-ResourceID. /// void SetIcon([In, MarshalAs(UnmanagedType.LPWStr)] string icon); /// Commits library updates to an existing Library Description file. void Commit(); /// Saves the library to a new Library Description (*.library-ms) file. /// /// The IShellItem object that specifies the folder in which to save the library, or NULL to save the library with the user's /// default libraries in the FOLDERID_Libraries known folder. /// /// /// The file name under which to save the library. The file name must not include the file name extension; the file name /// extension is added automatically. /// /// The LIBRARYSAVEFLAGS value that specifies how to handle a library name collision. /// The IShellItem object that represents the library description file into which the library was saved. [return: MarshalAs(UnmanagedType.Interface)] IShellItem Save([In] IShellItem folderToSaveIn, [In, MarshalAs(UnmanagedType.LPWStr)] string libraryName, [In] LIBRARYSAVEFLAGS lsf); /// Saves the library to a new file in a specified known folder. /// /// The ID of the known folder in which to save the IShellLibrary object. For more information, see KNOWNFOLDERID. /// /// /// The file name under which to save the library. The file name must not include the file name extension; the file name /// extension is added automatically. /// /// The LIBRARYSAVEFLAGS value that specifies how to handle a library name collision. /// The IShellItem object that represents the library description file into which the library was saved. [return: MarshalAs(UnmanagedType.Interface)] IShellItem SaveInKnownFolder(in Guid kfidToSaveIn, [In, MarshalAs(UnmanagedType.LPWStr)] string libraryName, [In] LIBRARYSAVEFLAGS lsf); } /// Retrieves the default target folder that the library uses for save operations. /// The type of the interface to get. /// The instance. /// The DEFAULTSAVEFOLDERTYPE value that specifies the save folder to get. /// A pointer to the interface requested. public static T GetDefaultSaveFolder(this IShellLibrary sl, [In] DEFAULTSAVEFOLDERTYPE dsft) where T : class => (T)sl.GetDefaultSaveFolder(dsft, typeof(T).GUID); /// Gets the set of child folders that are contained in the library. /// The type of the interface to get. /// The instance. /// One of the following LIBRARYFOLDERFILTER values that determines the folders to get. /// A pointer to the interface requested. If this call fails, this value is NULL. public static T GetFolders(this IShellLibrary sl, [In] LIBRARYFOLDERFILTER lff) where T : class => (T)sl.GetFolders(lff, typeof(T).GUID); /// Resolves the target location of a library folder, even if the folder has been moved or renamed. /// The type of the interface to get. /// The instance. /// An IShellItem object that represents the library folder to locate. /// /// The maximum time, in milliseconds, the method will attempt to locate the folder before returning. If the folder could not be /// located before the specified time elapses, an error is returned. /// /// A pointer to the interface requested. public static T ResolveFolder(this IShellLibrary sl, [In] IShellItem folderToResolve, [In] uint timeout) where T : class => (T)sl.ResolveFolder(folderToResolve, timeout, typeof(T).GUID); /// Resolves all locations in a library, even those locations that have been moved or renamed. /// A pointer to an IShellItem object that represents the library. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// /// This function can block the calling thread for as long as it takes to resolve all the locations in the specified library. Because /// it blocks the thread from which it is called, it should not be called from a thread that also handles user interface interactions. /// /// This function resolves all locations in the specified library in a single call. To resolve an individual location in a library, /// see the IShellLibrary::ResolveFolder method or the SHResolveFolderPathInLibrary function. /// /// [DllImport(Lib.Shell32, ExactSpelling = true)] [PInvokeData("Shobjidl.h", MSDNShortId = "dd378439")] public static extern HRESULT SHResolveLibrary([MarshalAs(UnmanagedType.Interface)] IShellItem psiLibrary); /// Shows the library management dialog box, which enables users to manage the library folders and default save location. /// A pointer to an IShellItem object that represents the library that is to be managed. /// /// The handle for the window that owns the library management dialog box. The value of this parameter can be NULL. /// /// /// A pointer to the title for the library management dialog. To display the generic title string, set the value of this parameter to NULL. /// /// /// A pointer to a help string to display below the title string in the library management dialog box. To display the generic help /// string, set the value of this parameter to NULL. /// /// /// A value from the LIBRARYMANAGEDIALOGOPTIONS enumeration that specifies the behavior of the management dialog box. /// /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [DllImport(Lib.Shell32, ExactSpelling = true)] [PInvokeData("Shobjidl.h", MSDNShortId = "dd378433")] public static extern HRESULT SHShowManageLibraryUI(IShellItem psiLibrary, HWND hwndOwner, [In, Optional, MarshalAs(UnmanagedType.LPWStr)] string pszTitle, [In, Optional, MarshalAs(UnmanagedType.LPWStr)] string pszInstruction, LIBRARYMANAGEDIALOGOPTIONS lmdOptions); /// Class interface for IShellLibrary [ComImport, Guid("d9b3211d-e57f-4426-aaef-30a806add397"), ClassInterface(ClassInterfaceType.None)] public class CShellLibrary { } } }