NativeMethods.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Security;
  5. using System.Text;
  6. namespace MediaBrowser.Controller.IO
  7. {
  8. /// <summary>
  9. /// Class NativeMethods
  10. /// </summary>
  11. [SuppressUnmanagedCodeSecurity]
  12. public static class NativeMethods
  13. {
  14. /// <summary>
  15. /// The MA x_ PATH
  16. /// </summary>
  17. public const int MAX_PATH = 260;
  18. /// <summary>
  19. /// The MA x_ ALTERNATE
  20. /// </summary>
  21. public const int MAX_ALTERNATE = 14;
  22. /// <summary>
  23. /// The INVALI d_ HANDL e_ VALUE
  24. /// </summary>
  25. public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  26. /// <summary>
  27. /// The STG m_ READ
  28. /// </summary>
  29. public const uint STGM_READ = 0;
  30. }
  31. /// <summary>
  32. /// Struct FILETIME
  33. /// </summary>
  34. [StructLayout(LayoutKind.Sequential)]
  35. public struct FILETIME
  36. {
  37. /// <summary>
  38. /// The dw low date time
  39. /// </summary>
  40. public uint dwLowDateTime;
  41. /// <summary>
  42. /// The dw high date time
  43. /// </summary>
  44. public uint dwHighDateTime;
  45. }
  46. /// <summary>
  47. /// Struct WIN32_FIND_DATA
  48. /// </summary>
  49. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  50. public struct WIN32_FIND_DATA
  51. {
  52. /// <summary>
  53. /// The dw file attributes
  54. /// </summary>
  55. public FileAttributes dwFileAttributes;
  56. /// <summary>
  57. /// The ft creation time
  58. /// </summary>
  59. public FILETIME ftCreationTime;
  60. /// <summary>
  61. /// The ft last access time
  62. /// </summary>
  63. public FILETIME ftLastAccessTime;
  64. /// <summary>
  65. /// The ft last write time
  66. /// </summary>
  67. public FILETIME ftLastWriteTime;
  68. /// <summary>
  69. /// The n file size high
  70. /// </summary>
  71. public int nFileSizeHigh;
  72. /// <summary>
  73. /// The n file size low
  74. /// </summary>
  75. public int nFileSizeLow;
  76. /// <summary>
  77. /// The dw reserved0
  78. /// </summary>
  79. public int dwReserved0;
  80. /// <summary>
  81. /// The dw reserved1
  82. /// </summary>
  83. public int dwReserved1;
  84. /// <summary>
  85. /// The c file name
  86. /// </summary>
  87. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)]
  88. public string cFileName;
  89. /// <summary>
  90. /// This will always be null when FINDEX_INFO_LEVELS = basic
  91. /// </summary>
  92. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)]
  93. public string cAlternate;
  94. /// <summary>
  95. /// Gets or sets the path.
  96. /// </summary>
  97. /// <value>The path.</value>
  98. public string Path { get; set; }
  99. /// <summary>
  100. /// Returns a <see cref="System.String" /> that represents this instance.
  101. /// </summary>
  102. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  103. public override string ToString()
  104. {
  105. return Path ?? string.Empty;
  106. }
  107. }
  108. /// <summary>
  109. /// Enum SLGP_FLAGS
  110. /// </summary>
  111. [Flags]
  112. public enum SLGP_FLAGS
  113. {
  114. /// <summary>
  115. /// Retrieves the standard short (8.3 format) file name
  116. /// </summary>
  117. SLGP_SHORTPATH = 0x1,
  118. /// <summary>
  119. /// Retrieves the Universal Naming Convention (UNC) path name of the file
  120. /// </summary>
  121. SLGP_UNCPRIORITY = 0x2,
  122. /// <summary>
  123. /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded
  124. /// </summary>
  125. SLGP_RAWPATH = 0x4
  126. }
  127. /// <summary>
  128. /// Enum SLR_FLAGS
  129. /// </summary>
  130. [Flags]
  131. public enum SLR_FLAGS
  132. {
  133. /// <summary>
  134. /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
  135. /// the high-order word of fFlags can be set to a time-out value that specifies the
  136. /// maximum amount of time to be spent resolving the link. The function returns if the
  137. /// link cannot be resolved within the time-out duration. If the high-order word is set
  138. /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
  139. /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
  140. /// duration, in milliseconds.
  141. /// </summary>
  142. SLR_NO_UI = 0x1,
  143. /// <summary>
  144. /// Obsolete and no longer used
  145. /// </summary>
  146. SLR_ANY_MATCH = 0x2,
  147. /// <summary>
  148. /// If the link object has changed, update its path and list of identifiers.
  149. /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
  150. /// whether or not the link object has changed.
  151. /// </summary>
  152. SLR_UPDATE = 0x4,
  153. /// <summary>
  154. /// Do not update the link information
  155. /// </summary>
  156. SLR_NOUPDATE = 0x8,
  157. /// <summary>
  158. /// Do not execute the search heuristics
  159. /// </summary>
  160. SLR_NOSEARCH = 0x10,
  161. /// <summary>
  162. /// Do not use distributed link tracking
  163. /// </summary>
  164. SLR_NOTRACK = 0x20,
  165. /// <summary>
  166. /// Disable distributed link tracking. By default, distributed link tracking tracks
  167. /// removable media across multiple devices based on the volume name. It also uses the
  168. /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
  169. /// has changed. Setting SLR_NOLINKINFO disables both types of tracking.
  170. /// </summary>
  171. SLR_NOLINKINFO = 0x40,
  172. /// <summary>
  173. /// Call the Microsoft Windows Installer
  174. /// </summary>
  175. SLR_INVOKE_MSI = 0x80
  176. }
  177. /// <summary>
  178. /// The IShellLink interface allows Shell links to be created, modified, and resolved
  179. /// </summary>
  180. [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
  181. public interface IShellLinkW
  182. {
  183. /// <summary>
  184. /// Retrieves the path and file name of a Shell link object
  185. /// </summary>
  186. /// <param name="pszFile">The PSZ file.</param>
  187. /// <param name="cchMaxPath">The CCH max path.</param>
  188. /// <param name="pfd">The PFD.</param>
  189. /// <param name="fFlags">The f flags.</param>
  190. void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags);
  191. /// <summary>
  192. /// Retrieves the list of item identifiers for a Shell link object
  193. /// </summary>
  194. /// <param name="ppidl">The ppidl.</param>
  195. void GetIDList(out IntPtr ppidl);
  196. /// <summary>
  197. /// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
  198. /// </summary>
  199. /// <param name="pidl">The pidl.</param>
  200. void SetIDList(IntPtr pidl);
  201. /// <summary>
  202. /// Retrieves the description string for a Shell link object
  203. /// </summary>
  204. /// <param name="pszName">Name of the PSZ.</param>
  205. /// <param name="cchMaxName">Name of the CCH max.</param>
  206. void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
  207. /// <summary>
  208. /// Sets the description for a Shell link object. The description can be any application-defined string
  209. /// </summary>
  210. /// <param name="pszName">Name of the PSZ.</param>
  211. void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
  212. /// <summary>
  213. /// Retrieves the name of the working directory for a Shell link object
  214. /// </summary>
  215. /// <param name="pszDir">The PSZ dir.</param>
  216. /// <param name="cchMaxPath">The CCH max path.</param>
  217. void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
  218. /// <summary>
  219. /// Sets the name of the working directory for a Shell link object
  220. /// </summary>
  221. /// <param name="pszDir">The PSZ dir.</param>
  222. void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  223. /// <summary>
  224. /// Retrieves the command-line arguments associated with a Shell link object
  225. /// </summary>
  226. /// <param name="pszArgs">The PSZ args.</param>
  227. /// <param name="cchMaxPath">The CCH max path.</param>
  228. void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
  229. /// <summary>
  230. /// Sets the command-line arguments for a Shell link object
  231. /// </summary>
  232. /// <param name="pszArgs">The PSZ args.</param>
  233. void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  234. /// <summary>
  235. /// Retrieves the hot key for a Shell link object
  236. /// </summary>
  237. /// <param name="pwHotkey">The pw hotkey.</param>
  238. void GetHotkey(out short pwHotkey);
  239. /// <summary>
  240. /// Sets a hot key for a Shell link object
  241. /// </summary>
  242. /// <param name="wHotkey">The w hotkey.</param>
  243. void SetHotkey(short wHotkey);
  244. /// <summary>
  245. /// Retrieves the show command for a Shell link object
  246. /// </summary>
  247. /// <param name="piShowCmd">The pi show CMD.</param>
  248. void GetShowCmd(out int piShowCmd);
  249. /// <summary>
  250. /// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
  251. /// </summary>
  252. /// <param name="iShowCmd">The i show CMD.</param>
  253. void SetShowCmd(int iShowCmd);
  254. /// <summary>
  255. /// Retrieves the location (path and index) of the icon for a Shell link object
  256. /// </summary>
  257. /// <param name="pszIconPath">The PSZ icon path.</param>
  258. /// <param name="cchIconPath">The CCH icon path.</param>
  259. /// <param name="piIcon">The pi icon.</param>
  260. void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
  261. int cchIconPath, out int piIcon);
  262. /// <summary>
  263. /// Sets the location (path and index) of the icon for a Shell link object
  264. /// </summary>
  265. /// <param name="pszIconPath">The PSZ icon path.</param>
  266. /// <param name="iIcon">The i icon.</param>
  267. void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
  268. /// <summary>
  269. /// Sets the relative path to the Shell link object
  270. /// </summary>
  271. /// <param name="pszPathRel">The PSZ path rel.</param>
  272. /// <param name="dwReserved">The dw reserved.</param>
  273. void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
  274. /// <summary>
  275. /// Attempts to find the target of a Shell link, even if it has been moved or renamed
  276. /// </summary>
  277. /// <param name="hwnd">The HWND.</param>
  278. /// <param name="fFlags">The f flags.</param>
  279. void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
  280. /// <summary>
  281. /// Sets the path and file name of a Shell link object
  282. /// </summary>
  283. /// <param name="pszFile">The PSZ file.</param>
  284. void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  285. }
  286. /// <summary>
  287. /// Interface IPersist
  288. /// </summary>
  289. [ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
  290. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  291. public interface IPersist
  292. {
  293. /// <summary>
  294. /// Gets the class ID.
  295. /// </summary>
  296. /// <param name="pClassID">The p class ID.</param>
  297. [PreserveSig]
  298. void GetClassID(out Guid pClassID);
  299. }
  300. /// <summary>
  301. /// Interface IPersistFile
  302. /// </summary>
  303. [ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
  304. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  305. public interface IPersistFile : IPersist
  306. {
  307. /// <summary>
  308. /// Gets the class ID.
  309. /// </summary>
  310. /// <param name="pClassID">The p class ID.</param>
  311. new void GetClassID(out Guid pClassID);
  312. /// <summary>
  313. /// Determines whether this instance is dirty.
  314. /// </summary>
  315. [PreserveSig]
  316. int IsDirty();
  317. /// <summary>
  318. /// Loads the specified PSZ file name.
  319. /// </summary>
  320. /// <param name="pszFileName">Name of the PSZ file.</param>
  321. /// <param name="dwMode">The dw mode.</param>
  322. [PreserveSig]
  323. void Load([In, MarshalAs(UnmanagedType.LPWStr)]
  324. string pszFileName, uint dwMode);
  325. /// <summary>
  326. /// Saves the specified PSZ file name.
  327. /// </summary>
  328. /// <param name="pszFileName">Name of the PSZ file.</param>
  329. /// <param name="remember">if set to <c>true</c> [remember].</param>
  330. [PreserveSig]
  331. void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  332. [In, MarshalAs(UnmanagedType.Bool)] bool remember);
  333. /// <summary>
  334. /// Saves the completed.
  335. /// </summary>
  336. /// <param name="pszFileName">Name of the PSZ file.</param>
  337. [PreserveSig]
  338. void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  339. /// <summary>
  340. /// Gets the cur file.
  341. /// </summary>
  342. /// <param name="ppszFileName">Name of the PPSZ file.</param>
  343. [PreserveSig]
  344. void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
  345. }
  346. // CLSID_ShellLink from ShlGuid.h
  347. /// <summary>
  348. /// Class ShellLink
  349. /// </summary>
  350. [
  351. ComImport,
  352. Guid("00021401-0000-0000-C000-000000000046")
  353. ]
  354. public class ShellLink
  355. {
  356. }
  357. }