LnkShortcutHandler.cs 15 KB

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