NativeMethods.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. //declare the Netapi32 : NetServerEnum method import
  15. /// <summary>
  16. /// Nets the server enum.
  17. /// </summary>
  18. /// <param name="ServerName">Name of the server.</param>
  19. /// <param name="dwLevel">The dw level.</param>
  20. /// <param name="pBuf">The p buf.</param>
  21. /// <param name="dwPrefMaxLen">The dw pref max len.</param>
  22. /// <param name="dwEntriesRead">The dw entries read.</param>
  23. /// <param name="dwTotalEntries">The dw total entries.</param>
  24. /// <param name="dwServerType">Type of the dw server.</param>
  25. /// <param name="domain">The domain.</param>
  26. /// <param name="dwResumeHandle">The dw resume handle.</param>
  27. /// <returns>System.Int32.</returns>
  28. [DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true),
  29. SuppressUnmanagedCodeSecurityAttribute]
  30. public static extern int NetServerEnum(
  31. string ServerName, // must be null
  32. int dwLevel,
  33. ref IntPtr pBuf,
  34. int dwPrefMaxLen,
  35. out int dwEntriesRead,
  36. out int dwTotalEntries,
  37. int dwServerType,
  38. string domain, // null for login domain
  39. out int dwResumeHandle
  40. );
  41. //declare the Netapi32 : NetApiBufferFree method import
  42. /// <summary>
  43. /// Nets the API buffer free.
  44. /// </summary>
  45. /// <param name="pBuf">The p buf.</param>
  46. /// <returns>System.Int32.</returns>
  47. [DllImport("Netapi32", SetLastError = true),
  48. SuppressUnmanagedCodeSecurityAttribute]
  49. public static extern int NetApiBufferFree(
  50. IntPtr pBuf);
  51. /// <summary>
  52. /// The MA x_ PATH
  53. /// </summary>
  54. public const int MAX_PATH = 260;
  55. /// <summary>
  56. /// The MA x_ ALTERNATE
  57. /// </summary>
  58. public const int MAX_ALTERNATE = 14;
  59. /// <summary>
  60. /// The INVALI d_ HANDL e_ VALUE
  61. /// </summary>
  62. public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  63. /// <summary>
  64. /// The STG m_ READ
  65. /// </summary>
  66. public const uint STGM_READ = 0;
  67. }
  68. //create a _SERVER_INFO_100 STRUCTURE
  69. /// <summary>
  70. /// Struct _SERVER_INFO_100
  71. /// </summary>
  72. [StructLayout(LayoutKind.Sequential)]
  73. public struct _SERVER_INFO_100
  74. {
  75. /// <summary>
  76. /// The sv100_platform_id
  77. /// </summary>
  78. internal int sv100_platform_id;
  79. /// <summary>
  80. /// The sv100_name
  81. /// </summary>
  82. [MarshalAs(UnmanagedType.LPWStr)]
  83. internal string sv100_name;
  84. }
  85. /// <summary>
  86. /// Class FindFirstFileExFlags
  87. /// </summary>
  88. public class FindFirstFileExFlags
  89. {
  90. /// <summary>
  91. /// The NONE
  92. /// </summary>
  93. public const int NONE = 0;
  94. /// <summary>
  95. /// Searches are case-sensitive.Searches are case-sensitive.
  96. /// </summary>
  97. public const int FIND_FIRST_EX_CASE_SENSITIVE = 1;
  98. /// <summary>
  99. /// Uses a larger buffer for directory queries, which can increase performance of the find operation.
  100. /// </summary>
  101. public const int FIND_FIRST_EX_LARGE_FETCH = 2;
  102. }
  103. /// <summary>
  104. /// Enum FINDEX_INFO_LEVELS
  105. /// </summary>
  106. public enum FINDEX_INFO_LEVELS
  107. {
  108. /// <summary>
  109. /// The FindFirstFileEx function retrieves a standard set of attribute information. The data is returned in a WIN32_FIND_DATA structure.
  110. /// </summary>
  111. FindExInfoStandard = 0,
  112. /// <summary>
  113. /// The FindFirstFileEx function does not query the short file name, improving overall enumeration speed. The data is returned in a WIN32_FIND_DATA structure, and the cAlternateFileName member is always a NULL string.
  114. /// </summary>
  115. FindExInfoBasic = 1
  116. }
  117. /// <summary>
  118. /// Enum FINDEX_SEARCH_OPS
  119. /// </summary>
  120. public enum FINDEX_SEARCH_OPS
  121. {
  122. /// <summary>
  123. /// The search for a file that matches a specified file name.
  124. /// The lpSearchFilter parameter of FindFirstFileEx must be NULL when this search operation is used.
  125. /// </summary>
  126. FindExSearchNameMatch = 0,
  127. /// <summary>
  128. /// The find ex search limit to directories
  129. /// </summary>
  130. FindExSearchLimitToDirectories = 1,
  131. /// <summary>
  132. /// This filtering type is not available.
  133. /// </summary>
  134. FindExSearchLimitToDevices = 2
  135. }
  136. /// <summary>
  137. /// Struct FILETIME
  138. /// </summary>
  139. [StructLayout(LayoutKind.Sequential)]
  140. public struct FILETIME
  141. {
  142. /// <summary>
  143. /// The dw low date time
  144. /// </summary>
  145. public uint dwLowDateTime;
  146. /// <summary>
  147. /// The dw high date time
  148. /// </summary>
  149. public uint dwHighDateTime;
  150. }
  151. /// <summary>
  152. /// Struct WIN32_FIND_DATA
  153. /// </summary>
  154. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  155. public struct WIN32_FIND_DATA
  156. {
  157. /// <summary>
  158. /// The dw file attributes
  159. /// </summary>
  160. public FileAttributes dwFileAttributes;
  161. /// <summary>
  162. /// The ft creation time
  163. /// </summary>
  164. public FILETIME ftCreationTime;
  165. /// <summary>
  166. /// The ft last access time
  167. /// </summary>
  168. public FILETIME ftLastAccessTime;
  169. /// <summary>
  170. /// The ft last write time
  171. /// </summary>
  172. public FILETIME ftLastWriteTime;
  173. /// <summary>
  174. /// The n file size high
  175. /// </summary>
  176. public int nFileSizeHigh;
  177. /// <summary>
  178. /// The n file size low
  179. /// </summary>
  180. public int nFileSizeLow;
  181. /// <summary>
  182. /// The dw reserved0
  183. /// </summary>
  184. public int dwReserved0;
  185. /// <summary>
  186. /// The dw reserved1
  187. /// </summary>
  188. public int dwReserved1;
  189. /// <summary>
  190. /// The c file name
  191. /// </summary>
  192. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)]
  193. public string cFileName;
  194. /// <summary>
  195. /// This will always be null when FINDEX_INFO_LEVELS = basic
  196. /// </summary>
  197. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)]
  198. public string cAlternate;
  199. /// <summary>
  200. /// Gets a value indicating whether this instance is hidden.
  201. /// </summary>
  202. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  203. public bool IsHidden
  204. {
  205. get
  206. {
  207. return dwFileAttributes.HasFlag(FileAttributes.Hidden);
  208. }
  209. }
  210. /// <summary>
  211. /// Gets a value indicating whether this instance is system file.
  212. /// </summary>
  213. /// <value><c>true</c> if this instance is system file; otherwise, <c>false</c>.</value>
  214. public bool IsSystemFile
  215. {
  216. get
  217. {
  218. return dwFileAttributes.HasFlag(FileAttributes.System);
  219. }
  220. }
  221. /// <summary>
  222. /// Gets a value indicating whether this instance is directory.
  223. /// </summary>
  224. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  225. public bool IsDirectory
  226. {
  227. get
  228. {
  229. return dwFileAttributes.HasFlag(FileAttributes.Directory);
  230. }
  231. }
  232. /// <summary>
  233. /// Gets the creation time UTC.
  234. /// </summary>
  235. /// <value>The creation time UTC.</value>
  236. public DateTime CreationTimeUtc
  237. {
  238. get
  239. {
  240. return ParseFileTime(ftCreationTime);
  241. }
  242. }
  243. /// <summary>
  244. /// Gets the last access time UTC.
  245. /// </summary>
  246. /// <value>The last access time UTC.</value>
  247. public DateTime LastAccessTimeUtc
  248. {
  249. get
  250. {
  251. return ParseFileTime(ftLastAccessTime);
  252. }
  253. }
  254. /// <summary>
  255. /// Gets the last write time UTC.
  256. /// </summary>
  257. /// <value>The last write time UTC.</value>
  258. public DateTime LastWriteTimeUtc
  259. {
  260. get
  261. {
  262. return ParseFileTime(ftLastWriteTime);
  263. }
  264. }
  265. /// <summary>
  266. /// Parses the file time.
  267. /// </summary>
  268. /// <param name="filetime">The filetime.</param>
  269. /// <returns>DateTime.</returns>
  270. private DateTime ParseFileTime(FILETIME filetime)
  271. {
  272. long highBits = filetime.dwHighDateTime;
  273. highBits = highBits << 32;
  274. var val = highBits + (long) filetime.dwLowDateTime;
  275. if (val < 0L)
  276. {
  277. return DateTime.MinValue;
  278. }
  279. if (val > 2650467743999999999L)
  280. {
  281. return DateTime.MaxValue;
  282. }
  283. return DateTime.FromFileTimeUtc(val);
  284. }
  285. /// <summary>
  286. /// Gets or sets the path.
  287. /// </summary>
  288. /// <value>The path.</value>
  289. public string Path { get; set; }
  290. /// <summary>
  291. /// Returns a <see cref="System.String" /> that represents this instance.
  292. /// </summary>
  293. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  294. public override string ToString()
  295. {
  296. return Path ?? string.Empty;
  297. }
  298. }
  299. /// <summary>
  300. /// Enum SLGP_FLAGS
  301. /// </summary>
  302. [Flags]
  303. public enum SLGP_FLAGS
  304. {
  305. /// <summary>
  306. /// Retrieves the standard short (8.3 format) file name
  307. /// </summary>
  308. SLGP_SHORTPATH = 0x1,
  309. /// <summary>
  310. /// Retrieves the Universal Naming Convention (UNC) path name of the file
  311. /// </summary>
  312. SLGP_UNCPRIORITY = 0x2,
  313. /// <summary>
  314. /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded
  315. /// </summary>
  316. SLGP_RAWPATH = 0x4
  317. }
  318. /// <summary>
  319. /// Enum SLR_FLAGS
  320. /// </summary>
  321. [Flags]
  322. public enum SLR_FLAGS
  323. {
  324. /// <summary>
  325. /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
  326. /// the high-order word of fFlags can be set to a time-out value that specifies the
  327. /// maximum amount of time to be spent resolving the link. The function returns if the
  328. /// link cannot be resolved within the time-out duration. If the high-order word is set
  329. /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
  330. /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
  331. /// duration, in milliseconds.
  332. /// </summary>
  333. SLR_NO_UI = 0x1,
  334. /// <summary>
  335. /// Obsolete and no longer used
  336. /// </summary>
  337. SLR_ANY_MATCH = 0x2,
  338. /// <summary>
  339. /// If the link object has changed, update its path and list of identifiers.
  340. /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
  341. /// whether or not the link object has changed.
  342. /// </summary>
  343. SLR_UPDATE = 0x4,
  344. /// <summary>
  345. /// Do not update the link information
  346. /// </summary>
  347. SLR_NOUPDATE = 0x8,
  348. /// <summary>
  349. /// Do not execute the search heuristics
  350. /// </summary>
  351. SLR_NOSEARCH = 0x10,
  352. /// <summary>
  353. /// Do not use distributed link tracking
  354. /// </summary>
  355. SLR_NOTRACK = 0x20,
  356. /// <summary>
  357. /// Disable distributed link tracking. By default, distributed link tracking tracks
  358. /// removable media across multiple devices based on the volume name. It also uses the
  359. /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
  360. /// has changed. Setting SLR_NOLINKINFO disables both types of tracking.
  361. /// </summary>
  362. SLR_NOLINKINFO = 0x40,
  363. /// <summary>
  364. /// Call the Microsoft Windows Installer
  365. /// </summary>
  366. SLR_INVOKE_MSI = 0x80
  367. }
  368. /// <summary>
  369. /// The IShellLink interface allows Shell links to be created, modified, and resolved
  370. /// </summary>
  371. [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
  372. public interface IShellLinkW
  373. {
  374. /// <summary>
  375. /// Retrieves the path and file name of a Shell link object
  376. /// </summary>
  377. /// <param name="pszFile">The PSZ file.</param>
  378. /// <param name="cchMaxPath">The CCH max path.</param>
  379. /// <param name="pfd">The PFD.</param>
  380. /// <param name="fFlags">The f flags.</param>
  381. void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags);
  382. /// <summary>
  383. /// Retrieves the list of item identifiers for a Shell link object
  384. /// </summary>
  385. /// <param name="ppidl">The ppidl.</param>
  386. void GetIDList(out IntPtr ppidl);
  387. /// <summary>
  388. /// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
  389. /// </summary>
  390. /// <param name="pidl">The pidl.</param>
  391. void SetIDList(IntPtr pidl);
  392. /// <summary>
  393. /// Retrieves the description string for a Shell link object
  394. /// </summary>
  395. /// <param name="pszName">Name of the PSZ.</param>
  396. /// <param name="cchMaxName">Name of the CCH max.</param>
  397. void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
  398. /// <summary>
  399. /// Sets the description for a Shell link object. The description can be any application-defined string
  400. /// </summary>
  401. /// <param name="pszName">Name of the PSZ.</param>
  402. void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
  403. /// <summary>
  404. /// Retrieves the name of the working directory for a Shell link object
  405. /// </summary>
  406. /// <param name="pszDir">The PSZ dir.</param>
  407. /// <param name="cchMaxPath">The CCH max path.</param>
  408. void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
  409. /// <summary>
  410. /// Sets the name of the working directory for a Shell link object
  411. /// </summary>
  412. /// <param name="pszDir">The PSZ dir.</param>
  413. void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  414. /// <summary>
  415. /// Retrieves the command-line arguments associated with a Shell link object
  416. /// </summary>
  417. /// <param name="pszArgs">The PSZ args.</param>
  418. /// <param name="cchMaxPath">The CCH max path.</param>
  419. void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
  420. /// <summary>
  421. /// Sets the command-line arguments for a Shell link object
  422. /// </summary>
  423. /// <param name="pszArgs">The PSZ args.</param>
  424. void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  425. /// <summary>
  426. /// Retrieves the hot key for a Shell link object
  427. /// </summary>
  428. /// <param name="pwHotkey">The pw hotkey.</param>
  429. void GetHotkey(out short pwHotkey);
  430. /// <summary>
  431. /// Sets a hot key for a Shell link object
  432. /// </summary>
  433. /// <param name="wHotkey">The w hotkey.</param>
  434. void SetHotkey(short wHotkey);
  435. /// <summary>
  436. /// Retrieves the show command for a Shell link object
  437. /// </summary>
  438. /// <param name="piShowCmd">The pi show CMD.</param>
  439. void GetShowCmd(out int piShowCmd);
  440. /// <summary>
  441. /// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
  442. /// </summary>
  443. /// <param name="iShowCmd">The i show CMD.</param>
  444. void SetShowCmd(int iShowCmd);
  445. /// <summary>
  446. /// Retrieves the location (path and index) of the icon for a Shell link object
  447. /// </summary>
  448. /// <param name="pszIconPath">The PSZ icon path.</param>
  449. /// <param name="cchIconPath">The CCH icon path.</param>
  450. /// <param name="piIcon">The pi icon.</param>
  451. void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
  452. int cchIconPath, out int piIcon);
  453. /// <summary>
  454. /// Sets the location (path and index) of the icon for a Shell link object
  455. /// </summary>
  456. /// <param name="pszIconPath">The PSZ icon path.</param>
  457. /// <param name="iIcon">The i icon.</param>
  458. void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
  459. /// <summary>
  460. /// Sets the relative path to the Shell link object
  461. /// </summary>
  462. /// <param name="pszPathRel">The PSZ path rel.</param>
  463. /// <param name="dwReserved">The dw reserved.</param>
  464. void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
  465. /// <summary>
  466. /// Attempts to find the target of a Shell link, even if it has been moved or renamed
  467. /// </summary>
  468. /// <param name="hwnd">The HWND.</param>
  469. /// <param name="fFlags">The f flags.</param>
  470. void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
  471. /// <summary>
  472. /// Sets the path and file name of a Shell link object
  473. /// </summary>
  474. /// <param name="pszFile">The PSZ file.</param>
  475. void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  476. }
  477. /// <summary>
  478. /// Interface IPersist
  479. /// </summary>
  480. [ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
  481. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  482. public interface IPersist
  483. {
  484. /// <summary>
  485. /// Gets the class ID.
  486. /// </summary>
  487. /// <param name="pClassID">The p class ID.</param>
  488. [PreserveSig]
  489. void GetClassID(out Guid pClassID);
  490. }
  491. /// <summary>
  492. /// Interface IPersistFile
  493. /// </summary>
  494. [ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
  495. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  496. public interface IPersistFile : IPersist
  497. {
  498. /// <summary>
  499. /// Gets the class ID.
  500. /// </summary>
  501. /// <param name="pClassID">The p class ID.</param>
  502. new void GetClassID(out Guid pClassID);
  503. /// <summary>
  504. /// Determines whether this instance is dirty.
  505. /// </summary>
  506. [PreserveSig]
  507. int IsDirty();
  508. /// <summary>
  509. /// Loads the specified PSZ file name.
  510. /// </summary>
  511. /// <param name="pszFileName">Name of the PSZ file.</param>
  512. /// <param name="dwMode">The dw mode.</param>
  513. [PreserveSig]
  514. void Load([In, MarshalAs(UnmanagedType.LPWStr)]
  515. string pszFileName, uint dwMode);
  516. /// <summary>
  517. /// Saves the specified PSZ file name.
  518. /// </summary>
  519. /// <param name="pszFileName">Name of the PSZ file.</param>
  520. /// <param name="remember">if set to <c>true</c> [remember].</param>
  521. [PreserveSig]
  522. void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  523. [In, MarshalAs(UnmanagedType.Bool)] bool remember);
  524. /// <summary>
  525. /// Saves the completed.
  526. /// </summary>
  527. /// <param name="pszFileName">Name of the PSZ file.</param>
  528. [PreserveSig]
  529. void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  530. /// <summary>
  531. /// Gets the cur file.
  532. /// </summary>
  533. /// <param name="ppszFileName">Name of the PPSZ file.</param>
  534. [PreserveSig]
  535. void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
  536. }
  537. // CLSID_ShellLink from ShlGuid.h
  538. /// <summary>
  539. /// Class ShellLink
  540. /// </summary>
  541. [
  542. ComImport,
  543. Guid("00021401-0000-0000-C000-000000000046")
  544. ]
  545. public class ShellLink
  546. {
  547. }
  548. }