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

Re: Enable Devices by Eliyas

Eliyas
Sun Mar 12 10:55:03 CST 2006

The old code you used is based on an old install sample from Windows 2000
DDK that exhibited this problem. I think it's to do with the use or lack
there of DICS_FLAG_CONFIGSPECIFIC flag. I don't remember exactly. Follow the
devcon sample source in the DDK. It has a routine that does install and
uninstall of devices.

-Eliyas

"Niclas" <lindblom_niclas@hotmail.com> wrote in message
news:%23ssDOedRGHA.1160@TK2MSFTNGP09.phx.gbl...
> 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
>
>



Re: Enable Devices by Niclas

Niclas
Sun Mar 12 13:57:27 CST 2006

Thanks for the info.

Any chance you have a sample for how to enable/disable devices using C#or VB
.Net ? I am strugllig reding the C++ samples in the DDK

Niclas

"Eliyas Yakub [MSFT]" <eliyasy@online.microsoft.com> wrote in message
news:%23hQfsWfRGHA.4572@TK2MSFTNGP10.phx.gbl...
> The old code you used is based on an old install sample from Windows 2000
> DDK that exhibited this problem. I think it's to do with the use or lack
> there of DICS_FLAG_CONFIGSPECIFIC flag. I don't remember exactly. Follow
> the devcon sample source in the DDK. It has a routine that does install
> and uninstall of devices.
>
> -Eliyas
>
> "Niclas" <lindblom_niclas@hotmail.com> wrote in message
> news:%23ssDOedRGHA.1160@TK2MSFTNGP09.phx.gbl...
>> 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
>>
>>
>
>