Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

198 righe
14 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.IO;
  24. using System.Runtime.InteropServices;
  25. using System.Security;
  26. using System.Text;
  27. namespace Alphaleonis.Win32.Filesystem
  28. {
  29. internal static partial class NativeMethods
  30. {
  31. #region AssocXxx
  32. /// <summary>Returns a pointer to an IQueryAssociations object.</summary>
  33. /// <returns>If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
  34. /// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  35. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  36. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  37. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  38. [return: MarshalAs(UnmanagedType.U4)]
  39. internal static extern uint AssocCreate(Guid clsid, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IQueryAssociations ppv);
  40. /// <summary>Searches for and retrieves a file or protocol association-related string from the registry.</summary>
  41. /// <returns>Return value Type: HRESULT. Returns a standard COM error value, including the following: S_OK, E_POINTER and S_FALSE.</returns>
  42. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  43. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  44. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  45. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "AssocQueryStringW"), SuppressUnmanagedCodeSecurity]
  46. [return: MarshalAs(UnmanagedType.U4)]
  47. 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);
  48. #region IQueryAssociations
  49. internal static readonly Guid ClsidQueryAssociations = new Guid("A07034FD-6CAA-4954-AC3F-97A27216F98A");
  50. internal const string QueryAssociationsGuid = "C46CA590-3C3F-11D2-BEE6-0000F805CA57";
  51. /// <summary>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.</summary>
  52. [Guid(QueryAssociationsGuid), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  53. [SuppressUnmanagedCodeSecurity]
  54. internal interface IQueryAssociations
  55. {
  56. /// <summary>Initializes the IQueryAssociations interface and sets the root key to the appropriate ProgID.</summary>
  57. /// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
  58. /// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  59. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  60. void Init(Shell32.AssociationAttributes flags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, IntPtr hkProgid, IntPtr hwnd);
  61. //[return: MarshalAs(UnmanagedType.U4)]
  62. //uint Init(Shell32.AssociationAttributes flags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, IntPtr hkProgid, IntPtr hwnd);
  63. /// <summary>Searches for and retrieves a file or protocol association-related string from the registry.</summary>
  64. /// <returns>A standard COM error value, including the following: S_OK, E_POINTER, S_FALSE</returns>
  65. /// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  66. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  67. void GetString(Shell32.AssociationAttributes flags, Shell32.AssociationString str, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, StringBuilder pwszOut, [MarshalAs(UnmanagedType.I4)] out int pcchOut);
  68. //[return: MarshalAs(UnmanagedType.U4)]
  69. //void GetString(Shell32.AssociationAttributes flags, Shell32.AssociationString str, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, StringBuilder pwszOut, [MarshalAs(UnmanagedType.I4)] out int pcchOut);
  70. ///// <summary>Searches for and retrieves a file or protocol association-related key from the registry.</summary>
  71. ///// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
  72. ///// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  73. ///// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  74. //[return: MarshalAs(UnmanagedType.U4)]
  75. //uint GetKey(Shell32.AssociationAttributes flags, Shell32.AssociationKey str, [MarshalAs(UnmanagedType.LPWStr)] string pwszExtra, out UIntPtr phkeyOut);
  76. ///// <summary>Searches for and retrieves file or protocol association-related binary data from the registry.</summary>
  77. ///// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
  78. ///// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  79. ///// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  80. //[return: MarshalAs(UnmanagedType.U4)]
  81. //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);
  82. ///// <summary>This method is not implemented.</summary>
  83. //void GetEnum();
  84. }
  85. #endregion // IQueryAssociations
  86. #endregion // AssocXxx
  87. #region Path
  88. /// <summary>Determines whether a path to a file system object such as a file or folder is valid.</summary>
  89. /// <returns><see langword="true"/> if the file exists; otherwise, <see langword="false"/>. Call GetLastError for extended error information.</returns>
  90. /// <remarks>
  91. /// This function tests the validity of the path.
  92. /// A path specified by Universal Naming Convention (UNC) is limited to a file only; that is, \\server\share\file is permitted.
  93. /// A network share path to a server or server share is not permitted; that is, \\server or \\server\share.
  94. /// This function returns FALSE if a mounted remote drive is out of service.
  95. /// </remarks>
  96. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  97. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  98. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  99. [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathFileExistsW"), SuppressUnmanagedCodeSecurity]
  100. [return: MarshalAs(UnmanagedType.Bool)]
  101. internal static extern bool PathFileExists([MarshalAs(UnmanagedType.LPWStr)] string pszPath);
  102. /// <summary>Converts a file URL to a Microsoft MS-DOS path.</summary>
  103. /// <returns>Type: HRESULT
  104. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
  105. /// </returns>
  106. /// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  107. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  108. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  109. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathCreateFromUrlW"), SuppressUnmanagedCodeSecurity]
  110. [return: MarshalAs(UnmanagedType.U4)]
  111. internal static extern uint PathCreateFromUrl([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, StringBuilder pszPath, [MarshalAs(UnmanagedType.U4)] ref uint pcchPath, [MarshalAs(UnmanagedType.U4)] uint dwFlags);
  112. /// <summary>Creates a path from a file URL.</summary>
  113. /// <returns>Type: HRESULT
  114. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
  115. /// </returns>
  116. /// <remarks>Minimum supported client: Windows Vista [desktop apps only]</remarks>
  117. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only]</remarks>
  118. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  119. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  120. [return: MarshalAs(UnmanagedType.U4)]
  121. internal static extern uint PathCreateFromUrlAlloc([MarshalAs(UnmanagedType.LPWStr)] string pszIn, out StringBuilder pszPath, [MarshalAs(UnmanagedType.U4)] uint dwFlags);
  122. /// <summary>Converts a Microsoft MS-DOS path to a canonicalized URL.</summary>
  123. /// <returns>Type: HRESULT
  124. /// Returns S_FALSE if pszPath is already in URL format. In this case, pszPath will simply be copied to pszUrl.
  125. /// Otherwise, it returns S_OK if successful or a standard COM error value if not.
  126. /// </returns>
  127. /// <remarks>
  128. /// UrlCreateFromPath does not support extended paths. These are paths that include the extended-length path prefix "\\?\".
  129. /// </remarks>
  130. /// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  131. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  132. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  133. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "UrlCreateFromPathW"), SuppressUnmanagedCodeSecurity]
  134. [return: MarshalAs(UnmanagedType.U4)]
  135. internal static extern uint UrlCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, StringBuilder pszUrl, ref uint pcchUrl, [MarshalAs(UnmanagedType.U4)] uint dwFlags);
  136. /// <summary>Tests whether a URL is a specified type.</summary>
  137. /// <returns>
  138. /// Type: BOOL
  139. /// For all but one of the URL types, UrlIs returns <see langword="true"/> if the URL is the specified type, <see langword="true"/> otherwise.
  140. /// If UrlIs is set to <see cref="Shell32.UrlType.IsAppliable"/>, UrlIs will attempt to determine the URL scheme.
  141. /// If the function is able to determine a scheme, it returns <see langword="true"/>, or <see langword="false"/>.
  142. /// </returns>
  143. /// <remarks>Minimum supported client: Windows 2000 Professional, Windows XP [desktop apps only]</remarks>
  144. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  145. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  146. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "UrlIsW"), SuppressUnmanagedCodeSecurity]
  147. [return: MarshalAs(UnmanagedType.Bool)]
  148. internal static extern bool UrlIs([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, Shell32.UrlType urlIs);
  149. #endregion // Path
  150. /// <summary>Destroys an icon and frees any memory the icon occupied.</summary>
  151. /// <remarks>Minimum supported client: Windows XP [desktop apps only]</remarks>
  152. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  153. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  154. [DllImport("user32.dll", SetLastError = false)]
  155. [return: MarshalAs(UnmanagedType.Bool)]
  156. internal static extern bool DestroyIcon(IntPtr hIcon);
  157. /// <summary>Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.</summary>
  158. /// <remarks>You should call this function from a background thread. Failure to do so could cause the UI to stop responding.</remarks>
  159. /// <remarks>Minimum supported client: Windows 2000 Professional [desktop apps only]</remarks>
  160. /// <remarks>Minimum supported server: Windows 2000 Server [desktop apps only]</remarks>
  161. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  162. [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SHGetFileInfoW"), SuppressUnmanagedCodeSecurity]
  163. 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);
  164. }
  165. }