/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using System.Security; using System.Text; namespace Alphaleonis.Win32.Filesystem { internal static partial class NativeMethods { #region AssocXxx /// Returns a pointer to an IQueryAssociations object. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.U4)] internal static extern uint AssocCreate(Guid clsid, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IQueryAssociations ppv); /// Searches for and retrieves a file or protocol association-related string from the registry. /// Return value Type: HRESULT. Returns a standard COM error value, including the following: S_OK, E_POINTER and S_FALSE. /// Minimum supported client: Windows 2000 Professional /// Minimum supported server: Windows 2000 Server [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "AssocQueryStringW"), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.U4)] internal static extern uint AssocQueryString(Shell32.AssociationAttributes flags, Shell32.AssociationString str, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, [MarshalAs(UnmanagedType.LPWStr)] string pszExtra, StringBuilder pszOut, [MarshalAs(UnmanagedType.U4)] out uint pcchOut); #region IQueryAssociations internal static readonly Guid ClsidQueryAssociations = new Guid("A07034FD-6CAA-4954-AC3F-97A27216F98A"); internal const string QueryAssociationsGuid = "C46CA590-3C3F-11D2-BEE6-0000F805CA57"; /// 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. [Guid(QueryAssociationsGuid), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [SuppressUnmanagedCodeSecurity] internal interface IQueryAssociations { /// Initializes the IQueryAssociations interface and sets the root key to the appropriate ProgID. /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] void Init(Shell32.AssociationAttributes flags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, IntPtr hkProgid, IntPtr hwnd); //[return: MarshalAs(UnmanagedType.U4)] //uint Init(Shell32.AssociationAttributes flags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, IntPtr hkProgid, IntPtr hwnd); /// Searches for and retrieves a file or protocol association-related string from the registry. /// A standard COM error value, including the following: S_OK, E_POINTER, S_FALSE /// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] void GetString(Shell32.AssociationAttributes flags, Shell32.AssociationString str, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, StringBuilder pwszOut, [MarshalAs(UnmanagedType.I4)] out int pcchOut); //[return: MarshalAs(UnmanagedType.U4)] //void GetString(Shell32.AssociationAttributes flags, Shell32.AssociationString str, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, StringBuilder pwszOut, [MarshalAs(UnmanagedType.I4)] out int pcchOut); ///// Searches for and retrieves a file or protocol association-related key from the registry. ///// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. ///// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] ///// Minimum supported server: Windows 2000 Server [desktop apps only] //[return: MarshalAs(UnmanagedType.U4)] //uint GetKey(Shell32.AssociationAttributes flags, Shell32.AssociationKey str, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, out UIntPtr phkeyOut); ///// Searches for and retrieves file or protocol association-related binary data from the registry. ///// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. ///// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] ///// Minimum supported server: Windows 2000 Server [desktop apps only] //[return: MarshalAs(UnmanagedType.U4)] //uint GetData(Shell32.AssociationAttributes flags, Shell32.AssociationData data, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)] out byte[] pvOut, [MarshalAs(UnmanagedType.I4)] out int pcbOut); ///// This method is not implemented. //void GetEnum(); } #endregion // IQueryAssociations #endregion // AssocXxx #region Path /// Determines whether a path to a file system object such as a file or folder is valid. /// if the file exists; otherwise, . Call GetLastError for extended error information. /// /// This function tests the validity of the path. /// A path specified by Universal Naming Convention (UNC) is limited to a file only; that is, \\server\share\file is permitted. /// A network share path to a server or server share is not permitted; that is, \\server or \\server\share. /// This function returns FALSE if a mounted remote drive is out of service. /// /// Minimum supported client: Windows 2000 Professional /// Minimum supported server: Windows 2000 Server [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathFileExistsW"), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool PathFileExists([MarshalAs(UnmanagedType.LPWStr)] string pszPath); /// Converts a file URL to a Microsoft MS-DOS path. /// Type: HRESULT /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// /// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathCreateFromUrlW"), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.U4)] internal static extern uint PathCreateFromUrl([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, StringBuilder pszPath, [MarshalAs(UnmanagedType.U4)] ref uint pcchPath, [MarshalAs(UnmanagedType.U4)] uint dwFlags); /// Creates a path from a file URL. /// Type: HRESULT /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// /// Minimum supported client: Windows Vista [desktop apps only] /// Minimum supported server: Windows Server 2008 [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.U4)] internal static extern uint PathCreateFromUrlAlloc([MarshalAs(UnmanagedType.LPWStr)] string pszIn, out StringBuilder pszPath, [MarshalAs(UnmanagedType.U4)] uint dwFlags); /// Converts a Microsoft MS-DOS path to a canonicalized URL. /// Type: HRESULT /// Returns S_FALSE if pszPath is already in URL format. In this case, pszPath will simply be copied to pszUrl. /// Otherwise, it returns S_OK if successful or a standard COM error value if not. /// /// /// UrlCreateFromPath does not support extended paths. These are paths that include the extended-length path prefix "\\?\". /// /// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "UrlCreateFromPathW"), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.U4)] internal static extern uint UrlCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, StringBuilder pszUrl, ref uint pcchUrl, [MarshalAs(UnmanagedType.U4)] uint dwFlags); /// Tests whether a URL is a specified type. /// /// Type: BOOL /// For all but one of the URL types, UrlIs returns if the URL is the specified type, otherwise. /// If UrlIs is set to , UrlIs will attempt to determine the URL scheme. /// If the function is able to determine a scheme, it returns , or . /// /// Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "UrlIsW"), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool UrlIs([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, Shell32.UrlType urlIs); #endregion // Path /// Destroys an icon and frees any memory the icon occupied. /// Minimum supported client: Windows XP [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("user32.dll", SetLastError = false)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DestroyIcon(IntPtr hIcon); /// Retrieves information about an object in the file system, such as a file, folder, directory, or drive root. /// You should call this function from a background thread. Failure to do so could cause the UI to stop responding. /// Minimum supported client: Windows 2000 Professional [desktop apps only] /// Minimum supported server: Windows 2000 Server [desktop apps only] [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")] [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SHGetFileInfoW"), SuppressUnmanagedCodeSecurity] internal static extern IntPtr ShGetFileInfo([MarshalAs(UnmanagedType.LPWStr)] string pszPath, FileAttributes dwFileAttributes, [MarshalAs(UnmanagedType.Struct)] out Shell32.FileInfo psfi, [MarshalAs(UnmanagedType.U4)] uint cbFileInfo, [MarshalAs(UnmanagedType.U4)] Shell32.FileAttributes uFlags); } }