Esempio n. 1
0
 private static extern IntPtr SetupDiGetClassDevsEx(
     ref Guid ClassGuid,
     [MarshalAs(UnmanagedType.LPTStr)] String Enumerator,
     IntPtr hwndParent,
     DIGCF Flags,
     IntPtr DeviceInfoSet,
     [MarshalAs(UnmanagedType.LPTStr)] String MachineName,
     IntPtr Reserved
     );
Esempio n. 2
0
 private static extern IntPtr SetupDiGetClassDevsEx(
     ref Guid ClassGuid,
     [MarshalAs(UnmanagedType.LPTStr)] String Enumerator,
     IntPtr hwndParent,
     DIGCF Flags,
     IntPtr DeviceInfoSet,
     [MarshalAs(UnmanagedType.LPTStr)] String MachineName,
     IntPtr Reserved
     );
Esempio n. 3
0
 /// <summary>
 /// Get list of devices.
 /// </summary>
 /// <param name="classGuid">Filter devices by class.</param>
 /// <param name="flags">Filter devices by options.</param>
 /// <param name="deviceId">Filter results by Device ID.</param>
 /// <param name="vid">Filter results by Vendor ID.</param>
 /// <param name="pid">Filter results by Product ID.</param>
 /// <param name="rev">Filter results by Revision ID.</param>
 /// <returns>List of devices</returns>
 /// <remarks>
 /// This is code I cobbled together from a number of newsgroup threads
 /// as well as some C++ stuff I translated off of MSDN.  Seems to work.
 /// The idea is to come up with a list of devices, same as the device
 /// manager does.  Currently it uses the actual "system" names for the
 /// hardware.  It is also possible to use hardware IDs.  See the docs
 /// for SetupDiGetDeviceRegistryProperty in the MS SDK for more details.
 /// Errors:   This method may throw the following errors.
 ///           Failed to enumerate device tree!
 ///           Invalid handle!
 /// </remarks>
 public static DeviceInfo[] GetDevices(Guid?classGuid = null, DIGCF flags = DIGCF.DIGCF_ALLCLASSES, string deviceId = null, int vid = 0, int pid = 0, int rev = 0)
 {
     if (!classGuid.HasValue)
     {
         classGuid = Guid.Empty;
     }
     lock (GetDevicesLock)
     {
         // https://msdn.microsoft.com/en-us/library/windows/hardware/ff541247%28v=vs.85%29.aspx
         //
         // [Device Information Set]
         //  ├──[Device Information]
         //  │   ├──[Device Node]
         //  │   └──[Device Interface], [Device Interface], ...
         //  ├──[Device Information]
         //  │   ├──[Device Node]
         //  │   └──[Device Interface], [Device Interface], ...
         //
         // Create a device information set composed of all devices associated with a specified device setup class or device interface class.
         IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(classGuid.Value, IntPtr.Zero, IntPtr.Zero, flags);
         if (deviceInfoSet.ToInt64() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         var list           = new List <DeviceInfo>();
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         // Enumerating Device Nodes.
         for (var i = 0; NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             var currentDeviceId = GetDeviceId(deviceInfoData.DevInst);
             if (!string.IsNullOrEmpty(deviceId) && deviceId != currentDeviceId)
             {
                 continue;
             }
             var device = GetDeviceInfo(deviceInfoSet, deviceInfoData);
             if (vid > 0 && device.VendorId != vid)
             {
                 continue;
             }
             if (pid > 0 && device.ProductId != pid)
             {
                 continue;
             }
             if (rev > 0 && device.Revision != rev)
             {
                 continue;
             }
             list.Add(device);
         }
         NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return(list.OrderBy(x => x.ClassDescription).ThenBy(x => x.Description).ToArray());
     }
 }
Esempio n. 4
0
 public static DeviceInfo GetParentDevice(Guid classGuid, DIGCF flags, string deviceId)
 {
     lock (GetDevicesLock)
     {
         IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(classGuid, IntPtr.Zero, IntPtr.Zero, flags); //  | DIGCF.DIGCF_PRESENT
         if (deviceInfoSet.ToInt64() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         DeviceInfo device         = null;
         var        deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         int i;
         for (i = 0; NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             if (deviceId == GetDeviceId(deviceInfoData.DevInst))
             {
                 uint parentDeviceInstance = 0;
                 var  CRResult             = NativeMethods.CM_Get_Parent(out parentDeviceInstance, deviceInfoData.DevInst, 0);
                 if (CRResult == CR.CR_NO_SUCH_DEVNODE)
                 {
                     break;
                 }
                 if (CRResult != CR.CR_SUCCESS)
                 {
                     break;
                 }
                 var parentDeviceId = GetDeviceId(parentDeviceInstance);
                 var devices        = GetDevices(classGuid, flags, parentDeviceId);
                 if (devices.Length > 0)
                 {
                     device = devices[0];
                 }
                 break;
             }
         }
         NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return(device);
     }
 }
Esempio n. 5
0
 public static DeviceInfo[] GetDevices(Guid classGuid, DIGCF flags, string deviceId, int vid, int pid, int rev)
 {
     lock (GetDevicesLock)
     {
         IntPtr deviceInfoSet = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, flags);                 //  | DIGCF.DIGCF_PRESENT
         if (deviceInfoSet.ToInt32() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         var list           = new List <DeviceInfo>();
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         int i;
         for (i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             var currentDeviceId = GetDeviceId(deviceInfoData.DevInst);
             if (!string.IsNullOrEmpty(deviceId) && deviceId != currentDeviceId)
             {
                 continue;
             }
             var device = GetDeviceInfo(deviceInfoSet, deviceInfoData, deviceId);
             if (vid > 0 && device.VendorId != vid)
             {
                 continue;
             }
             if (pid > 0 && device.ProductId != pid)
             {
                 continue;
             }
             if (rev > 0 && device.Revision != rev)
             {
                 continue;
             }
             list.Add(device);
         }
         SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return(list.OrderBy(x => x.ClassDescription).ThenBy(x => x.Description).ToArray());
     }
 }
Esempio n. 6
0
        public static SP_DRVINFO_DATA[] GetDrivers(Guid?classGuid = null, DIGCF flags = DIGCF.DIGCF_ALLCLASSES, SPDIT driverType = SPDIT.SPDIT_COMPATDRIVER, string deviceId = null, string hardwareId = null)
        {
            var drvInfoList   = new List <SP_DRVINFO_DATA>();
            var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(classGuid ?? Guid.Empty, IntPtr.Zero, IntPtr.Zero, flags);

            if (deviceInfoSet.ToInt64() == ERROR_INVALID_HANDLE_VALUE)
            {
                return(drvInfoList.ToArray());
            }
            // Loop through device info.
            var deviceInfoData = new SP_DEVINFO_DATA();

            deviceInfoData.Initialize();
            for (int i = 0; NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
            {
                if (!string.IsNullOrEmpty(deviceId))
                {
                    var currentDeviceId = GetDeviceId(deviceInfoData.DevInst);
                    if (string.Compare(deviceId, currentDeviceId, true) != 0)
                    {
                        continue;
                    }
                }
                if (!string.IsNullOrEmpty(hardwareId))
                {
                    var currentHardwareId = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, SPDRP.SPDRP_HARDWAREID);
                    if (string.Compare(hardwareId, currentHardwareId, true) != 0)
                    {
                        continue;
                    }
                }
                var drivers = GetDrivers(deviceInfoSet, ref deviceInfoData, driverType);
                drvInfoList.AddRange(drivers);
            }
            NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
            return(drvInfoList.ToArray());
        }
Esempio n. 7
0
 public static extern HDEVINFO SetupDiGetClassDevs
     ([MarshalAs(UnmanagedType.LPStruct)] Guid classGuid, string enumerator, IntPtr hwndParent, DIGCF flags);
 public static extern IntPtr SetupDiGetClassDevs(int ClassGuid, string Enumerator, IntPtr hwndParent, DIGCF Flags);
Esempio n. 9
0
 private static extern IntPtr SetupDiGetClassDevs(
     IntPtr ClassGuid,       // null 
     String Enumerator,
     IntPtr hwndParent,
     DIGCF Flags
     );
Esempio n. 10
0
 public static extern IntPtr SetupDiGetClassDevs(
     ref Guid classGuid,
     [MarshalAs(UnmanagedType.LPTStr)] string enumerator,
     IntPtr hwndParent,
     DIGCF flags // uint flags
     );
 public static DeviceInfo[] GetDevices(Guid classGuid, DIGCF flags, string deviceId, int vid, int pid, int rev)
 {
     lock (GetDevicesLock)
     {
         IntPtr deviceInfoSet = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, flags); //  | DIGCF.DIGCF_PRESENT
         if (deviceInfoSet.ToInt32() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         var list = new List<DeviceInfo>();
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         int i;
         for (i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             var currentDeviceId = GetDeviceId(deviceInfoData.DevInst);
             if (!string.IsNullOrEmpty(deviceId) && deviceId != currentDeviceId) continue;
             var device = GetDeviceInfo(deviceInfoSet, deviceInfoData, deviceId);
             if (vid > 0 && device.VendorId != vid) continue;
             if (pid > 0 && device.ProductId != pid) continue;
             if (rev > 0 && device.Revision != rev) continue;
             list.Add(device);
         }
         SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return list.OrderBy(x => x.ClassDescription).ThenBy(x => x.Description).ToArray();
     }
 }
 public static DeviceInfo GetDevice(Guid classGuid, DIGCF flags, string deviceId)
 {
     return GetDevices(classGuid, flags, deviceId, 0, 0, 0).FirstOrDefault();
 }
Esempio n. 13
0
 internal static extern IntPtr SetupDiGetClassDevs(
     ref Guid classGuid,
     [MarshalAs(UnmanagedType.LPTStr)] string enumerator,
     IntPtr hwndParent,
     DIGCF flags);
 private static extern SafeDeviceInformationSetHandle SetupDiGetClassDevs(
     [In] ref Guid ClassGuid,
     [In] string Enumerator,
     IntPtr hwndParent,
     DIGCF Flags
     );
Esempio n. 15
0
 private static extern IntPtr SetupDiGetClassDevs(
     [MarshalAs(UnmanagedType.LPStruct), In] Guid ClassGuid,
     [MarshalAs(UnmanagedType.LPWStr), In] string Enumerator,
     IntPtr hwndParent,
     DIGCF Flags);
Esempio n. 16
0
 internal static extern IntPtr SetupDiGetClassDevsW([In] ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPWStr)] string Enumerator, IntPtr parent, DIGCF flags);
Esempio n. 17
0
 public static extern Handle SetupDiGetClassDevs(ref Guid classGuid, string enumerator, IntPtr hwndParent, DIGCF flags);
Esempio n. 18
0
 private static extern IntPtr SetupDiGetClassDevs(
     ref Guid ClassGuid,
     [MarshalAs(UnmanagedType.LPTStr)] String Enumerator,
     IntPtr hwndParent,
     DIGCF Flags
     );
Esempio n. 19
0
 private static extern IntPtr SetupDiGetClassDevs(
     [MarshalAs(UnmanagedType.LPStruct), In] Guid ClassGuid,
     IntPtr Enumerator,             // Null
     IntPtr hwndParent,             // Null
     DIGCF Flags);
 public static extern IntPtr SetupDiGetClassDevs([MarshalAs(UnmanagedType.LPStruct)]System.Guid classGuid, string enumerator, IntPtr hwndParent, DIGCF flags);
 /// <summary>
 /// Get list of devices.
 /// </summary>
 /// <returns>List of devices</returns>
 /// <remarks>
 /// This is code I cobbled together from a number of newsgroup threads
 /// as well as some C++ stuff I translated off of MSDN.  Seems to work.
 /// The idea is to come up with a list of devices, same as the device
 /// manager does.  Currently it uses the actual "system" names for the
 /// hardware.  It is also possible to use hardware IDs.  See the docs
 /// for SetupDiGetDeviceRegistryProperty in the MS SDK for more details.
 /// Errors:   This method may throw the following errors.
 ///           Failed to enumerate device tree!
 ///           Invalid handle!
 /// </remarks>		
 public static DeviceInfo[] GetDevices(Guid classGuid, DIGCF flags)
 {
     return GetDevices(classGuid, flags, null, 0, 0, 0);
 }
Esempio n. 22
0
 private static extern IntPtr SetupDiGetClassDevs(IntPtr ClassGuid, string Enumerator, IntPtr hwndParent, DIGCF Flags);
 public static DeviceInfo GetParentDevice(Guid classGuid, DIGCF flags, string deviceId)
 {
     lock (GetDevicesLock)
     {
         IntPtr deviceInfoSet = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, flags); //  | DIGCF.DIGCF_PRESENT
         if (deviceInfoSet.ToInt32() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         DeviceInfo device = null;
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         int i;
         for (i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             if (deviceId == GetDeviceId(deviceInfoData.DevInst))
             {
                 uint parentDeviceInstance = 0;
                 var CRResult = CM_Get_Parent(out parentDeviceInstance, deviceInfoData.DevInst, 0);
                 if (CRResult == CR.CR_NO_SUCH_DEVNODE) break;
                 if (CRResult != CR.CR_SUCCESS) break;
                 var parentDeviceId = GetDeviceId(parentDeviceInstance);
                 device = GetDevice(classGuid, flags, parentDeviceId);
                 break;
             }
         }
         SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return device;
     }
 }
        public static bool EnumClassDevs(string enumerator,
                                         DIGCF flags,
                                         ClassEnumeratorDelegate classEnumeratorCallback,
                                         object classEnumeratorCallbackParam1,
                                         Guid ClassGuid)
        {
            SP_DEVINFO_DATA dev_info_data = SP_DEVINFO_DATA.Empty;

            int dev_index = 0;

            IntPtr dev_info = SetupDiGetClassDevs(ref ClassGuid, enumerator, IntPtr.Zero, flags);

            if (dev_info == IntPtr.Zero || dev_info.ToInt64() == -1) return false;
            bool bSuccess = false;
            while (SetupDiEnumDeviceInfo(dev_info, dev_index, ref dev_info_data))
            {
                if (classEnumeratorCallback(dev_info, dev_index, ref dev_info_data, classEnumeratorCallbackParam1))
                {
                    bSuccess = true;
                    break;
                }

                dev_index++;
            }

            SetupDiDestroyDeviceInfoList(dev_info);

            return bSuccess;
        }
 public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);
Esempio n. 26
0
 public static extern IntPtr SetupDiGetClassDevs([MarshalAs(UnmanagedType.LPStruct)] System.Guid classGuid, IntPtr enumerator, IntPtr hwndParent, DIGCF flags);
Esempio n. 27
0
 private static extern IntPtr SetupDiGetClassDevs(
     ref Guid ClassGuid,
     [MarshalAs(UnmanagedType.LPTStr)] String Enumerator,
     IntPtr hwndParent,
     DIGCF Flags
     );
Esempio n. 28
0
 /// <summary>
 /// Get list of devices.
 /// </summary>
 /// <returns>List of devices</returns>
 /// <remarks>
 /// This is code I cobbled together from a number of newsgroup threads
 /// as well as some C++ stuff I translated off of MSDN.  Seems to work.
 /// The idea is to come up with a list of devices, same as the device
 /// manager does.  Currently it uses the actual "system" names for the
 /// hardware.  It is also possible to use hardware IDs.  See the docs
 /// for SetupDiGetDeviceRegistryProperty in the MS SDK for more details.
 /// Errors:   This method may throw the following errors.
 ///           Failed to enumerate device tree!
 ///           Invalid handle!
 /// </remarks>
 public static DeviceInfo[] GetDevices(Guid classGuid, DIGCF flags)
 {
     return(GetDevices(classGuid, flags, null, 0, 0, 0));
 }
Esempio n. 29
0
 public static DeviceInfo GetDevice(Guid classGuid, DIGCF flags, string deviceId)
 {
     return(GetDevices(classGuid, flags, deviceId, 0, 0, 0).FirstOrDefault());
 }
 public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, int Enumerator, IntPtr hwndParent, DIGCF Flags);