using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Text; namespace Vanara.PInvoke { public static partial class ShlwApi { /// /// Truncates a path to fit within a certain number of characters by replacing path components with ellipses. /// /// /// Type: LPTSTR /// The address of the string that has been altered. /// /// /// Type: LPCTSTR /// A pointer to a null-terminated string of length MAX_PATH that contains the path to be altered. /// /// /// Type: UINT /// /// The maximum number of characters to be contained in the new string, including the terminating null character. For example, if = /// 8, the resulting string can contain a maximum of 7 characters plus the terminating null character. /// /// /// /// Type: DWORD /// Reserved. Must be 0. /// /// /// Type: BOOL /// Returns TRUE if successful, or FALSE otherwise. /// /// /// /// The '/' separator will be used instead of '' if the original string used it. If points to a file name that is too long, instead /// of a path, the file name will be truncated to characters, including the ellipsis and the terminating NULL character. For /// example, if the input file name is "My Filename" and is 10, PathCompactPathEx will return "My Fil...". /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathcompactpathexa // BOOL PathCompactPathExA( LPSTR pszOut, LPCSTR pszSrc, UINT cchMax, DWORD dwFlags ); [DllImport(Lib.Shlwapi, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("shlwapi.h", MSDNShortId = "ff108ee6-3d71-4ab2-a04a-d4bcce408f88")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool PathCompactPathEx(StringBuilder pszOut, string pszSrc, uint cchMax, uint dwFlags = 0); /// Searches for a file. /// /// Type: LPTSTR /// /// A pointer to a null-terminated string of length MAX_PATH that contains the file name for which to search. If the search is successful, this parameter /// is used to return the fully qualified path name. /// /// /// /// Type: LPCTSTR* /// An optional, null-terminated array of directories to be searched first. This value can be NULL. /// /// /// Type: BOOL /// Returns TRUE if successful, or FALSE otherwise. /// // BOOL PathFindOnPath( _Inout_ LPTSTR pszFile, _In_opt_ LPCTSTR *ppszOtherDirs); // https://msdn.microsoft.com/en-us/library/windows/desktop/bb773594(v=vs.85).aspx [PInvokeData("Shlwapi.h", MSDNShortId = "bb773594")] [DllImport(Lib.Shlwapi, CharSet = CharSet.Auto, SetLastError = false)] public static extern bool PathFindOnPath(StringBuilder pszFile, [In] string[] ppszOtherDirs); /// Opens or creates a file and retrieves a stream to read or write to that file. /// A pointer to a null-terminated string that specifies the file name. /// One or more STGM values that are used to specify the file access mode and how the object that exposes the stream is created and deleted. /// One or more flag values that specify file attributes in the case that a new file is created. For a complete list of possible values, see the dwFlagsAndAttributes parameter of the CreateFile function. /// A BOOL value that helps specify, in conjunction with grfMode, how existing files should be treated when creating the stream. See Remarks for details. /// Reserved. /// Receives an IStream interface pointer for the stream associated with the file. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. [DllImport(Lib.Shlwapi, CharSet = CharSet.Unicode, ExactSpelling = true)] [PInvokeData("Shlwapi.h", MSDNShortId = "bb759866")] public static extern HRESULT SHCreateStreamOnFileEx(string pszFile, STGM grfMode, FileFlagsAndAttributes dwAttributes, [MarshalAs(UnmanagedType.Bool)] bool fCreate, [Optional] IStream pstmTemplate, out IStream ppstm); /// /// Exposes methods that simplify the process of retrieving information stored in the registry in association with defining a file /// type or protocol and associating it with an application. /// // https://msdn.microsoft.com/en-us/library/windows/desktop/bb761400(v=vs.85).aspx [PInvokeData("Shlwapi.h", MSDNShortId = "bb761400")] [ComImport, Guid("c46ca590-3c3f-11d2-bee6-0000f805ca57"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IQueryAssociations { /// /// Initializes the IQueryAssociations interface and sets the root key to the appropriate ProgID. /// /// A flag that specifies how the search is to be initialized. It is typically set to zero, but it can also take one of the following ASSOCF values. /// A Unicode string that is used to determine the root key. If a value is specified for hkProgid, set this parameter to NULL. Four types of string can be used: /// /// File name extensionA file name extension, such as .txt. /// CLSIDA CLSID GUID in the standard "{GUID}" format. /// ProgIDAn application's ProgID, such as Word.Document.8. /// Executable nameThe name of an application's .exe file. The ASSOCF_OPEN_BYEXENAME flag must be set in flags. /// /// The HKEY value of the subkey that is used as a root key. The search looks only below this key. If a value is specified for pwszAssoc, set this parameter to NULL. /// The HWND. void Init(ASSOCF flags, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, [Optional] IntPtr hkProgid, [Optional] IntPtr hwnd); /// /// Searches for and retrieves a file or protocol association-related string from the registry. /// /// A flag that can be used to control the search. It can be any combination of the following ASSOCF values. /// An ASSOCSTR value that specifies the type of string that is to be returned. /// A pointer to an optional, null-terminated Unicode string with information about the location of the string. It is typically set to a Shell verb such as open. Set this parameter to NULL if it is not used. /// A pointer to a null-terminated Unicode string used to return the requested string. Set this parameter to NULL to retrieve the required buffer size. /// A pointer to a value that, on entry, is set to the number of characters in the pwszOut buffer. When the function returns successfully, it points to the number of characters placed in the buffer. /// If the ASSOCF_NOTRUNCATE flag is set in flags and the buffer specified in pwszOut is too small, the function returns E_POINTER and pcchOut points to the required size of the buffer. /// If pwszOut is NULL, the function returns S_FALSE and pcchOut points to the required size of the buffer. void GetString(ASSOCF flags, ASSOCSTR str, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pszExtra, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszOut, ref uint pcchOut); /// /// Searches for and retrieves a file or protocol association-related key from the registry. /// /// The ASSOCF value that can be used to control the search. /// The ASSOCKEY value that specifies the type of key that is to be returned. /// A pointer to an optional, null-terminated Unicode string with information about the location of the key. It is normally set to a Shell verb such as open. Set this parameter to NULL if it is not used. /// A pointer to the key's HKEY value. void GetKey(ASSOCF flags, ASSOCKEY key, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pszExtra, out IntPtr phkeyOut); /// /// Searches for and retrieves file or protocol association-related binary data from the registry. /// /// The ASSOCF value that can be used to control the search. /// The ASSOCDATA value that specifies the type of data that is to be returned. /// A pointer to an optional, null-terminated Unicode string with information about the location of the data. It is normally set to a Shell verb such as open. Set this parameter to NULL if it is not used. /// A pointer to a value that, when this method returns successfully, receives the requested data value. /// A pointer to a value that, when this method is called, holds the size of pvOut, in bytes. When this method returns successfully, the value contains the size of the data actually retrieved. void GetData(ASSOCF flags, ASSOCDATA data, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pszExtra, IntPtr pvOut, ref uint pcbOut); /// /// This method is not implemented. /// /// Undocumented. /// Undocumented. /// Undocumented. /// Undocumented. /// Undocumented. void GetEnum(ASSOCF flags, ASSOCENUM assocenum, [MarshalAs(UnmanagedType.LPWStr)] string pszExtra, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IntPtr ppvOut); } /// Provides information to the IQueryAssociations interface methods. // typedef enum { ASSOCF_NONE = 0x00000000, ASSOCF_INIT_NOREMAPCLSID = 0x00000001, ASSOCF_INIT_BYEXENAME = 0x00000002, ASSOCF_OPEN_BYEXENAME = 0x00000002, ASSOCF_INIT_DEFAULTTOSTAR = 0x00000004, ASSOCF_INIT_DEFAULTTOFOLDER = 0x00000008, ASSOCF_NOUSERSETTINGS = 0x00000010, ASSOCF_NOTRUNCATE = 0x00000020, ASSOCF_VERIFY = 0x00000040, ASSOCF_REMAPRUNDLL = 0x00000080, ASSOCF_NOFIXUPS = 0x00000100, ASSOCF_IGNOREBASECLASS = 0x00000200, ASSOCF_INIT_IGNOREUNKNOWN = 0x00000400, ASSOCF_INIT_FIXED_PROGID = 0x00000800, ASSOCF_IS_PROTOCOL = 0x00001000, ASSOCF_INIT_FOR_FILE = 0x00002000} ASSOCF; // https://msdn.microsoft.com/en-us/library/windows/desktop/bb762471(v=vs.85).aspx [Flags] [PInvokeData("Shlwapi.h", MSDNShortId = "bb762471")] public enum ASSOCF { /// None of the following options are set. ASSOCF_NONE = 0x00000000, /// Instructs IQueryAssociations interface methods not to map CLSID values to ProgID values. ASSOCF_INIT_NOREMAPCLSID = 0x00000001, /// Identifies the value of the pwszAssoc parameter of IQueryAssociations::Init as an executable file name. If this flag is not set, the root key will be set to the ProgID associated with the .exe key instead of the executable file's ProgID. ASSOCF_INIT_BYEXENAME = 0x00000002, /// Identical to ASSOCF_INIT_BYEXENAME. ASSOCF_OPEN_BYEXENAME = 0x00000002, /// Specifies that when an IQueryAssociations method does not find the requested value under the root key, it should attempt to retrieve the comparable value from the * subkey. ASSOCF_INIT_DEFAULTTOSTAR = 0x00000004, /// Specifies that when a IQueryAssociations method does not find the requested value under the root key, it should attempt to retrieve the comparable value from the Folder subkey. ASSOCF_INIT_DEFAULTTOFOLDER = 0x00000008, /// Specifies that only HKEY_CLASSES_ROOT should be searched, and that HKEY_CURRENT_USER should be ignored. ASSOCF_NOUSERSETTINGS = 0x00000010, /// /// Specifies that the return string should not be truncated. Instead, return an error value and the required size for the /// complete string. /// ASSOCF_NOTRUNCATE = 0x00000020, /// Specifies that the return string should not be truncated. Instead, return an error value and the required size for the complete string. ASSOCF_VERIFY = 0x00000040, /// Instructs IQueryAssociations methods to ignore Rundll.exe and return information about its target. Typically IQueryAssociations methods return information about the first .exe or .dll in a command string. If a command uses Rundll.exe, setting this flag tells the method to ignore Rundll.exe and return information about its target. ASSOCF_REMAPRUNDLL = 0x00000080, /// Instructs IQueryAssociations methods not to fix errors in the registry, such as the friendly name of a function not matching the one found in the .exe file. ASSOCF_NOFIXUPS = 0x00000100, /// Specifies that the BaseClass value should be ignored. ASSOCF_IGNOREBASECLASS = 0x00000200, /// Introduced in Windows 7. Specifies that the "Unknown" ProgID should be ignored; instead, fail. ASSOCF_INIT_IGNOREUNKNOWN = 0x00000400, /// Introduced in Windows 8. Specifies that the supplied ProgID should be mapped using the system defaults, rather than the current user defaults. ASSOCF_INIT_FIXED_PROGID = 0x00000800, /// Introduced in Windows 8. Specifies that the value is a protocol, and should be mapped using the current user defaults. ASSOCF_IS_PROTOCOL = 0x00001000, /// Introduced in Windows 8.1. Specifies that the ProgID corresponds with a file extension based association. Use together with ASSOCF_INIT_FIXED_PROGID. ASSOCF_INIT_FOR_FILE = 0x00002000 } /// Used by IQueryAssociations::GetString to define the type of string that is to be returned. // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/ne-shlwapi-assocstr [PInvokeData("shlwapi.h", MSDNShortId = "b5fd3d25-3630-4dd8-acd2-d2e4ed571604")] public enum ASSOCSTR { /// A command string associated with a Shell verb. ASSOCSTR_COMMAND = 1, /// An executable from a Shell verb command string. For example, this string is found as the (Default) value for a subkey such as HKEY_CLASSES_ROOT\\shell\Open\command. If the command uses Rundll.exe, set the ASSOCF_REMAPRUNDLL flag in the parameter of IQueryAssociations::GetString to retrieve the target executable. This type of string will identify the code that will be invoked in the implementation of the verb. ASSOCSTR_EXECUTABLE, /// The friendly name of a document type. ASSOCSTR_FRIENDLYDOCNAME, /// The friendly name of an executable file. ASSOCSTR_FRIENDLYAPPNAME, /// Ignore the information associated with the open subkey. ASSOCSTR_NOOPEN, /// Look under the ShellNew subkey. ASSOCSTR_SHELLNEWVALUE, /// The DDE command to use to create a process. ASSOCSTR_DDEIFEXEC, /// The application name in a DDE broadcast. ASSOCSTR_DDEAPPLICATION, /// The topic name in a DDE broadcast. ASSOCSTR_DDETOPIC, /// Corresponds to the InfoTip registry value. Returns an info tip for an item, or list of properties in the form of an IPropertyDescriptionList from which to create an info tip, such as when hovering the cursor over a file name. The list of properties can be parsed with PSGetPropertyDescriptionListFromString. ASSOCSTR_INFOTIP, /// Introduced in Internet Explorer 6. Corresponds to the QuickTip registry value. Same as ASSOCSTR_INFOTIP, except that it always returns a list of property names in the form of an IPropertyDescriptionList. The difference between this value and ASSOCSTR_INFOTIP is that this returns properties that are safe for any scenario that causes slow property retrieval, such as offline or slow networks. Some of the properties returned from ASSOCSTR_INFOTIP might not be appropriate for slow property retrieval scenarios. The list of properties can be parsed with PSGetPropertyDescriptionListFromString. ASSOCSTR_QUICKTIP, /// Introduced in Internet Explorer 6. Corresponds to the TileInfo registry value. Contains a list of properties to be displayed for a particular file type in a Windows Explorer window that is in tile view. This is the same as ASSOCSTR_INFOTIP, but, like ASSOCSTR_QUICKTIP, it also returns a list of property names in the form of an IPropertyDescriptionList. The list of properties can be parsed with PSGetPropertyDescriptionListFromString. ASSOCSTR_TILEINFO, /// Introduced in Internet Explorer 6. Describes a general type of MIME file association, such as image and bmp, so that applications can make general assumptions about a specific file type. ASSOCSTR_CONTENTTYPE, /// Introduced in Internet Explorer 6. Returns the path to the icon resources to use by default for this association. Positive numbers indicate an index into the dll's resource table, while negative numbers indicate a resource ID. An example of the syntax for the resource is "c:\myfolder\myfile.dll,-1". ASSOCSTR_DEFAULTICON, /// Introduced in Internet Explorer 6. For an object that has a Shell extension associated with it, you can use this to retrieve the CLSID of that Shell extension object by passing a string representation of the IID of the interface you want to retrieve as the parameter of IQueryAssociations::GetString. For example, if you want to retrieve a handler that implements the IExtractImage interface, you would specify "{BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}", which is the IID of IExtractImage. ASSOCSTR_SHELLEXTENSION, /// Introduced in Internet Explorer 8.. For a verb invoked through COM and the IDropTarget interface, you can use this flag to retrieve the IDropTarget object's CLSID. This CLSID is registered in the DropTarget subkey. The verb is specified in the parameter in the call to IQueryAssociations::GetString. This type of string will identify the code that will be invoked in the implementation of the verb. ASSOCSTR_DROPTARGET, /// Introduced in Internet Explorer 8.. For a verb invoked through COM and the IExecuteCommand interface, you can use this flag to retrieve the IExecuteCommand object's CLSID. This CLSID is registered in the verb's command subkey as the DelegateExecute entry. The verb is specified in the parameter in the call to IQueryAssociations::GetString. This type of string will identify the code that will be invoked in the implementation of the verb. ASSOCSTR_DELEGATEEXECUTE, /// Introduced in Windows 8. ASSOCSTR_SUPPORTED_URI_PROTOCOLS, /// The ProgID provided by the app associated with the file type or URI scheme. This if configured by users in their default program settings. ASSOCSTR_PROGID, /// The AppUserModelID of the app associated with the file type or URI scheme. This is configured by users in their default program settings. ASSOCSTR_APPID, /// The publisher of the app associated with the file type or URI scheme. This is configured by users in their default program settings. ASSOCSTR_APPPUBLISHER, /// The icon reference of the app associated with the file type or URI scheme. This is configured by users in their default program settings. ASSOCSTR_APPICONREFERENCE, /// The maximum defined ASSOCSTR value, used for validation purposes. ASSOCSTR_MAX, } /// Specifies the type of key to be returned by IQueryAssociations::GetKey. // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/ne-shlwapi-assockey [PInvokeData("shlwapi.h", MSDNShortId = "f4ac0ba0-4113-498f-a51b-74a37fe33d49")] public enum ASSOCKEY { /// A key that is passed to ShellExecuteEx through a SHELLEXECUTEINFO structure. ASSOCKEY_SHELLEXECCLASS = 1, /// An Application key for the file type. ASSOCKEY_APP, /// A ProgID or class key. ASSOCKEY_CLASS, /// A BaseClass value. ASSOCKEY_BASECLASS, /// ASSOCKEY_MAX, } /// Used by IQueryAssociations::GetData to define the type of data that is to be returned. // https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/ne-shlwapi-assocdata [PInvokeData("shlwapi.h", MSDNShortId = "0ae5c8db-81fd-4d00-8e54-0c474f1bfd06")] public enum ASSOCDATA { /// The component descriptor to pass to the Windows Installer API. ASSOCDATA_MSIDESCRIPTOR = 1, /// Attempts to activate a window are restricted. There is no data associated with this value. ASSOCDATA_NOACTIVATEHANDLER, /// ASSOCDATA_UNUSED1, /// Defaults to user specified association. ASSOCDATA_HASPERUSERASSOC, /// Internet Explorer version 6 or later. Gets the data stored in the EditFlags value of a file association PROGID registry key. This value consists of one or more FILETYPEATTRIBUTEFLAGS. Compare against those values to determine which attributes have been set. ASSOCDATA_EDITFLAGS, /// Internet Explorer version 6 or later. Uses the parameter from the IQueryAssociations::GetData method as the value name. ASSOCDATA_VALUE, /// ASSOCDATA_MAX, } /// Used by IQueryAssociations::GetEnum to define the type of enum that is to be returned. public enum ASSOCENUM { /// Nothing. ASSOCENUM_NONE } } }