/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security;
namespace Alphaleonis.Win32.Filesystem
{
internal static partial class NativeMethods
{
#region CM_Xxx
/// The CM_Connect_Machine function creates a connection to a remote machine.
///
/// Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.
/// You cannot access remote machines when running on these versions of Windows.
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// Name of the unc server.
/// [out] The ph machine.
///
/// If the operation succeeds, the function returns CR_SUCCESS.
/// Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CM_Connect_MachineW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int CM_Connect_Machine([MarshalAs(UnmanagedType.LPWStr)] string uncServerName, out SafeCmConnectMachineHandle phMachine);
///
/// The CM_Get_Device_ID_Ex function retrieves the device instance ID for a specified device instance on a local or a remote machine.
///
///
/// Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.
/// You cannot access remote machines when running on these versions of Windows.
///
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// The dn development instance.
/// The buffer.
/// Length of the buffer.
/// The ul flags.
/// The machine.
///
/// If the operation succeeds, the function returns CR_SUCCESS.
/// Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CM_Get_Device_ID_ExW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.I4)]
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);
///
/// The CM_Disconnect_Machine function removes a connection to a remote machine.
///
///
/// Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.
/// You cannot access remote machines when running on these versions of Windows.
/// SetLastError is set to .
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// The machine.
///
/// If the operation succeeds, the function returns CR_SUCCESS.
/// Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.I4)]
internal static extern int CM_Disconnect_Machine(IntPtr hMachine);
///
/// The CM_Get_Parent_Ex function obtains a device instance handle to the parent node of a specified device node (devnode) in a local
/// or a remote machine's device tree.
///
///
/// Beginning in Windows 8 and Windows Server 2012 functionality to access remote machines has been removed.
/// You cannot access remote machines when running on these versions of Windows.
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// [out] The pdn development instance.
/// The dn development instance.
/// The ul flags.
/// The machine.
///
/// If the operation succeeds, the function returns CR_SUCCESS.
/// Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.I4)]
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);
#endregion // CM_Xxx
#region DeviceIoControl
/// Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
///
/// If the operation completes successfully, the return value is nonzero.
/// If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.
///
///
/// To retrieve a handle to the device, you must call the function with either the name of a device or
/// the name of the driver associated with a device.
/// To specify a device name, use the following format: \\.\DeviceName
/// Minimum supported client: Windows XP
/// Minimum supported server: Windows Server 2003
///
/// The device.
/// The i/o control code.
/// Buffer for in data.
/// Size of the in buffer.
/// Buffer for out data.
/// Size of the out buffer.
/// [out] The bytes returned.
/// The overlapped.
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
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);
/// Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
///
/// If the operation completes successfully, the return value is nonzero.
/// If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.
///
///
/// To retrieve a handle to the device, you must call the function with either the name of a device or
/// the name of the driver associated with a device.
/// To specify a device name, use the following format: \\.\DeviceName
/// Minimum supported client: Windows XP
/// Minimum supported server: Windows Server 2003
///
/// The device.
/// The i/o control code.
/// Buffer for in data.
/// Size of the in buffer.
/// Buffer for out data.
/// Size of the out buffer.
/// [out] The bytes returned.
/// The overlapped.
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
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);
#endregion // DeviceIoControl
#region SetupDiXxx
///
/// The SetupDiDestroyDeviceInfoList function deletes a device information set and frees all associated memory.
///
///
/// SetLastError is set to .
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// Information describing the development.
///
/// The function returns TRUE if it is successful.
/// Otherwise, it returns FALSE and the logged error can be retrieved with a call to GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
///
/// The SetupDiEnumDeviceInterfaces function enumerates the device interfaces that are contained in a device information set.
///
///
/// Repeated calls to this function return an structure for a different device
/// interface.
/// This function can be called repeatedly to get information about interfaces in a device information set that are
/// associated
/// with a particular device information element or that are associated with all device information elements.
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// Information describing the development.
/// Information describing the development.
/// [in,out] Unique identifier for the interface class.
/// Zero-based index of the member.
/// [in,out] Information describing the device interface.
///
/// SetupDiEnumDeviceInterfaces returns TRUE if the function completed without error.
/// If the function completed with an error, FALSE is returned and the error code for the failure can be retrieved by calling
/// GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiEnumDeviceInterfaces(SafeHandle hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, [MarshalAs(UnmanagedType.U4)] uint memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
///
/// The SetupDiGetClassDevsEx function returns a handle to a device information set that contains requested device information elements
/// for a local or a remote computer.
///
///
/// The caller of SetupDiGetClassDevsEx must delete the returned device information set when it is no longer needed by calling
/// .
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// [in,out] Unique identifier for the class.
/// The enumerator.
/// The parent.
/// The devs ex flags.
/// Set the device information belongs to.
/// Name of the machine.
/// The reserved.
///
/// If the operation succeeds, SetupDiGetClassDevsEx returns a handle to a device information set that contains all installed
/// devices that matched the supplied parameters.
/// If the operation fails, the function returns INVALID_HANDLE_VALUE. To get extended error information, call
/// GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
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);
///
/// The SetupDiGetDeviceInterfaceDetail function returns details about a device interface.
///
///
/// The interface detail returned by this function consists of a device path that can be passed to Win32 functions such as
/// CreateFile.
/// Do not attempt to parse the device path symbolic name. The device path can be reused across system starts.
/// Available in Microsoft Windows 2000 and later versions of Windows.
///
/// Information describing the development.
/// [in,out] Information describing the device interface.
/// [in,out] Information describing the device interface detail.
/// Size of the device interface detail data.
/// Size of the required.
/// [in,out] Information describing the device information.
///
/// SetupDiGetDeviceInterfaceDetail returns TRUE if the function completed without error.
/// If the function completed with an error, FALSE is returned and the error code for the failure can be retrieved by calling
/// GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
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);
///
/// The SetupDiGetDeviceRegistryProperty function retrieves a specified Plug and Play device property.
///
/// Available in Microsoft Windows 2000 and later versions of Windows.
/// Set the device information belongs to.
/// [in,out] Information describing the device information.
/// The property.
/// [out] Type of the property register data.
/// Buffer for property data.
/// Size of the property buffer.
/// Size of the required.
///
/// SetupDiGetDeviceRegistryProperty returns TRUE if the call was successful.
/// Otherwise, it returns FALSE and the logged error can be retrieved by making a call to GetLastError.
/// SetupDiGetDeviceRegistryProperty returns the ERROR_INVALID_DATA error code if the requested property does not exist for a
/// device or if the property data is not valid.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
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);
#endregion // SetupDiXxx
}
}