using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using Vanara.InteropServices;
using static Vanara.PInvoke.AdvApi32;
using static Vanara.PInvoke.ComCtl32;
using static Vanara.PInvoke.Kernel32;
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedParameter.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable FieldCanBeMadeReadOnly.Global
// ReSharper disable InconsistentNaming
// ReSharper disable MemberHidesStaticFromOuterClass
// ReSharper disable UnusedMethodReturnValue.Global
namespace Vanara.PInvoke
{
[SuppressUnmanagedCodeSecurity]
public static partial class Shell32
{
// Defined in wingdi.h
private const int LF_FACESIZE = 32;
/// Used for options in SHOpenFolderAndSelectItems.
[PInvokeData("Shlobj.h", MSDNShortId = "bb762232")]
public enum OFASI
{
/// No options.
OFASI_NONE = 0,
///
/// Select an item and put its name in edit mode. This flag can only be used when a single item is being selected. For multiple item selections, it
/// is ignored.
///
OFASI_EDIT = 1,
///
/// Select the item or items on the desktop rather than in a Windows Explorer window. Note that if the desktop is obscured behind open windows, it
/// will not be made visible.
///
OFASI_OPENDESKTOP = 2
}
/// Flags that direct the handling of the item from which you're retrieving the info tip text. This value is commonly zero (QITIPF_DEFAULT).
[PInvokeData("Shlobj.h", MSDNShortId = "bb761357")]
public enum QITIP
{
/// No special handling.
QITIPF_DEFAULT = 0x00000000,
/// Provide the name of the item in ppwszTip rather than the info tip text.
QITIPF_USENAME = 0x00000001,
/// If the item is a shortcut, retrieve the info tip text of the shortcut rather than its target.
QITIPF_LINKNOTARGET = 0x00000002,
/// If the item is a shortcut, retrieve the info tip text of the shortcut's target.
QITIPF_LINKUSETARGET = 0x00000004,
/// Search the entire namespace for the information. This value can result in a delayed response time.
QITIPF_USESLOWTIP = 0x00000008, // Flag says it's OK to take a long time generating tip
/// Windows Vista and later. Put the info tip on a single line.
QITIPF_SINGLELINE = 0x00000010,
}
///
/// Indicates the interpretation of the data passed by SHAddToRecentDocs in its pv parameter to identify the item whose usage statistics are being tracked.
///
[PInvokeData("Shlobj.h", MSDNShortId = "dd378453")]
public enum SHARD
{
///
/// Windows 7 and later. The pv parameter points to a SHARDAPPIDINFO structure that pairs an IShellItem that identifies the item with an
/// AppUserModelID that associates it with a particular process or application.
///
SHARD_APPIDINFO = 4,
///
/// Windows 7 and later. The pv parameter points to a SHARDAPPIDINFOIDLIST structure that pairs an absolute PIDL that identifies the item with
/// an AppUserModelID that associates it with a particular process or application.
///
SHARD_APPIDINFOIDLIST = 5,
///
/// Windows 7 and later. The pv parameter points to a SHARDAPPIDINFOLINK structure that pairs an IShellLink that identifies the item with an
/// AppUserModelID that associates it with a particular process or application.
///
SHARD_APPIDINFOLINK = 7,
/// Windows 7 and later. The pv parameter is an interface pointer to an IShellLink object.
SHARD_LINK = 6,
/// The pv parameter points to a null-terminated ANSI string with the path and file name of the object.
SHARD_PATHA = 2,
/// The pv parameter points to a null-terminated Unicode string with the path and file name of the object.
SHARD_PATHW = 3,
/// The pv parameter points to a PIDL that identifies the document's file object. PIDLs that identify non-file objects are not accepted.
SHARD_PIDL = 1,
/// Windows 7 and later. The pv parameter is an interface pointer to an IShellItem object.
SHARD_SHELLITEM = 8
}
/// Receives a value that determines what type the item is in .
[PInvokeData("Shlobj_core.h", MSDNShortId = "bb759775")]
public enum SHDID
{
/// The item is a registered item on the desktop.
SHDID_ROOT_REGITEM = 1,
/// The item is a file.
SHDID_FS_FILE = 2,
/// The item is a folder.
SHDID_FS_DIRECTORY = 3,
/// The item is an unidentified item in the file system.
SHDID_FS_OTHER = 4,
/// The item is a 3.5-inch floppy drive.
SHDID_COMPUTER_DRIVE35 = 5,
/// The item is a 5.25-inch floppy drive.
SHDID_COMPUTER_DRIVE525 = 6,
/// The item is a removable disk.
SHDID_COMPUTER_REMOVABLE = 7,
/// The item is a fixed hard disk.
SHDID_COMPUTER_FIXED = 8,
/// The item is a drive that is mapped to a network share.
SHDID_COMPUTER_NETDRIVE = 9,
/// The item is a CD-ROM drive.
SHDID_COMPUTER_CDROM = 10,
/// The item is a RAM disk.
SHDID_COMPUTER_RAMDISK = 11,
/// The item is an unidentified system device.
SHDID_COMPUTER_OTHER = 12,
/// The item is a network domain.
SHDID_NET_DOMAIN = 13,
/// The item is a network server.
SHDID_NET_SERVER = 14,
/// The item is a network share.
SHDID_NET_SHARE = 15,
/// Not currently used.
SHDID_NET_RESTOFNET = 16,
/// The item is an unidentified network resource.
SHDID_NET_OTHER = 17,
/// Windows XP and later. Not currently used.
SHDID_COMPUTER_IMAGING = 18,
/// Windows XP and later. Not currently used.
SHDID_COMPUTER_AUDIO = 19,
/// Windows XP and later. The item is the system shared documents folder.
SHDID_COMPUTER_SHAREDDOCS = 20,
/// Windows Vista and later. The item is a mobile device, such as a personal digital assistant (PDA).
SHDID_MOBILE_DEVICE = 21,
}
/// The format in which the data is being requested.
[PInvokeData("Shlobj.h", MSDNShortId = "bb762174")]
public enum SHGetDataFormat
{
/// Format used for file system objects. The pv parameter is the address of a WIN32_FIND_DATA structure.
[CorrespondingType(typeof(WIN32_FIND_DATA), CorrepsondingAction.Get)]
SHGDFIL_FINDDATA = 1,
/// Format used for network resources. The pv parameter is the address of a NETRESOURCE structure.
// TODO: Define NETRESOURCE (https://msdn.microsoft.com/en-us/library/windows/desktop/aa385353(v=vs.85).aspx)
//[CorrespondingType(typeof(NETRESOURCE), CorrepsondingAction.Get)]
SHGDFIL_NETRESOURCE = 2,
/// Version 4.71. Format used for network resources. The pv parameter is the address of an SHDESCRIPTIONID structure.
[CorrespondingType(typeof(SHDESCRIPTIONID), CorrepsondingAction.Get)]
SHGDFIL_DESCRIPTIONID = 3
}
/// Flags used by .
[PInvokeData("Shlobj.h", MSDNShortId = "bb762181")]
public enum SHGFP
{
/// Retrieve the folder's current path.
SHGFP_TYPE_CURRENT = 0,
/// Retrieve the folder's default path.
SHGFP_TYPE_DEFAULT = 1
}
/// Used by SHGetImageList.
[PInvokeData("Shlobj.h", MSDNShortId = "bb762185")]
public enum SHIL
{
///
/// The image size is normally 32x32 pixels. However, if the Use large icons option is selected from the Effects section of the Appearance tab in
/// Display Properties, the image is 48x48 pixels.
///
SHIL_LARGE = 0,
/// These images are the Shell standard small icon size of 16x16, but the size can be customized by the user.
SHIL_SMALL = 1,
/// These images are the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user.
SHIL_EXTRALARGE = 2,
/// These images are the size specified by GetSystemMetrics called with SM_CXSMICON and GetSystemMetrics called with SM_CYSMICON.
SHIL_SYSSMALL = 3,
/// Windows Vista and later. The image is normally 256x256 pixels.
SHIL_JUMBO = 4,
}
///
/// CSIDL (constant special item ID list) values provide a unique system-independent way to identify special folders used frequently by applications, but
/// which may not have the same name or location on any given system. For example, the system folder may be "C:\Windows" on one system and "C:\Winnt" on
/// another. These constants are defined in Shlobj.h.
///
[PInvokeData("Shlobj.h", MSDNShortId = "bb762494")]
internal enum CSIDL
{
CSIDL_ADMINTOOLS = 0x0030,
CSIDL_CDBURN_AREA = 0x003b,
CSIDL_COMMON_ADMINTOOLS = 0x002f,
CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019,
CSIDL_COMMON_DOCUMENTS = 0x002e,
CSIDL_COMMON_MUSIC = 0x0035,
CSIDL_COMMON_OEM_LINKS = 0x003a,
CSIDL_COMMON_PICTURES = 0x0036,
CSIDL_COMMON_PROGRAMS = 0X0017,
CSIDL_COMMON_STARTMENU = 0x0016,
CSIDL_COMMON_STARTUP = 0x0018,
CSIDL_COMMON_TEMPLATES = 0x002d,
CSIDL_COMMON_VIDEO = 0x0037,
CSIDL_FLAG_CREATE = 0x8000, // force folder creation in SHGetFolderPath
CSIDL_FLAG_DONT_VERIFY = 0x4000, // return an unverified folder path
CSIDL_FONTS = 0x0014, // windows\fonts
CSIDL_MYVIDEO = 0x000e, // "My Videos" folder
CSIDL_NETHOOD = 0x0013, // %APPDATA%\Microsoft\Windows\Network Shortcuts
CSIDL_PRINTHOOD = 0x001b, // %APPDATA%\Microsoft\Windows\Printer Shortcuts
CSIDL_PROFILE = 0x0028, // %USERPROFILE% (%SystemDrive%\Users\%USERNAME%)
CSIDL_PROGRAM_FILES_COMMONX86 = 0x002c, // x86 Program Files\Common on RISC
CSIDL_PROGRAM_FILESX86 = 0x002a, // x86 C:\Program Files on RISC
CSIDL_RESOURCES = 0x0038, // %windir%\Resources
CSIDL_RESOURCES_LOCALIZED = 0x0039, // %windir%\resources\0409 (code page)
CSIDL_SYSTEMX86 = 0x0029, // %windir%\system32
CSIDL_WINDOWS = 0x0024, // GetWindowsDirectory()
}
///
/// Notifies the system that an item has been accessed, for the purposes of tracking those items used most recently and most frequently. This function
/// can also be used to clear all usage data.
///
/// A value from the SHARD enumeration that indicates the form of the information pointed to by the pv parameter.
///
/// A pointer to data that identifies the item that has been accessed. The item can be specified in this parameter in one of the following forms:
///
/// - A null-terminated string that contains the path and file name of the item.
/// - A PIDL that identifies the item's file object.
/// -
/// Windows 7 and later only. A SHARDAPPIDINFO, SHARDAPPIDINFOIDLIST, or SHARDAPPIDINFOLINK structure that identifies the item through an
/// AppUserModelID. See Application User Model IDs (AppUserModelIDs) for more information.
///
/// - Windows 7 and later only. An IShellLink object that identifies the item through a shortcut.
///
/// Set this parameter to NULL to clear all usage data on all items.
///
[DllImport(Lib.Shell32, ExactSpelling = true)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762105")]
public static extern void SHAddToRecentDocs(SHARD uFlags, IShellLinkW pv);
///
/// Notifies the system that an item has been accessed, for the purposes of tracking those items used most recently and most frequently. This function
/// can also be used to clear all usage data.
///
/// A value from the SHARD enumeration that indicates the form of the information pointed to by the pv parameter.
///
/// A pointer to data that identifies the item that has been accessed. The item can be specified in this parameter in one of the following forms:
///
/// - A null-terminated string that contains the path and file name of the item.
/// - A PIDL that identifies the item's file object.
/// -
/// Windows 7 and later only. A SHARDAPPIDINFO, SHARDAPPIDINFOIDLIST, or SHARDAPPIDINFOLINK structure that identifies the item through an
/// AppUserModelID. See Application User Model IDs (AppUserModelIDs) for more information.
///
/// - Windows 7 and later only. An IShellLink object that identifies the item through a shortcut.
///
/// Set this parameter to NULL to clear all usage data on all items.
///
[DllImport(Lib.Shell32, ExactSpelling = true)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762105")]
public static extern void SHAddToRecentDocs(SHARD uFlags, [MarshalAs(UnmanagedType.LPWStr)] string pv);
///
/// Notifies the system that an item has been accessed, for the purposes of tracking those items used most recently and most frequently. This function
/// can also be used to clear all usage data.
///
/// A value from the SHARD enumeration that indicates the form of the information pointed to by the pv parameter.
///
/// A pointer to data that identifies the item that has been accessed. The item can be specified in this parameter in one of the following forms:
///
/// - A null-terminated string that contains the path and file name of the item.
/// - A PIDL that identifies the item's file object.
/// -
/// Windows 7 and later only. A SHARDAPPIDINFO, SHARDAPPIDINFOIDLIST, or SHARDAPPIDINFOLINK structure that identifies the item through an
/// AppUserModelID. See Application User Model IDs (AppUserModelIDs) for more information.
///
/// - Windows 7 and later only. An IShellLink object that identifies the item through a shortcut.
///
/// Set this parameter to NULL to clear all usage data on all items.
///
[DllImport(Lib.Shell32, ExactSpelling = true)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762105")]
public static extern void SHAddToRecentDocs(SHARD uFlags, PIDL pv);
/// Displays a dialog box that enables the user to select a Shell folder.
/// A pointer to a BROWSEINFO structure that contains information used to display the dialog box.
///
/// Returns a PIDL that specifies the location of the selected folder relative to the root of the namespace. If the user chooses the Cancel button in the
/// dialog box, the return value is NULL.
///
[DllImport(Lib.Shell32, CharSet = CharSet.Auto)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762115")]
public static extern PIDL SHBrowseForFolder(ref BROWSEINFO lpbi);
/// Provides a default handler to extract an icon from a file.
/// A pointer to a null-terminated buffer that contains the path and name of the file from which the icon is extracted.
///
/// The location of the icon within the file named in pszIconFile. If this is a positive number, it refers to the zero-based position of the icon in the
/// file. For instance, 0 refers to the 1st icon in the resource file and 2 refers to the 3rd. If this is a negative number, it refers to the icon's
/// resource ID.
///
/// A flag that controls the icon extraction.
///
/// A pointer to an HICON that, when this function returns successfully, receives the handle of the large version of the icon specified in the LOWORD of
/// nIconSize. This value can be NULL.
///
///
/// A pointer to an HICON that, when this function returns successfully, receives the handle of the small version of the icon specified in the HIWORD of nIconSize.
///
///
/// A value that contains the large icon size in its LOWORD and the small icon size in its HIWORD. Size is measured in pixels. Pass 0 to specify default
/// large and small sizes.
///
[DllImport(Lib.Shell32, CharSet = CharSet.Auto)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762149")]
public static extern HRESULT SHDefExtractIcon(string pszIconFile, int iIndex, uint uFlags, ref IntPtr phiconLarge,
ref IntPtr phiconSmall, uint nIconSize);
/// Provides a default handler to extract an icon from a file.
/// A pointer to a null-terminated buffer that contains the path and name of the file from which the icon is extracted.
///
/// The location of the icon within the file named in pszIconFile. If this is a positive number, it refers to the zero-based position of the icon in the
/// file. For instance, 0 refers to the 1st icon in the resource file and 2 refers to the 3rd. If this is a negative number, it refers to the icon's
/// resource ID.
///
/// A flag that controls the icon extraction.
///
/// A pointer to an HICON that, when this function returns successfully, receives the handle of the large version of the icon specified in the LOWORD of
/// nIconSize. This value can be NULL.
///
///
/// A pointer to an HICON that, when this function returns successfully, receives the handle of the small version of the icon specified in the HIWORD of nIconSize.
///
///
/// A value that contains the large icon size in its LOWORD and the small icon size in its HIWORD. Size is measured in pixels. Pass 0 to specify default
/// large and small sizes.
///
[DllImport(Lib.Shell32, CharSet = CharSet.Auto)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762149")]
public static extern HRESULT SHDefExtractIcon(string pszIconFile, int iIndex, uint uFlags, IntPtr phiconLarge,
ref IntPtr phiconSmall, uint nIconSize);
/// Retrieves extended property data from a relative identifier list.
///
/// The address of the parent IShellFolder interface. This must be the immediate parent of the ITEMIDLIST structure referenced by the pidl parameter.
///
/// A pointer to an ITEMIDLIST structure that identifies the object relative to the folder specified in psf.
/// The format in which the data is being requested.
///
/// A pointer to a buffer that, when this function returns successfully, receives the requested data. The format of this buffer is determined by nFormat.
///
/// If nFormat is SHGDFIL_NETRESOURCE, there are two possible cases. If the buffer is large enough, the net resource's string information (fields for the
/// network name, local name, provider, and comments) will be placed into the buffer. If the buffer is not large enough, only the net resource structure
/// will be placed into the buffer and the string information pointers will be NULL.
///
///
/// Size of the buffer at pv, in bytes.
///
/// This function extracts only information that is present in the pointer to an item identifier list (PIDL). Since the content of a PIDL depends on the
/// folder object that created the PIDL, there is no guarantee that all requested information will be available. In addition, the information that is
/// returned reflects the state of the object at the time the PIDL was created. The current state of the object could be different. For example, if you
/// set nFormat to SHGDFIL_FINDDATA, the function might assign meaningful values to only some of the members of the WIN32_FIND_DATA structure. The
/// remaining members will be set to zero. To retrieve complete current information on a file system file or folder, use standard file system functions
/// such as GetFileTime or FindFirstFile.
///
/// E_INVALIDARG is returned if the psf, pidl, pv, or cb parameter does not match the nFormat parameter, or if nFormat is not one of the specific
/// SHGDFIL_ values shown above.
///
///
[DllImport(Lib.Shell32, CharSet = CharSet.Auto)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762174")]
public static extern HRESULT SHGetDataFromIDList([In, MarshalAs(UnmanagedType.Interface)]
IShellFolder psf, [In] PIDL pidl, SHGetDataFormat nFormat, [In, Out] IntPtr pv, int cb);
/// Retrieves the IShellFolder interface for the desktop folder, which is the root of the Shell's namespace.
///
/// When this method returns, receives an IShellFolder interface pointer for the desktop folder. The calling application is responsible for eventually
/// freeing the interface by calling its IUnknown::Release method.
///
/// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
[DllImport(Lib.Shell32, CharSet = CharSet.Unicode, ExactSpelling = true)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762175")]
public static extern HRESULT SHGetDesktopFolder([MarshalAs(UnmanagedType.Interface)] out IShellFolder ppv);
/// Deprecated. Retrieves the path of a folder as an ITEMIDLIST structure.
/// Reserved.
///
/// A CSIDL value that identifies the folder to be located. The folders associated with the CSIDLs might not exist on a particular system.
///
///
/// An access token that can be used to represent a particular user. It is usually set to NULL, but it may be needed when there are multiple users for
/// those folders that are treated as belonging to a single user. The most commonly used folder of this type is My Documents. The calling application is
/// responsible for correct impersonation when hToken is non-NULL. It must have appropriate security privileges for the particular user, and the user's
/// registry hive must be currently mounted.
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetFolderLocation to find folder locations (such as
/// the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as My Documents and Desktop. Any items added to the Default User folder also appear in any new user account.
///
///
/// Reserved.
///
/// The address of a pointer to an item identifier list structure that specifies the folder's location relative to the root of the namespace (the
/// desktop). The ppidl parameter is set to NULL on failure. The calling application is responsible for freeing this resource by calling CoTaskMemFree.
///
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762180")]
public static extern HRESULT SHGetFolderLocation(IntPtr hwndOwner, int nFolder, SafeTokenHandle hToken,
int dwReserved, out PIDL ppidl);
///
/// Deprecated. Gets the path of a folder identified by a CSIDL value. As of Windows Vista, this function is merely a wrapper for
/// SHGetKnownFolderPath. The CSIDL value is translated to its associated KNOWNFOLDERID and then SHGetKnownFolderPath is called. New applications should
/// use the known folder system rather than the older CSIDL system, which is supported only for backward compatibility.
///
/// Reserved.
///
/// A CSIDL value that identifies the folder whose path is to be retrieved. Only real folders are valid. If a virtual folder is specified, this function
/// fails. You can force creation of a folder by combining the folder's CSIDL with CSIDL_FLAG_CREATE.
///
///
/// An access token that represents a particular user. If this parameter is NULL, which is the most common usage, the function requests the known folder
/// for the current user.
///
/// Request a specific user's folder by passing the hToken of that user. This is typically done in the context of a service that has sufficient
/// privileges to retrieve the token of a given user. That token must be opened with TOKEN_QUERY and TOKEN_IMPERSONATE rights. In some cases, you also
/// need to include TOKEN_DUPLICATE. In addition to passing the user's hToken, the registry hive of that specific user must be mounted. See Access
/// Control for further discussion of access control issues.
///
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderPath to find folder locations (such
/// as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the
/// Default User folders requires administrator privileges.
///
///
///
/// Flags that specify the path to be returned. This value is used in cases where the folder associated with a KNOWNFOLDERID (or CSIDL) can be moved,
/// renamed, redirected, or roamed across languages by a user or administrator.
///
/// The known folder system that underlies SHGetFolderPath allows users or administrators to redirect a known folder to a location that suits their
/// needs. This is achieved by calling IKnownFolderManager::Redirect, which sets the "current" value of the folder associated with the SHGFP_TYPE_CURRENT flag.
///
///
/// The default value of the folder, which is the location of the folder if a user or administrator had not redirected it elsewhere, is retrieved by
/// specifying the SHGFP_TYPE_DEFAULT flag. This value can be used to implement a "restore defaults" feature for a known folder.
///
///
/// For example, the default value (SHGFP_TYPE_DEFAULT) for FOLDERID_Music (CSIDL_MYMUSIC) is "C:\Users\user name\Music". If the folder was redirected,
/// the current value (SHGFP_TYPE_CURRENT) might be "D:\Music". If the folder has not been redirected, then SHGFP_TYPE_DEFAULT and SHGFP_TYPE_CURRENT
/// retrieve the same path.
///
///
///
/// A pointer to a null-terminated string of length MAX_PATH which will receive the path. If an error occurs or S_FALSE is returned, this string will be
/// empty. The returned path does not include a trailing backslash. For example, "C:\Users" is returned rather than "C:\Users\".
///
[DllImport(Lib.Shell32, CharSet = CharSet.Auto)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762181")]
public static extern HRESULT SHGetFolderPath(IntPtr hwndOwner, int nFolder, [In, Optional] SafeTokenHandle hToken,
SHGFP dwFlags, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPath);
///
/// Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID. This extends SHGetKnownFolderPath by allowing you to set the
/// initial size of the string buffer.
///
/// A reference to the KNOWNFOLDERID that identifies the folder.
/// Flags that specify special retrieval options. This value can be 0; otherwise, one or more of the KNOWN_FOLDER_FLAG values.
///
/// An access token that represents a particular user. If this parameter is NULL, which is the most common usage, the function requests the known folder
/// for the current user.
///
/// Request a specific user's folder by passing the hToken of that user. This is typically done in the context of a service that has sufficient
/// privileges to retrieve the token of a given user. That token must be opened with TOKEN_QUERY and TOKEN_IMPERSONATE rights. In some cases, you also
/// need to include TOKEN_DUPLICATE. In addition to passing the user's hToken, the registry hive of that specific user must be mounted. See Access
/// Control for further discussion of access control issues.
///
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderPath to find folder locations (such
/// as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the
/// Default User folders requires administrator privileges.
///
///
///
/// A null-terminated, Unicode string. This buffer must be of size cchPath. When SHGetFolderPathEx returns successfully, this parameter contains the path
/// for the known folder.
///
/// The size of the ppszPath buffer, in characters.
[DllImport(Lib.Shell32, CharSet = CharSet.Unicode, ExactSpelling = true)]
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[PInvokeData("Shlobj.h", MSDNShortId = "mt757093")]
public static extern HRESULT SHGetFolderPathEx([In, MarshalAs(UnmanagedType.LPStruct)]
Guid rfid, KNOWN_FOLDER_FLAG dwFlags, [In, Optional] SafeTokenHandle hToken,
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPath, uint cchPath);
/// Retrieves the pointer to an item identifier list (PIDL) of an object.
/// A pointer to the IUnknown of the object from which to get the PIDL.
/// When this function returns, contains a pointer to the PIDL of the given object.
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762184")]
public static extern HRESULT SHGetIDListFromObject([MarshalAs(UnmanagedType.IUnknown)] object iUnknown,
out PIDL ppidl);
/// Retrieves an image list.
/// The image type contained in the list.
/// Reference to the image list interface identifier, normally IID_IImageList.
/// When this method returns, contains the interface pointer requested in riid. This is typically IImageList.
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762185")]
public static extern HRESULT SHGetImageList(SHIL iImageList, [MarshalAs(UnmanagedType.LPStruct)] Guid riid,
out IImageList ppv);
/// Retrieves the path of a known folder as an ITEMIDLIST structure.
///
/// A reference to the KNOWNFOLDERID that identifies the folder. The folders associated with the known folder IDs might not exist on a particular system.
///
///
/// Flags that specify special retrieval options. This value can be 0; otherwise, it is one or more of the KNOWN_FOLDER_FLAG values.
///
///
/// An access token used to represent a particular user. This parameter is usually set to NULL, in which case the function tries to access the current
/// user's instance of the folder. However, you may need to assign a value to hToken for those folders that can have multiple users but are treated as
/// belonging to a single user. The most commonly used folder of this type is Documents.
///
/// The calling application is responsible for correct impersonation when hToken is non-null. It must have appropriate security privileges for the
/// particular user, including TOKEN_QUERY and TOKEN_IMPERSONATE, and the user's registry hive must be currently mounted. See Access Control for further
/// discussion of access control issues.
///
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderIDList to find folder locations (such
/// as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the
/// Default User folders requires administrator privileges.
///
///
///
/// When this method returns, contains a pointer to the PIDL of the folder. This parameter is passed uninitialized. The caller is responsible for freeing
/// the returned PIDL when it is no longer needed by calling ILFree.
///
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762187")]
public static extern HRESULT SHGetKnownFolderIDList([In, MarshalAs(UnmanagedType.LPStruct)]
Guid rfid, KNOWN_FOLDER_FLAG dwFlags, [In, Optional] SafeTokenHandle hToken, out PIDL ppidl);
/// Retrieves an IShellItem object that represents a known folder.
/// A reference to the KNOWNFOLDERID, a GUID that identifies the folder that contains the item.
///
/// Flags that specify special options used in the retrieval of the known folder IShellItem. This value can be KF_FLAG_DEFAULT; otherwise, one or more of
/// the KNOWN_FOLDER_FLAG values.
///
///
/// An access token used to represent a particular user. This parameter is usually set to NULL, in which case the function tries to access the current
/// user's instance of the folder. However, you may need to assign a value to hToken for those folders that can have multiple users but are treated as
/// belonging to a single user. The most commonly used folder of this type is Documents.
///
/// The calling application is responsible for correct impersonation when hToken is non-null. It must have appropriate security privileges for the
/// particular user, including TOKEN_QUERY and TOKEN_IMPERSONATE, and the user's registry hive must be currently mounted. See Access Control for further
/// discussion of access control issues.
///
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderIDList to find folder locations (such
/// as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the
/// Default User folders requires administrator privileges.
///
///
/// A reference to the IID of the interface that represents the item, usually IID_IShellItem or IID_IShellItem2.
/// When this method returns, contains the interface pointer requested in riid.
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "dd378429")]
public static extern HRESULT SHGetKnownFolderItem([In, MarshalAs(UnmanagedType.LPStruct)]
Guid rfid, KNOWN_FOLDER_FLAG dwFlags, [In, Optional] SafeTokenHandle hToken,
[In, MarshalAs(UnmanagedType.LPStruct)]
Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);
/// Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID.
/// A reference to the KNOWNFOLDERID that identifies the folder.
/// Flags that specify special retrieval options. This value can be 0; otherwise, one or more of the KNOWN_FOLDER_FLAG values.
///
/// An access token that represents a particular user. If this parameter is NULL, which is the most common usage, the function requests the known folder
/// for the current user.
///
/// Request a specific user's folder by passing the hToken of that user. This is typically done in the context of a service that has sufficient
/// privileges to retrieve the token of a given user. That token must be opened with TOKEN_QUERY and TOKEN_IMPERSONATE rights. In some cases, you also
/// need to include TOKEN_DUPLICATE. In addition to passing the user's hToken, the registry hive of that specific user must be mounted. See Access
/// Control for further discussion of access control issues.
///
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderPath to find folder locations (such
/// as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the
/// Default User folders requires administrator privileges.
///
///
///
/// When this method returns, contains the address of a pointer to a null-terminated Unicode string that specifies the path of the known folder. The
/// calling process is responsible for freeing this resource once it is no longer needed by calling CoTaskMemFree. The returned path does not include a
/// trailing backslash. For example, "C:\Users" is returned rather than "C:\Users\".
///
/// Returns S_OK if successful, or an error value otherwise.
/// This function replaces SHGetFolderPath. That older function is now simply a wrapper for SHGetKnownFolderPath.
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762188")]
public static extern HRESULT SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
KNOWN_FOLDER_FLAG dwFlags, [In, Optional] SafeTokenHandle hToken, out SafeCoTaskMemHandle pszPath);
/// Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID.
/// A reference to the KNOWNFOLDERID that identifies the folder.
/// Flags that specify special retrieval options. This value can be 0; otherwise, one or more of the KNOWN_FOLDER_FLAG values.
///
/// An access token that represents a particular user. If this parameter is NULL, which is the most common usage, the function requests the known folder
/// for the current user.
///
/// Request a specific user's folder by passing the hToken of that user. This is typically done in the context of a service that has sufficient
/// privileges to retrieve the token of a given user. That token must be opened with TOKEN_QUERY and TOKEN_IMPERSONATE rights. In some cases, you also
/// need to include TOKEN_DUPLICATE. In addition to passing the user's hToken, the registry hive of that specific user must be mounted. See Access
/// Control for further discussion of access control issues.
///
///
/// Assigning the hToken parameter a value of -1 indicates the Default User. This allows clients of SHGetKnownFolderPath to find folder locations (such
/// as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special
/// folders such as Documents and Desktop. Any items added to the Default User folder also appear in any new user account. Note that access to the
/// Default User folders requires administrator privileges.
///
///
/// String that specifies the path of the known folder.
/// This function replaces SHGetFolderPath. That older function is now simply a wrapper for SHGetKnownFolderPath.
[PInvokeData("Shlobj.h", MSDNShortId = "bb762188")]
public static string SHGetKnownFolderPath(KNOWNFOLDERID id, KNOWN_FOLDER_FLAG dwFlags, SafeTokenHandle hToken = null)
{
SHGetKnownFolderPath(id.Guid(), dwFlags, hToken ?? SafeTokenHandle.Null, out SafeCoTaskMemHandle path);
return path.ToString(-1);
}
/// Retrieves the display name of an item identified by its IDList.
/// A PIDL that identifies the item.
/// A value from the SIGDN enumeration that specifies the type of display name to retrieve.
/// A value that, when this function returns successfully, receives the address of a pointer to the retrieved display name.
[DllImport(Lib.Shell32, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762191")]
public static extern HRESULT SHGetNameFromIDList(PIDL pidl, SIGDN sigdnName, out SafeCoTaskMemHandle ppszName);
/// Converts an item identifier list to a file system path.
///
/// The address of an item identifier list that specifies a file or directory location relative to the root of the namespace (the desktop).
///
/// The address of a buffer to receive the file system path. This buffer must be at least MAX_PATH characters in size.
/// Returns TRUE if successful; otherwise, FALSE.
[DllImport(Lib.Shell32, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762194")]
public static extern bool SHGetPathFromIDList(PIDL pidl, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder pszPath);
/// Opens a Windows Explorer window with specified items in a particular folder selected.
/// A pointer to a fully qualified item ID list that specifies the folder.
///
/// A count of items in the selection array, apidl. If cidl is zero, then pidlFolder must point to a fully specified ITEMIDLIST describing a single item
/// to select. This function opens the parent folder and selects that item.
///
/// A pointer to an array of PIDL structures, each of which is an item to select in the target folder referenced by pidlFolder.
/// The optional flags. Under Windows XP this parameter is ignored. In Windows Vista, the following flags are defined.
[DllImport(Lib.Shell32, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762232")]
public static extern HRESULT SHOpenFolderAndSelectItems(PIDL pidlFolder, uint cidl,
[In, Optional, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
PIDL[] apidl, OFASI dwFlags);
///
/// Translates a Shell namespace object's display name into an item identifier list and returns the attributes of the object. This function is the
/// preferred method to convert a string to a pointer to an item identifier list (PIDL).
///
/// A pointer to a zero-terminated wide string that contains the display name to parse.
/// A bind context that controls the parsing operation. This parameter is normally set to NULL.
///
/// The address of a pointer to a variable of type ITEMIDLIST that receives the item identifier list for the object. If an error occurs, then this
/// parameter is set to NULL.
///
///
/// A ULONG value that specifies the attributes to query. To query for one or more attributes, initialize this parameter with the flags that represent
/// the attributes of interest. For a list of available SFGAO flags, see IShellFolder::GetAttributesOf.
///
///
/// A pointer to a ULONG. On return, those attributes that are true for the object and were requested in sfgaoIn are set. An object's attribute flags can
/// be zero or a combination of SFGAO flags. For a list of available SFGAO flags, see IShellFolder::GetAttributesOf.
///
[DllImport(Lib.Shell32, CharSet = CharSet.Unicode, ExactSpelling = true)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb762236")]
public static extern HRESULT SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string pszName,
[In, Optional] IntPtr pbc, out PIDL ppidl, SFGAO sfgaoIn, out SFGAO psfgaoOut);
///
/// Defines the coordinates of a character cell in a console screen buffer. The origin of the coordinate system (0,0) is at the top, left cell of the buffer.
///
[PInvokeData("wincon.h")]
[StructLayout(LayoutKind.Sequential, Pack = 2)]
public struct COORD
{
/// The horizontal coordinate or column value. The units depend on the function call.
public short X;
/// The vertical coordinate or row value. The units depend on the function call.
public short Y;
}
/// Serves as the header for some of the extra data structures used by IShellLinkDataList.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773249")]
public struct DATABLOCKHEADER
{
/// The size of the extra data block.
public uint cbSize;
/// A signature that identifies the type of data block that follows the header.
public ShellDataBlockSignature dwSignature;
}
/// Holds an extra data block used by IShellLinkDataList. It holds the link's Windows Installer ID.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773274")]
public struct EXP_DARWIN_LINK
{
///
/// DATABLOCK_HEADER structure stating the size and signature of the EXP_DARWIN_LINK structure. The following is the only recognized signature value: EXP_DARWIN_ID_SIG
///
public DATABLOCKHEADER dbh;
/// The link's ID in the form of an ANSI string.
[MarshalAs(UnmanagedType.LPStr, SizeConst = MAX_PATH)]
public string szDarwinID;
/// The link's ID in the form of an Unicode string.
[MarshalAs(UnmanagedType.LPWStr, SizeConst = MAX_PATH)]
public string szwDarwinID;
}
/// Holds an extra data block used by IShellLinkDataList. It holds special folder information.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773279")]
public struct EXP_SPECIAL_FOLDER
{
/// The size of the EXP_SPECIAL_FOLDER structure.
public uint cbSize;
/// The structure's signature. It should be set to EXP_SPECIAL_FOLDER_SIG.
public ShellDataBlockSignature dwSignature;
/// The ID of the special folder that the link points into.
public uint idSpecialFolder;
/// The offset into the saved PIDL.
public uint cbOffset;
}
/// Holds an extra data block used by IShellLinkDataList. It holds expandable environment strings for the icon or target.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773282")]
public struct EXP_SZ_LINK
{
/// The size of the EXP_SZ_LINK structure.
public uint cbSize;
///
/// The structure's signature. It can have one of the following values: EXP_SZ_LINK_SIG = Contains the link's target path; EXP_SZ_ICON_SIG = Contains
/// the links icon path.
///
public ShellDataBlockSignature dwSignature;
/// The null-terminated ANSI string with the path of the target or icon.
[MarshalAs(UnmanagedType.LPStr, SizeConst = MAX_PATH)]
public string szTarget;
/// The null-terminated Unicode string with the path of the target or icon.
[MarshalAs(UnmanagedType.LPWStr, SizeConst = MAX_PATH)]
public string swzTarget;
}
/// Holds an extra data block used by IShellLinkDataList. It holds console properties.
[StructLayout(LayoutKind.Sequential, Pack = 2)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773359")]
public struct NT_CONSOLE_PROPS
{
///
/// The DATABLOCK_HEADER structure with the NT_CONSOLE_PROPS structure's size and signature. The signature for an NT_CONSOLE_PROPS structure is NT_CONSOLE_PROPS_SIG.
///
public DATABLOCKHEADER dbh;
/// Fill attribute for the console.
public ushort wFillAttribute;
/// Fill attribute for console pop-ups.
public ushort wPopupFillAttribute;
/// A COORD structure with the console's screen buffer size.
public COORD dwScreenBufferSize;
/// A COORD structure with the console's window size.
public COORD dwWindowSize;
/// A COORD structure with the console's window origin.
public COORD dwWindowOrigin;
/// The font.
public uint nFont;
/// The input buffer size.
public uint nInputBufferSize;
/// A COORD structure with the font size.
public COORD dwFontSize;
/// The font family/
public uint uFontFamily;
/// The font weight.
public uint uFontWeight;
/// A character array that contains the font's face name.
[MarshalAs(UnmanagedType.LPWStr, SizeConst = LF_FACESIZE)]
public string FaceName;
/// The cursor size.
public uint uCursorSize;
/// A boolean value that is set to TRUE if the console is in full-screen mode, or FALSE otherwise.
[MarshalAs(UnmanagedType.Bool)] public bool bFullScreen;
/// A boolean value that is set to TRUE if the console is in quick-edit mode, or FALSE otherwise.
[MarshalAs(UnmanagedType.Bool)] public bool bQuickEdit;
/// A boolean value that is set to TRUE if the console is in insert mode, or FALSE otherwise.
[MarshalAs(UnmanagedType.Bool)] public bool bInsertMode;
/// A boolean value that is set to TRUE if the console is in auto-position mode, or FALSE otherwise.
[MarshalAs(UnmanagedType.Bool)] public bool bAutoPosition;
/// The size of the history buffer.
public uint uHistoryBufferSize;
/// The number of history buffers.
public uint uNumberOfHistoryBuffers;
/// A boolean value that is set to TRUE if old duplicate history lists should be discarded, or FALSE otherwise.
[MarshalAs(UnmanagedType.Bool)] public bool bHistoryNoDup;
/// An array of COLORREF values with the console's color settings.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public uint[] ColorTable;
}
/// Holds an extra data block used by IShellLinkDataList. It holds the console's code page.
[StructLayout(LayoutKind.Sequential, Pack = 2)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773362")]
public struct NT_FE_CONSOLE_PROPS
{
///
/// The DATABLOCK_HEADER structure with the NT_FE_CONSOLE_PROPS structure's size and signature. The signature for an NT_FE_CONSOLE_PROPS structure is NT_FE_CONSOLE_PROPS_SIG.
///
public DATABLOCKHEADER dbh;
/// The console's code page.
public uint uCodePage;
}
/// Receives item data in response to a call to SHGetDataFromIDList.
[StructLayout(LayoutKind.Sequential)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb759775")]
public struct SHDESCRIPTIONID
{
/// Receives a value that determines what type the item is.
public SHDID dwDescriptionId;
/// Receives the CLSID of the object to which the item belongs.
public Guid clsid;
}
/*[StructLayout(LayoutKind.Sequential)]
[PInvokeData("Shlobj.h", MSDNShortId = "bb773399")]
public struct SFV_CREATE
{
public uint cbSize;
[MarshalAs(UnmanagedType.Interface)] public IShellFolder pshf;
[MarshalAs(UnmanagedType.Interface)] public IShellView psvOuter;
[MarshalAs(UnmanagedType.Interface)] public IShellFolderViewCB psfbcb;
}*/
}
}