LnkShortcutHandler.cs 12 KB

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