Example #1
0
        static List <string> GetInstanceIdsFromClassGuid(Guid classGuid)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;
            List <string>           resultList  = new List <string>();

            try
            {
                // Get the handle to a device information set for all devices matching classGuid that are present on the
                // system.
                diSetHandle = NativeMethods.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, SetupDiGetClassDevsFlags.Present);
                // Get the device information data for each matching device.
                DeviceInfoData[] diData = GetDeviceInfoData(diSetHandle);

                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(diSetHandle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);
                    if (result == false && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
                    {
                        sb.Capacity = requiredSize;
                        result      = NativeMethods.SetupDiGetDeviceInstanceId(diSetHandle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);

                        resultList.Add(sb.ToString());
                    }
                }
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
            return(resultList);
        }
Example #2
0
 public static extern bool SetupDiSetClassInstallParams(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [In()] ref DeviceInfoData deviceInfoData,
     [In()] ref PropertyChangeParameters classInstallParams,
     int classInstallParamsSize );
Example #3
0
 public static extern bool SetupDiEnumDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     int memberIndex,
     ref DeviceInfoData deviceInfoData );
Example #4
0
 public static extern bool SetupDiCallClassInstaller(
     DiFunction installFunction,
     SafeDeviceInfoSetHandle deviceInfoSet,
     [In()] ref DeviceInfoData deviceInfoData );
Example #5
0
 /// <summary>
 /// Find the index of the particular DeviceInfoData for the instanceId.
 /// </summary>
 /// <param name="handle"></param>
 /// <param name="diData"></param>
 /// <param name="instanceId"></param>
 /// <returns></returns> 
 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 #6
0
 static DeviceInfoData[] GetDeviceInfoData( SafeDeviceInfoSetHandle handle )
 {
     List<DeviceInfoData> data = new List<DeviceInfoData>();
     DeviceInfoData did = new DeviceInfoData();
     int didSize = Marshal.SizeOf( did );
     did.Size = didSize;
     int index = 0;
     while ( NativeMethods.SetupDiEnumDeviceInfo( handle, index, ref did ) )
     {
         data.Add( did );
         index += 1;
         did = new DeviceInfoData();
         did.Size = didSize;
     }
     return data.ToArray();
 }
Example #7
0
        // enable/disable...
        static void EnableDevice( SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable )
        {
            PropertyChangeParameters @params = new PropertyChangeParameters();
            // The size is just the size of the header, but we've flattened the structure.
            // The header comprises the first two fields, both integer.
            @params.Size = 8;
            @params.DiFunction = DiFunction.PropertyChange;
            @params.Scope = Scopes.Global;
            if ( enable )
            {
                @params.StateChange = StateChangeAction.Enable;
            }
            else
            {
                @params.StateChange = StateChangeAction.Disable;
            }

            bool result = NativeMethods.SetupDiSetClassInstallParams( handle, ref diData, ref @params, Marshal.SizeOf( @params ) );
            if ( result == false )
            {
                throw new Win32Exception( Marshal.GetLastWin32Error() );
            }
            result = NativeMethods.SetupDiCallClassInstaller( DiFunction.PropertyChange, handle, ref diData );
            if ( result == false )
            {
                int err = Marshal.GetLastWin32Error();
                if ( err == (int)SetupApiError.NotDisableable )
                {
                    throw new ArgumentException( "Device can't be disabled (programmatically or in Device Manager)." );
                }
                else if ( err <= (int)SetupApiError.NoAssociatedClass && err >= (int)SetupApiError.OnlyValidateViaAuthenticode )
                {
                    throw new Win32Exception( "SetupAPI error: " + ( (SetupApiError)err ).ToString() );
                }
                else
                {
                    throw new Win32Exception( err );
                }
            }
        }
Example #8
0
 public static extern bool SetupDiSetClassInstallParams(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [In()] ref DeviceInfoData deviceInfoData,
     [In()] ref PropertyChangeParameters classInstallParams,
     int classInstallParamsSize);
Example #9
0
 public static extern bool SetupDiEnumDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     int memberIndex,
     ref DeviceInfoData deviceInfoData);
Example #10
0
 public static extern bool SetupDiCallClassInstaller(
     DiFunction installFunction,
     SafeDeviceInfoSetHandle deviceInfoSet,
     [In()] ref DeviceInfoData deviceInfoData);