LnkShortcutHandler.cs 12 KB

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