I have a problem with a simple function in my program, this function (listed below) should find the device with the HardwareId ID, and then turn it off / on. He finds this, but then I get an error, and GetLastError () returns a value from the range described in msdn. I noted a bug in the code with a comment. If anyone seeing this is familiar with SetupDiCallClassInstaller (), please help. I do not know where to look for this error, and if it is a code error or env system. I use Windows 7 64-bit and run this program as admin.
bool DisableInterface(bool bStatus) { IN LPTSTR HardwareId; HardwareId = L"DAUDIO\\FUNC_01&VEN_10DE&DEV_0018&SUBSYS_10DE0101"; DWORD NewState ; if(bStatus) { NewState = DICS_DISABLE; } else { NewState = DICS_ENABLE; } DWORD i, err; bool found = false; HDEVINFO hDevInfo; SP_DEVINFO_DATA spDevInfoData ; hDevInfo=SetupDiGetClassDevs(NULL, 0, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); if(hDevInfo == INVALID_HANDLE_VALUE) { printf("blad1"); return false; } spDevInfoData.cbSize=sizeof(SP_DEVINFO_DATA); for(i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++) { DWORD DataT; LPTSTR p, buffer = NULL; DWORD buffersize = 0; // get all devices info while(!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_HARDWAREID, &DataT, (PBYTE)buffer, buffersize, &buffersize) ) { if(GetLastError() == ERROR_INVALID_DATA) { break ; } else if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) { if(buffer) LocalFree(buffer); buffer = (wchar_t*)LocalAlloc(LPTR,buffersize); } else { goto cleanup_DeviceInfo; } } if(GetLastError() == ERROR_INVALID_DATA) continue; //find device with HardwerId for(p = buffer; *p && (p<&buffer[buffersize]) ; p += lstrlen(p) + sizeof(TCHAR)) { if( !_tcscmp(HardwareId, p) ) { found = true; break; } } if(buffer) LocalFree(buffer); // if device found change it state if(found) { SP_PROPCHANGE_PARAMS params; params.ClassInstallHeader.cbSize=sizeof(SP_CLASSINSTALL_HEADER); params.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE ; params.Scope=DICS_FLAG_GLOBAL ; params.StateChange = NewState ; // setup proper parameters if(!SetupDiSetClassInstallParams(hDevInfo, &spDevInfoData, ¶ms.ClassInstallHeader, sizeof(params))) { DWORD errorcode = GetLastError(); } // use parameters if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spDevInfoData)) { DWORD errorcode = GetLastError(); // error here } switch(NewState) { case DICS_DISABLE : printf("off"); break; case DICS_ENABLE : printf("on"); break; } break; } } cleanup_DeviceInfo : err = GetLastError(); SetupDiDestroyDeviceInfoList(hDevInfo); SetLastError(err); return true; }
Thanks for the help.
user1668674
source share