Example #1
0
        // Find the index of the particular DeviceInfoData for the instanceId.
        private static int GetIndexOfInstance(SafeDeviceInfoSetHandle handle, DeviceInfoData[] diData, string instanceId)
        {
            const int ERROR_INSUFFICIENT_BUFFER = 122;

            for (int index = 0; index <= diData.Length - 1; index++)
            {
                StringBuilder sb           = new StringBuilder(1);
                int           requiredSize = 0;
                bool          result       = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);
                if (result == false && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
                {
                    sb.Capacity = requiredSize;
                    result      = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);
                }
                if (result == false)
                {
                    throw new Win32Exception();
                }
                if (instanceId.Equals(sb.ToString()))
                {
                    return(index);
                }
            }
            // not found
            return(-1);
        }
Example #2
0
 // Find the index of the particular DeviceInfoData for the instanceId.
 private static int GetIndexOfInstance(SafeDeviceInfoSetHandle handle, DeviceInfoData[] diData, string instanceId)
 {
     const int ERROR_INSUFFICIENT_BUFFER = 122;
     for (int index = 0; index <= diData.Length - 1; index++)
     {
         StringBuilder sb = new StringBuilder(1);
         int requiredSize = 0;
         bool result = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);
         if (result == false && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
         {
             sb.Capacity = requiredSize;
             result = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);
         }
         if (result == false)
             throw new Win32Exception();
         if (instanceId.Equals(sb.ToString()))
         {
             return index;
         }
     }
     // not found
     return -1;
 }