This test with a Network adapter works for me as Admin and in x64 =>
WCHAR* wsAdapter = TEXT("TunnelBear Adapter V9");
HDEVINFO hDevInfo;
SP_DEVINFO_DATA did;
SP_PROPCHANGE_PARAMS pcp;
TCHAR wsBuffer[1024] = TEXT("");
DEVPROPTYPE dpt = 0;
BOOL bRet;
//hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_IMAGE, 0, 0, DIGCF_PRESENT);
//hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT);
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, 0, 0, DIGCF_PRESENT);
for (int i = 0; ; i++)
{
did.cbSize = sizeof(did);
if (!SetupDiEnumDeviceInfo(hDevInfo, i, &did))
break;
bRet = SetupDiGetDeviceProperty(hDevInfo, &did, &DEVPKEY_Device_DeviceDesc, &dpt, (PBYTE)wsBuffer, 1000, NULL, 0);
if (bRet == FALSE)
continue;
if (wcscmp(wsBuffer, wsAdapter) == 0)
{
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
if (SetupDiSetClassInstallParams(hDevInfo, &did, &pcp.ClassInstallHeader, sizeof(SP_PROPCHANGE_PARAMS)))
{
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.HwProfile = 0;
pcp.Scope = DICS_FLAG_GLOBAL; // DICS_FLAG_CONFIGSPECIFIC;
pcp.StateChange = DICS_DISABLE;
SetupDiSetClassInstallParams(hDevInfo, &did, &pcp.ClassInstallHeader, sizeof(SP_PROPCHANGE_PARAMS));
TCHAR wsError[512] = TEXT("");
if (SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &did) == 0)
{
DWORD dwError = GetLastError();
if (GetLastError() == ERROR_IN_WOW64)
wsprintf(wsError, TEXT("SetupDiCallClassInstaller does not work from WOW64\r\n"));
else
wsprintf(wsError, TEXT("SetupDiCallClassInstaller Error : %d\r\n"), dwError);
}
else
wsprintf(wsError, TEXT("Device state successfully changed\r\n"));
OutputDebugString(wsError);
}
else
{
// Error...
}
break;
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);