Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NativeMethods.DeviceManagement.cs 20 KiB

pirms 8 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using Microsoft.Win32.SafeHandles;
  22. using System;
  23. using System.Diagnostics.CodeAnalysis;
  24. using System.Runtime.InteropServices;
  25. using System.Security;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. internal static partial class NativeMethods
  29. {
  30. #region CM_Xxx
  31. /// <summary>The CM_Connect_Machine function creates a connection to a remote machine.</summary>
  32. /// <remarks>
  33. /// <para>Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.</para>
  34. /// <para>You cannot access remote machines when running on these versions of Windows.</para>
  35. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  36. /// </remarks>
  37. /// <param name="uncServerName">Name of the unc server.</param>
  38. /// <param name="phMachine">[out] The ph machine.</param>
  39. /// <returns>
  40. /// <para>If the operation succeeds, the function returns CR_SUCCESS.</para>
  41. /// <para>Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.</para>
  42. /// </returns>
  43. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  44. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CM_Connect_MachineW"), SuppressUnmanagedCodeSecurity]
  45. [return: MarshalAs(UnmanagedType.I4)]
  46. public static extern int CM_Connect_Machine([MarshalAs(UnmanagedType.LPWStr)] string uncServerName, out SafeCmConnectMachineHandle phMachine);
  47. /// <summary>
  48. /// The CM_Get_Device_ID_Ex function retrieves the device instance ID for a specified device instance on a local or a remote machine.
  49. /// </summary>
  50. /// <remarks>
  51. /// <para>Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.</para>
  52. /// <para>You cannot access remote machines when running on these versions of Windows.</para>
  53. /// <para>&#160;</para>
  54. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  55. /// </remarks>
  56. /// <param name="dnDevInst">The dn development instance.</param>
  57. /// <param name="buffer">The buffer.</param>
  58. /// <param name="bufferLen">Length of the buffer.</param>
  59. /// <param name="ulFlags">The ul flags.</param>
  60. /// <param name="hMachine">The machine.</param>
  61. /// <returns>
  62. /// <para>If the operation succeeds, the function returns CR_SUCCESS.</para>
  63. /// <para>Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.</para>
  64. /// </returns>
  65. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  66. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CM_Get_Device_ID_ExW"), SuppressUnmanagedCodeSecurity]
  67. [return: MarshalAs(UnmanagedType.I4)]
  68. public static extern int CM_Get_Device_ID_Ex([MarshalAs(UnmanagedType.U4)] uint dnDevInst, SafeGlobalMemoryBufferHandle buffer, [MarshalAs(UnmanagedType.U4)] uint bufferLen, [MarshalAs(UnmanagedType.U4)] uint ulFlags, SafeCmConnectMachineHandle hMachine);
  69. /// <summary>
  70. /// The CM_Disconnect_Machine function removes a connection to a remote machine.
  71. /// </summary>
  72. /// <remarks>
  73. /// <para>Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.</para>
  74. /// <para>You cannot access remote machines when running on these versions of Windows.</para>
  75. /// <para>SetLastError is set to <see langword="false"/>.</para>
  76. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  77. /// </remarks>
  78. /// <param name="hMachine">The machine.</param>
  79. /// <returns>
  80. /// <para>If the operation succeeds, the function returns CR_SUCCESS.</para>
  81. /// <para>Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.</para>
  82. /// </returns>
  83. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  84. [DllImport("setupapi.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  85. [return: MarshalAs(UnmanagedType.I4)]
  86. internal static extern int CM_Disconnect_Machine(IntPtr hMachine);
  87. /// <summary>
  88. /// The CM_Get_Parent_Ex function obtains a device instance handle to the parent node of a specified device node (devnode) in a local
  89. /// or a remote machine's device tree.
  90. /// </summary>
  91. /// <remarks>
  92. /// <para>Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.</para>
  93. /// <para>You cannot access remote machines when running on these versions of Windows.</para>
  94. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  95. /// </remarks>
  96. /// <param name="pdnDevInst">[out] The pdn development instance.</param>
  97. /// <param name="dnDevInst">The dn development instance.</param>
  98. /// <param name="ulFlags">The ul flags.</param>
  99. /// <param name="hMachine">The machine.</param>
  100. /// <returns>
  101. /// <para>If the operation succeeds, the function returns CR_SUCCESS.</para>
  102. /// <para>Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.</para>
  103. /// </returns>
  104. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  105. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  106. [return: MarshalAs(UnmanagedType.I4)]
  107. internal static extern int CM_Get_Parent_Ex([MarshalAs(UnmanagedType.U4)] out uint pdnDevInst, [MarshalAs(UnmanagedType.U4)] uint dnDevInst, [MarshalAs(UnmanagedType.U4)] uint ulFlags, SafeCmConnectMachineHandle hMachine);
  108. #endregion // CM_Xxx
  109. #region DeviceIoControl
  110. /// <summary>Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.</summary>
  111. /// <returns>
  112. /// <para>If the operation completes successfully, the return value is nonzero.</para>
  113. /// <para>If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.</para>
  114. /// </returns>
  115. /// <remarks>
  116. /// <para>To retrieve a handle to the device, you must call the <see cref="CreateFile"/> function with either the name of a device or
  117. /// the name of the driver associated with a device.</para>
  118. /// <para>To specify a device name, use the following format: <c>\\.\DeviceName</c></para>
  119. /// <para>Minimum supported client: Windows XP</para>
  120. /// <para>Minimum supported server: Windows Server 2003</para>
  121. /// </remarks>
  122. /// <param name="hDevice">The device.</param>
  123. /// <param name="dwIoControlCode">The i/o control code.</param>
  124. /// <param name="lpInBuffer">Buffer for in data.</param>
  125. /// <param name="nInBufferSize">Size of the in buffer.</param>
  126. /// <param name="lpOutBuffer">Buffer for out data.</param>
  127. /// <param name="nOutBufferSize">Size of the out buffer.</param>
  128. /// <param name="lpBytesReturned">[out] The bytes returned.</param>
  129. /// <param name="lpOverlapped">The overlapped.</param>
  130. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  131. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  132. [return: MarshalAs(UnmanagedType.Bool)]
  133. internal static extern bool DeviceIoControl(SafeFileHandle hDevice, [MarshalAs(UnmanagedType.U4)] uint dwIoControlCode, IntPtr lpInBuffer, [MarshalAs(UnmanagedType.U4)] uint nInBufferSize, SafeGlobalMemoryBufferHandle lpOutBuffer, [MarshalAs(UnmanagedType.U4)] uint nOutBufferSize, [MarshalAs(UnmanagedType.U4)] out uint lpBytesReturned, IntPtr lpOverlapped);
  134. /// <summary>Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.</summary>
  135. /// <returns>
  136. /// <para>If the operation completes successfully, the return value is nonzero.</para>
  137. /// <para>If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.</para>
  138. /// </returns>
  139. /// <remarks>
  140. /// <para>To retrieve a handle to the device, you must call the <see cref="CreateFile"/> function with either the name of a device or
  141. /// the name of the driver associated with a device.</para>
  142. /// <para>To specify a device name, use the following format: <c>\\.\DeviceName</c></para>
  143. /// <para>Minimum supported client: Windows XP</para>
  144. /// <para>Minimum supported server: Windows Server 2003</para>
  145. /// </remarks>
  146. /// <param name="hDevice">The device.</param>
  147. /// <param name="dwIoControlCode">The i/o control code.</param>
  148. /// <param name="lpInBuffer">Buffer for in data.</param>
  149. /// <param name="nInBufferSize">Size of the in buffer.</param>
  150. /// <param name="lpOutBuffer">Buffer for out data.</param>
  151. /// <param name="nOutBufferSize">Size of the out buffer.</param>
  152. /// <param name="lpBytesReturned">[out] The bytes returned.</param>
  153. /// <param name="lpOverlapped">The overlapped.</param>
  154. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  155. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  156. [return: MarshalAs(UnmanagedType.Bool)]
  157. internal static extern bool DeviceIoControl(SafeFileHandle hDevice, [MarshalAs(UnmanagedType.U4)] uint dwIoControlCode, [MarshalAs(UnmanagedType.AsAny)] object lpInBuffer, [MarshalAs(UnmanagedType.U4)] uint nInBufferSize, [MarshalAs(UnmanagedType.AsAny)] [Out] object lpOutBuffer, [MarshalAs(UnmanagedType.U4)] uint nOutBufferSize, [MarshalAs(UnmanagedType.U4)] out uint lpBytesReturned, IntPtr lpOverlapped);
  158. #endregion // DeviceIoControl
  159. #region SetupDiXxx
  160. /// <summary>
  161. /// The SetupDiDestroyDeviceInfoList function deletes a device information set and frees all associated memory.
  162. /// </summary>
  163. /// <remarks>
  164. /// <para>SetLastError is set to <see langword="false"/>.</para>
  165. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  166. /// </remarks>
  167. /// <param name="hDevInfo">Information describing the development.</param>
  168. /// <returns>
  169. /// <para>The function returns TRUE if it is successful.</para>
  170. /// <para>Otherwise, it returns FALSE and the logged error can be retrieved with a call to GetLastError.</para>
  171. /// </returns>
  172. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  173. [DllImport("setupapi.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  174. [return: MarshalAs(UnmanagedType.Bool)]
  175. private static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
  176. /// <summary>
  177. /// The SetupDiEnumDeviceInterfaces function enumerates the device interfaces that are contained in a device information set.
  178. /// </summary>
  179. /// <remarks>
  180. /// <para>Repeated calls to this function return an <see cref="SP_DEVICE_INTERFACE_DATA"/> structure for a different device
  181. /// interface.</para>
  182. /// <para>This function can be called repeatedly to get information about interfaces in a device information set that are
  183. /// associated</para>
  184. /// <para>with a particular device information element or that are associated with all device information elements.</para>
  185. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  186. /// </remarks>
  187. /// <param name="hDevInfo">Information describing the development.</param>
  188. /// <param name="devInfo">Information describing the development.</param>
  189. /// <param name="interfaceClassGuid">[in,out] Unique identifier for the interface class.</param>
  190. /// <param name="memberIndex">Zero-based index of the member.</param>
  191. /// <param name="deviceInterfaceData">[in,out] Information describing the device interface.</param>
  192. /// <returns>
  193. /// <para>SetupDiEnumDeviceInterfaces returns TRUE if the function completed without error.</para>
  194. /// <para>If the function completed with an error, FALSE is returned and the error code for the failure can be retrieved by calling
  195. /// GetLastError.</para>
  196. /// </returns>
  197. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  198. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  199. [return: MarshalAs(UnmanagedType.Bool)]
  200. internal static extern bool SetupDiEnumDeviceInterfaces(SafeHandle hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, [MarshalAs(UnmanagedType.U4)] uint memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
  201. /// <summary>
  202. /// The SetupDiGetClassDevsEx function returns a handle to a device information set that contains requested device information elements
  203. /// for a local or a remote computer.
  204. /// </summary>
  205. /// <remarks>
  206. /// <para>The caller of SetupDiGetClassDevsEx must delete the returned device information set when it is no longer needed by calling
  207. /// <see cref="SetupDiDestroyDeviceInfoList"/>.</para>
  208. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  209. /// </remarks>
  210. /// <param name="classGuid">[in,out] Unique identifier for the class.</param>
  211. /// <param name="enumerator">The enumerator.</param>
  212. /// <param name="hwndParent">The parent.</param>
  213. /// <param name="devsExFlags">The devs ex flags.</param>
  214. /// <param name="deviceInfoSet">Set the device information belongs to.</param>
  215. /// <param name="machineName">Name of the machine.</param>
  216. /// <param name="reserved">The reserved.</param>
  217. /// <returns>
  218. /// <para>If the operation succeeds, SetupDiGetClassDevsEx returns a handle to a device information set that contains all installed
  219. /// devices that matched the supplied parameters.</para>
  220. /// <para>If the operation fails, the function returns INVALID_HANDLE_VALUE. To get extended error information, call
  221. /// GetLastError.</para>
  222. /// </returns>
  223. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  224. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  225. internal static extern SafeSetupDiClassDevsExHandle SetupDiGetClassDevsEx(ref Guid classGuid, IntPtr enumerator, IntPtr hwndParent, [MarshalAs(UnmanagedType.U4)] SetupDiGetClassDevsExFlags devsExFlags, IntPtr deviceInfoSet, [MarshalAs(UnmanagedType.LPWStr)] string machineName, IntPtr reserved);
  226. /// <summary>
  227. /// The SetupDiGetDeviceInterfaceDetail function returns details about a device interface.
  228. /// </summary>
  229. /// <remarks>
  230. /// <para>The interface detail returned by this function consists of a device path that can be passed to Win32 functions such as
  231. /// CreateFile.</para>
  232. /// <para>Do not attempt to parse the device path symbolic name. The device path can be reused across system starts.</para>
  233. /// <para>Available in Microsoft Windows 2000 and later versions of Windows.</para>
  234. /// </remarks>
  235. /// <param name="hDevInfo">Information describing the development.</param>
  236. /// <param name="deviceInterfaceData">[in,out] Information describing the device interface.</param>
  237. /// <param name="deviceInterfaceDetailData">[in,out] Information describing the device interface detail.</param>
  238. /// <param name="deviceInterfaceDetailDataSize">Size of the device interface detail data.</param>
  239. /// <param name="requiredSize">Size of the required.</param>
  240. /// <param name="deviceInfoData">[in,out] Information describing the device information.</param>
  241. /// <returns>
  242. /// <para>SetupDiGetDeviceInterfaceDetail returns TRUE if the function completed without error.</para>
  243. /// <para>If the function completed with an error, FALSE is returned and the error code for the failure can be retrieved by calling
  244. /// GetLastError.</para>
  245. /// </returns>
  246. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  247. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  248. [return: MarshalAs(UnmanagedType.Bool)]
  249. internal static extern bool SetupDiGetDeviceInterfaceDetail(SafeHandle hDevInfo, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, [MarshalAs(UnmanagedType.U4)] uint deviceInterfaceDetailDataSize, IntPtr requiredSize, ref SP_DEVINFO_DATA deviceInfoData);
  250. /// <summary>
  251. /// The SetupDiGetDeviceRegistryProperty function retrieves a specified Plug and Play device property.
  252. /// </summary>
  253. /// <remarks><para>Available in Microsoft Windows 2000 and later versions of Windows.</para></remarks>
  254. /// <param name="deviceInfoSet">Set the device information belongs to.</param>
  255. /// <param name="deviceInfoData">[in,out] Information describing the device information.</param>
  256. /// <param name="property">The property.</param>
  257. /// <param name="propertyRegDataType">[out] Type of the property register data.</param>
  258. /// <param name="propertyBuffer">Buffer for property data.</param>
  259. /// <param name="propertyBufferSize">Size of the property buffer.</param>
  260. /// <param name="requiredSize">Size of the required.</param>
  261. /// <returns>
  262. /// <para>SetupDiGetDeviceRegistryProperty returns TRUE if the call was successful.</para>
  263. /// <para>Otherwise, it returns FALSE and the logged error can be retrieved by making a call to GetLastError.</para>
  264. /// <para>SetupDiGetDeviceRegistryProperty returns the ERROR_INVALID_DATA error code if the requested property does not exist for a
  265. /// device or if the property data is not valid.</para>
  266. /// </returns>
  267. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  268. [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  269. [return: MarshalAs(UnmanagedType.Bool)]
  270. internal static extern bool SetupDiGetDeviceRegistryProperty(SafeHandle deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property, [MarshalAs(UnmanagedType.U4)] out uint propertyRegDataType, SafeGlobalMemoryBufferHandle propertyBuffer, [MarshalAs(UnmanagedType.U4)] uint propertyBufferSize, IntPtr requiredSize);
  271. #endregion // SetupDiXxx
  272. }
  273. }