Hi,
I am trying to programmatically control Enable/Diable devices and am using
some code I found using the Setup API. When using the function below, I am
able to enable a CDROM device only if I used the same code to disable it. If
I disable the CDROM using the Windows Device Manager GUI, enabling it with
the function does not work. the call:
ok = SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, HwDevInfo,
DeviceInfoData) returns true, but the device remains in disabled state.
If anyone has some experience from this, I would appreciate a code review to
see if there are any obvious errors
Thanks
Niclas
Private Shared Function changeDeviceState(ByVal HwDevInfo As IntPtr, ByVal
DeviceInfoData As SP_DEVINFO_DATA, ByVal state As System.UInt32) As Boolean
Dim propertyChangeParams As SP_PROPCHANGE_PARAMS = New SP_PROPCHANGE_PARAMS
propertyChangeParams.ClassInstallHeader.cbSize =
Marshal.SizeOf(GetType(SP_CLASSINSTALL_HEADER))
propertyChangeParams.ClassInstallHeader.InstallFunction =
Convert.ToUInt32(DIF_PROPERTYCHANGE)
propertyChangeParams.StateChange = state
propertyChangeParams.Scope = Convert.ToUInt32(DICS_FLAG_GLOBAL)
Dim propertyChangeParamsSize As Integer =
Marshal.SizeOf(propertyChangeParams)
Dim PSP_PROPCHANGE_PARAMS As IntPtr =
Marshal.AllocHGlobal(propertyChangeParamsSize)
Marshal.StructureToPtr(propertyChangeParams, PSP_PROPCHANGE_PARAMS, True)
Dim ok As Boolean = SetupDiSetClassInstallParamsA(HwDevInfo, DeviceInfoData,
PSP_PROPCHANGE_PARAMS, System.Convert.ToUInt32(propertyChangeParamsSize))
If ok Then
ok = SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, HwDevInfo,
DeviceInfoData)
If (Not ok) Then
Dim errorCode As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("Error (" & errorCode.ToString() & ") +
SetupDiSetClassInstaller")
Return False
End If
Else
Dim errorCode As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("Error (" & errorCode.ToString() & ") +
SetupDiSetClassInstallParams")
Return False
End If
Return True
End Function