NativeMethods.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 or sets the path.
  201. /// </summary>
  202. /// <value>The path.</value>
  203. public string Path { get; set; }
  204. /// <summary>
  205. /// Returns a <see cref="System.String" /> that represents this instance.
  206. /// </summary>
  207. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  208. public override string ToString()
  209. {
  210. return Path ?? string.Empty;
  211. }
  212. }
  213. /// <summary>
  214. /// Enum SLGP_FLAGS
  215. /// </summary>
  216. [Flags]
  217. public enum SLGP_FLAGS
  218. {
  219. /// <summary>
  220. /// Retrieves the standard short (8.3 format) file name
  221. /// </summary>
  222. SLGP_SHORTPATH = 0x1,
  223. /// <summary>
  224. /// Retrieves the Universal Naming Convention (UNC) path name of the file
  225. /// </summary>
  226. SLGP_UNCPRIORITY = 0x2,
  227. /// <summary>
  228. /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded
  229. /// </summary>
  230. SLGP_RAWPATH = 0x4
  231. }
  232. /// <summary>
  233. /// Enum SLR_FLAGS
  234. /// </summary>
  235. [Flags]
  236. public enum SLR_FLAGS
  237. {
  238. /// <summary>
  239. /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
  240. /// the high-order word of fFlags can be set to a time-out value that specifies the
  241. /// maximum amount of time to be spent resolving the link. The function returns if the
  242. /// link cannot be resolved within the time-out duration. If the high-order word is set
  243. /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
  244. /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
  245. /// duration, in milliseconds.
  246. /// </summary>
  247. SLR_NO_UI = 0x1,
  248. /// <summary>
  249. /// Obsolete and no longer used
  250. /// </summary>
  251. SLR_ANY_MATCH = 0x2,
  252. /// <summary>
  253. /// If the link object has changed, update its path and list of identifiers.
  254. /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
  255. /// whether or not the link object has changed.
  256. /// </summary>
  257. SLR_UPDATE = 0x4,
  258. /// <summary>
  259. /// Do not update the link information
  260. /// </summary>
  261. SLR_NOUPDATE = 0x8,
  262. /// <summary>
  263. /// Do not execute the search heuristics
  264. /// </summary>
  265. SLR_NOSEARCH = 0x10,
  266. /// <summary>
  267. /// Do not use distributed link tracking
  268. /// </summary>
  269. SLR_NOTRACK = 0x20,
  270. /// <summary>
  271. /// Disable distributed link tracking. By default, distributed link tracking tracks
  272. /// removable media across multiple devices based on the volume name. It also uses the
  273. /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
  274. /// has changed. Setting SLR_NOLINKINFO disables both types of tracking.
  275. /// </summary>
  276. SLR_NOLINKINFO = 0x40,
  277. /// <summary>
  278. /// Call the Microsoft Windows Installer
  279. /// </summary>
  280. SLR_INVOKE_MSI = 0x80
  281. }
  282. /// <summary>
  283. /// The IShellLink interface allows Shell links to be created, modified, and resolved
  284. /// </summary>
  285. [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
  286. public interface IShellLinkW
  287. {
  288. /// <summary>
  289. /// Retrieves the path and file name of a Shell link object
  290. /// </summary>
  291. /// <param name="pszFile">The PSZ file.</param>
  292. /// <param name="cchMaxPath">The CCH max path.</param>
  293. /// <param name="pfd">The PFD.</param>
  294. /// <param name="fFlags">The f flags.</param>
  295. void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags);
  296. /// <summary>
  297. /// Retrieves the list of item identifiers for a Shell link object
  298. /// </summary>
  299. /// <param name="ppidl">The ppidl.</param>
  300. void GetIDList(out IntPtr ppidl);
  301. /// <summary>
  302. /// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
  303. /// </summary>
  304. /// <param name="pidl">The pidl.</param>
  305. void SetIDList(IntPtr pidl);
  306. /// <summary>
  307. /// Retrieves the description string for a Shell link object
  308. /// </summary>
  309. /// <param name="pszName">Name of the PSZ.</param>
  310. /// <param name="cchMaxName">Name of the CCH max.</param>
  311. void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
  312. /// <summary>
  313. /// Sets the description for a Shell link object. The description can be any application-defined string
  314. /// </summary>
  315. /// <param name="pszName">Name of the PSZ.</param>
  316. void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
  317. /// <summary>
  318. /// Retrieves the name of the working directory for a Shell link object
  319. /// </summary>
  320. /// <param name="pszDir">The PSZ dir.</param>
  321. /// <param name="cchMaxPath">The CCH max path.</param>
  322. void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
  323. /// <summary>
  324. /// Sets the name of the working directory for a Shell link object
  325. /// </summary>
  326. /// <param name="pszDir">The PSZ dir.</param>
  327. void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  328. /// <summary>
  329. /// Retrieves the command-line arguments associated with a Shell link object
  330. /// </summary>
  331. /// <param name="pszArgs">The PSZ args.</param>
  332. /// <param name="cchMaxPath">The CCH max path.</param>
  333. void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
  334. /// <summary>
  335. /// Sets the command-line arguments for a Shell link object
  336. /// </summary>
  337. /// <param name="pszArgs">The PSZ args.</param>
  338. void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  339. /// <summary>
  340. /// Retrieves the hot key for a Shell link object
  341. /// </summary>
  342. /// <param name="pwHotkey">The pw hotkey.</param>
  343. void GetHotkey(out short pwHotkey);
  344. /// <summary>
  345. /// Sets a hot key for a Shell link object
  346. /// </summary>
  347. /// <param name="wHotkey">The w hotkey.</param>
  348. void SetHotkey(short wHotkey);
  349. /// <summary>
  350. /// Retrieves the show command for a Shell link object
  351. /// </summary>
  352. /// <param name="piShowCmd">The pi show CMD.</param>
  353. void GetShowCmd(out int piShowCmd);
  354. /// <summary>
  355. /// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
  356. /// </summary>
  357. /// <param name="iShowCmd">The i show CMD.</param>
  358. void SetShowCmd(int iShowCmd);
  359. /// <summary>
  360. /// Retrieves the location (path and index) of the icon for a Shell link object
  361. /// </summary>
  362. /// <param name="pszIconPath">The PSZ icon path.</param>
  363. /// <param name="cchIconPath">The CCH icon path.</param>
  364. /// <param name="piIcon">The pi icon.</param>
  365. void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
  366. int cchIconPath, out int piIcon);
  367. /// <summary>
  368. /// Sets the location (path and index) of the icon for a Shell link object
  369. /// </summary>
  370. /// <param name="pszIconPath">The PSZ icon path.</param>
  371. /// <param name="iIcon">The i icon.</param>
  372. void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
  373. /// <summary>
  374. /// Sets the relative path to the Shell link object
  375. /// </summary>
  376. /// <param name="pszPathRel">The PSZ path rel.</param>
  377. /// <param name="dwReserved">The dw reserved.</param>
  378. void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
  379. /// <summary>
  380. /// Attempts to find the target of a Shell link, even if it has been moved or renamed
  381. /// </summary>
  382. /// <param name="hwnd">The HWND.</param>
  383. /// <param name="fFlags">The f flags.</param>
  384. void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
  385. /// <summary>
  386. /// Sets the path and file name of a Shell link object
  387. /// </summary>
  388. /// <param name="pszFile">The PSZ file.</param>
  389. void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  390. }
  391. /// <summary>
  392. /// Interface IPersist
  393. /// </summary>
  394. [ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
  395. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  396. public interface IPersist
  397. {
  398. /// <summary>
  399. /// Gets the class ID.
  400. /// </summary>
  401. /// <param name="pClassID">The p class ID.</param>
  402. [PreserveSig]
  403. void GetClassID(out Guid pClassID);
  404. }
  405. /// <summary>
  406. /// Interface IPersistFile
  407. /// </summary>
  408. [ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
  409. InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  410. public interface IPersistFile : IPersist
  411. {
  412. /// <summary>
  413. /// Gets the class ID.
  414. /// </summary>
  415. /// <param name="pClassID">The p class ID.</param>
  416. new void GetClassID(out Guid pClassID);
  417. /// <summary>
  418. /// Determines whether this instance is dirty.
  419. /// </summary>
  420. [PreserveSig]
  421. int IsDirty();
  422. /// <summary>
  423. /// Loads the specified PSZ file name.
  424. /// </summary>
  425. /// <param name="pszFileName">Name of the PSZ file.</param>
  426. /// <param name="dwMode">The dw mode.</param>
  427. [PreserveSig]
  428. void Load([In, MarshalAs(UnmanagedType.LPWStr)]
  429. string pszFileName, uint dwMode);
  430. /// <summary>
  431. /// Saves the specified PSZ file name.
  432. /// </summary>
  433. /// <param name="pszFileName">Name of the PSZ file.</param>
  434. /// <param name="remember">if set to <c>true</c> [remember].</param>
  435. [PreserveSig]
  436. void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  437. [In, MarshalAs(UnmanagedType.Bool)] bool remember);
  438. /// <summary>
  439. /// Saves the completed.
  440. /// </summary>
  441. /// <param name="pszFileName">Name of the PSZ file.</param>
  442. [PreserveSig]
  443. void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  444. /// <summary>
  445. /// Gets the cur file.
  446. /// </summary>
  447. /// <param name="ppszFileName">Name of the PPSZ file.</param>
  448. [PreserveSig]
  449. void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
  450. }
  451. // CLSID_ShellLink from ShlGuid.h
  452. /// <summary>
  453. /// Class ShellLink
  454. /// </summary>
  455. [
  456. ComImport,
  457. Guid("00021401-0000-0000-C000-000000000046")
  458. ]
  459. public class ShellLink
  460. {
  461. }
  462. }