LnkShortcutHandler.cs 12 KB

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