The goal is to been able to enable or diseable a USB device in a small test application.The following code works on Windows XP and Vista. It doesn’t on Windows 7. Does anybody face the same kind of issue?
I got the code from here: https://www.osronline.com/showThread.CFM?link=82625
inline bool EnableDevice(const HDEVINFO Devs, const PSP_DEVINFO_DATA DevInfo, const bool bEnable)
{
bool reboot = false;
// ControlCallback function body
SP_PROPCHANGE_PARAMS pcp;
SP_DEVINSTALL_PARAMS devParams;
if (bEnable)
{
//
// enable both on global and config-specific profile
// do global first and see if that succeeded in enabling the device
// (global enable doesn’t mark reboot required if device is still
// disabled on current config whereas vice-versa isn’t true)
//
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.StateChange = DICS_ENABLE;
pcp.Scope = DICS_FLAG_GLOBAL;
pcp.HwProfile = 0;
//
// don’t worry if this fails, we’ll get an error when we try config-
// specific.
if (SetupDiSetClassInstallParams(Devs, DevInfo,
&pcp.ClassInstallHeader, sizeof(pcp)))
{
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);
}
//
// now enable on config-specific
//
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.StateChange = DICS_ENABLE;
pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
pcp.HwProfile = 0;
}
else
{
//
// operate on config-specific profile
//
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.StateChange = DICS_DISABLE;
pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
pcp.HwProfile = 0;
}
if (!SetupDiSetClassInstallParams(Devs, DevInfo, &pcp.ClassInstallHeader,
sizeof(pcp)) ||
!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo))
{
TESTTRACE(_T("Device state changed to "));
//_ASSERT(false);
}
else
{
//
// see if device needs reboot
//
devParams.cbSize = sizeof(devParams);
if (SetupDiGetDeviceInstallParams(Devs,DevInfo,&devParams) &&
(devParams.Flags & (DI_NEEDRESTART|DI_NEEDREBOOT)))
{
reboot = true;
}
else
{
//
// appears to have succeeded
//
}
}
if (reboot)
TESTTRACE(_T(“Reboot request!”));
return !reboot;
}