public static IEnumerable <DeviceInfo> GetDevices(Guid deviceClass, DiGetClassFlags flags = DiGetClassFlags.DIGCF_DEFAULT) { var enumerator = default(SetupApiDeviceEnumerator); if (!SetupApi_Enumerate(deviceClass, flags, ref enumerator)) { throw new Win32Exception(); } try { while (SetupApi_Next(ref enumerator)) { yield return(new DeviceInfo { FriendlyName = ReadDeviceProperty(ref enumerator, DeviceRegistryCode.SPDRP_FRIENDLYNAME) as string, Path = enumerator.current, }); } if (enumerator.wasError) { throw new Win32Exception(); } } finally { SetupApi_Done(ref enumerator); } }
public static extern IntPtr SetupDiGetClassDevs ( ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, DiGetClassFlags Flags );
/// <summary> /// Initializes a new instance of the <see cref="DeviceEnumerator"/> class. /// </summary> /// <param name="deviceClass">The identifier of the associated devices.</param> /// <param name="flags">The flags used to defined the type of expected devices.</param> public DeviceEnumerator(Guid deviceClass, DiGetClassFlags flags) { this.deviceClass = deviceClass; this.deviceSet = SetupApiMethods.GetClassDevs(ref deviceClass, null, IntPtr.Zero, flags); this.interfaceData = new DeviceInterfaceData(); this.index = -1; }
/// <summary> /// Get list of device nodes. /// </summary> /// <param name="all_devices">Return all devices including ones which aren't present.</param> /// <returns>The list of device nodes.</returns> public static IEnumerable <DeviceNode> GetDeviceNodeList(bool all_devices) { DiGetClassFlags flags = DiGetClassFlags.ALLCLASSES; if (!all_devices) { flags |= DiGetClassFlags.PRESENT; } return(GetDeviceList(null, null, flags)); }
public DeviceInformationSet(Guid classGuid, DiGetClassFlags flags) { // We start at the "root" of the device tree and look for all // devices that match the interface GUID of a disk _handle = SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, (uint)(flags)); if (_handle == _invalidHandleValue) { var lastError = Marshal.GetLastWin32Error(); throw new Win32Exception(lastError, "SetupDiGetClassDevs failed"); } }
public DeviceInfoSet( IntPtr classGuid_, IntPtr enumerator_, IntPtr hwndParent_, DiGetClassFlags flags_) { devInfoSet = SetupDiGetClassDevs( classGuid_, enumerator_, hwndParent_, flags_ ); }
public DeviceInfoSet( ref Guid classGuid_, [MarshalAs(UnmanagedType.LPTStr)] string enumerator_, IntPtr hwndParent_, DiGetClassFlags flags_) { devInfoSet = SetupDiGetClassDevs( ref classGuid_, enumerator_, hwndParent_, flags_ ); }
internal static extern IntPtr SetupDiGetClassDevs(ref Guid classGuid, [MarshalAs(UnmanagedType.LPTStr)] string enumerator, IntPtr hwndParent, DiGetClassFlags flags);
public static extern IntPtr SetupDiGetClassDevs( ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, DiGetClassFlags Flags);
public static extern IntPtr SetupDiGetClassDevsEx(ref Guid guidClass, [MarshalAs(UnmanagedType.LPWStr)] string enumerator, IntPtr hwndParent, DiGetClassFlags flags, IntPtr deviceInfoSet, [MarshalAs(UnmanagedType.LPWStr)] string machineName, IntPtr reserved);
static extern IntPtr SetupDiGetClassDevs(IntPtr classGuid, string enumerator, IntPtr hwndParent, DiGetClassFlags flags);
public static extern IntPtr SetupDiGetClassDevs( ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPTStr)][Optional] string Enumerator, [Optional] IntPtr hwndParent, DiGetClassFlags Flags);
public extern static IntPtr SetupDiGetClassDevs( // 1st form using a ClassGUID only, with null Enumerator ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, DiGetClassFlags Flags );
/// <summary> /// Get list of device entries. /// </summary> /// <param name="class_guid">Specify the Device Setup Class GUID.</param> /// <param name="all_devices">Only return present devices.</param> /// <returns>The list of device entries.</returns> public static IEnumerable <DeviceNode> GetDeviceNodeList(Guid class_guid, bool all_devices) { DiGetClassFlags flags = !all_devices ? DiGetClassFlags.PRESENT : 0; return(GetDeviceList(class_guid, null, flags)); }
public static extern IntPtr SetupDiGetClassDevs( [In, Optional] ref Guid ClassGuid, [In, Optional][MarshalAs(UnmanagedType.LPWStr)] string Enumerator, [In, Optional] IntPtr hwndParent, [In] DiGetClassFlags Flags);
internal static extern DeviceInfoSetSafeHandle SetupDiGetClassDevs( ref Guid classGuid, IntPtr enumerator, IntPtr hwndParent, DiGetClassFlags flags);
private static extern IntPtr SetupDiGetClassDevs(ref Guid classGuid, IntPtr iEnumerator, IntPtr hParent, DiGetClassFlags nFlags);
public static unsafe void EnumerateDevices <TContext> ( string machine, DiGetClassFlags flags, string[] deviceClassNames, EnumerateDevicesFunc <TContext> callback, TContext context ) where TContext : class { DeviceInfoListHandle devs = null; var devInfo = new SP_DEVINFO_DATA(); var devInfoListDetail = new SP_DEVINFO_LIST_DETAIL_DATA(); Guid[] deviceClassGuids; try { int numClasses = 0; if (deviceClassNames != null) { deviceClassGuids = new Guid[deviceClassNames.Length]; foreach (var className in deviceClassNames) { UInt32 numResults; if ( !SetupDiClassGuidsFromNameEx( className, out deviceClassGuids[numClasses], 1, out numResults, machine, IntPtr.Zero ) && (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) ) { throw new Exception("Unable to resolve class name '" + className + "'"); } numClasses += 1; } var newClassGuids = new Guid[numClasses]; Array.Copy(deviceClassGuids, newClassGuids, numClasses); deviceClassGuids = newClassGuids; } else { deviceClassGuids = new Guid[0]; } if (deviceClassGuids.Length > 0) { for (int i = 0; i < deviceClassGuids.Length; i++) { fixed(Guid *pGuid = &(deviceClassGuids[i])) { var existingPtr = IntPtr.Zero; if (devs != null) { existingPtr = devs.DangerousGetHandle(); } var result = SetupDiGetClassDevsEx( new IntPtr(pGuid), null, IntPtr.Zero, flags, existingPtr, machine, IntPtr.Zero ); var lastError = Marshal.GetLastWin32Error(); if (lastError != 0) { throw new Win32Exception(lastError); } if (devs == null) { devs = new DeviceInfoListHandle(result, true); } } } } else { devs = new DeviceInfoListHandle( SetupDiGetClassDevsEx( IntPtr.Zero, null, IntPtr.Zero, flags | DiGetClassFlags.DIGCF_ALLCLASSES, IntPtr.Zero, machine, IntPtr.Zero ), true ); var lastError = Marshal.GetLastWin32Error(); if (lastError != 0) { throw new Win32Exception(lastError); } } if (devs.IsInvalid) { throw new Exception("Failed to create device info list"); } devInfoListDetail.cbSize = (UInt32)Marshal.SizeOf(devInfoListDetail.GetType()); if (!SetupDiGetDeviceInfoListDetail(devs.DangerousGetHandle(), ref devInfoListDetail)) { var lastError = Marshal.GetLastWin32Error(); if (lastError != 0) { throw new Win32Exception(lastError); } return; } devInfo.cbSize = (UInt32)Marshal.SizeOf(devInfo.GetType()); for (UInt32 devIndex = 0; SetupDiEnumDeviceInfo(devs.DangerousGetHandle(), devIndex, ref devInfo); devIndex++) { callback(devs, ref devInfo, GetDeviceId(ref devInfo), context); } { var lastError = Marshal.GetLastWin32Error(); if ((lastError != 0) && (lastError != ERROR_NO_MORE_ITEMS)) { throw new Win32Exception(lastError); } } } finally { if (devs != null && !devs.IsClosed) { devs.Close(); } } }
public static extern SafeDeviceInfoSetHandle SetupDiGetClassDevs(IntPtr ClassGuid, [MarshalAs(UnmanagedType.LPWStr)] string?Enumerator, IntPtr hwndParent, DiGetClassFlags Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] // 2nd form uses an Enumerator only, with null ClassGUID public extern static IntPtr SetupDiGetClassDevs( IntPtr ClassGuid, string Enumerator, IntPtr hwndParent, DiGetClassFlags Flags );
private static IEnumerable <DeviceNode> GetDeviceList(OptionalGuid class_guid, string enumerator, DiGetClassFlags flags) { var devices = new List <DeviceNode>(); DeviceNativeMethods.CM_Locate_DevNodeW(out int root, null, 0).ToNtStatus().ToNtException(); using (var p = DeviceNativeMethods.SetupDiGetClassDevsW(class_guid, enumerator, IntPtr.Zero, flags)) { if (p.IsInvalid) { Win32Utils.GetLastWin32Error().ToNtException(); } int index = 0; int size = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); SP_DEVINFO_DATA dev_info = new SP_DEVINFO_DATA() { cbSize = size }; while (DeviceNativeMethods.SetupDiEnumDeviceInfo(p, index++, ref dev_info)) { if (dev_info.DevInst != root) { devices.Add(new DeviceNode(dev_info.DevInst)); } dev_info.cbSize = size; } return(devices.AsReadOnly()); } }
/// <summary> /// Initializes a new instance of the <see cref="DeviceCollection"/> class. /// </summary> /// <param name="deviceClass">The identifier of the device class.</param> /// <param name="flags">The flags used to defined the type of expected devices.</param> public DeviceCollection(Guid deviceClass, DiGetClassFlags flags) { this.deviceClass = deviceClass; this.flags = flags; }
public static extern IntPtr SetupDiGetClassDevsEx( IntPtr pClassGuid, string enumerator, IntPtr hwndParent, DiGetClassFlags flags, IntPtr existingDeviceInfoSet, string machineName, IntPtr reserved );
internal static extern IntPtr SetupDiGetClassDevs(ref Guid lpGuid, IntPtr Enumerator, IntPtr hwndParent, DiGetClassFlags Flags);
private static DeviceInfoSetSafeHandle SetupDiGetClassDevs(Guid serviceClass, DiGetClassFlags classFlags) { return(Interop.SetupDiGetClassDevs(ref serviceClass, IntPtr.Zero, IntPtr.Zero, classFlags)); }
private static extern IntPtr SetupDiGetClassDevs( ref Guid classGuid, [MarshalAs(UnmanagedType.LPTStr)] string enumerator, IntPtr hwndParent, DiGetClassFlags flags );
private static extern IntPtr SetupDiGetClassDevs(ref Guid gClass, UInt32 iEnumerator, UInt32 hParent, DiGetClassFlags nFlags);
private static extern IntPtr SetupDiGetClassDevs( IntPtr classGuid, IntPtr enumerator, IntPtr hwndParent, DiGetClassFlags flags );
static extern bool SetupApi_Enumerate(Guid deviceClass, DiGetClassFlags flags, ref SetupApiDeviceEnumerator enumerator);
public static extern IntPtr SetupDiGetClassDevs( ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPTStr)] [Optional] string Enumerator, [Optional] IntPtr hwndParent, DiGetClassFlags Flags);
public static extern IntPtr SetupDiGetClassDevs( IntPtr ClassGuid, string Enumerator, IntPtr hwndParent, DiGetClassFlags Flags );
public static extern SafeDeviceSetHandle GetClassDevs( ref Guid deviceClass, [MarshalAs(UnmanagedType.LPStr)] string enumerator, IntPtr parent, DiGetClassFlags flags);