// EPDReader.cpp : Defines the functions for the static library. // #include "pch.h" #include "framework.h" #include "EPDReader.h" #pragma comment (lib, "Setupapi.lib") #include #include #include #include int ExecuteTest(LPCTSTR DriverName) { HDEVINFO hDevInfo; int numberFound_ = 1; LPCTSTR g_deviceDesc = _T("USB Composite Device"); LPCTSTR thrmo = _T("USB\\VID_0EBB&PID_0340&MI_00"); TCHAR tempString[512] = { 0 }; hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_USB, // ignored if DIGCF_ALLCLASSES set NULL, // all devices NULL, // optional DIGCF_PRESENT); if (!hDevInfo) { return FALSE; } else { SP_DEVINFO_DATA deviceInfoData; int i = 0; deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); DWORD devIndex; for (devIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, devIndex, &deviceInfoData); ++devIndex) { // for each device DWORD junk; bool found = false; if (SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_DEVICEDESC, &junk, (PBYTE)tempString, sizeof(tempString), NULL)) { OutputDebugStringW(tempString); OutputDebugStringW(L"\n" ); if (_tcsicmp((LPCTSTR)tempString, g_deviceDesc) == 0) { // This is what we are looking for // Add this line to find the registry key for this device and print it to the command line memset(tempString, 0, 500); SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_HARDWAREID, &junk, (PBYTE)tempString, sizeof(tempString), NULL); if (numberFound_ == 1) { OutputDebugStringW(L" -> "); OutputDebugStringW(tempString); OutputDebugStringW(L"\n"); //This is the case we want where only one device is plugged in only do stuff with this device if it is the one that is plugged in if (_tcsicmp((LPCTSTR)tempString, DriverName) == 0) { // this is the device that we want } else { continue; } } SP_CLASSINSTALL_HEADER classInstallParams; SP_PROPCHANGE_PARAMS propChangeParams; classInstallParams.cbSize = sizeof(SP_CLASSINSTALL_HEADER); classInstallParams.InstallFunction = DIF_PROPERTYCHANGE; propChangeParams.ClassInstallHeader = classInstallParams; propChangeParams.StateChange = DICS_PROPCHANGE; propChangeParams.Scope = DICS_FLAG_GLOBAL; propChangeParams.HwProfile = 0; // This sets the parameters that we want to change SetupDiSetClassInstallParams(hDevInfo, &deviceInfoData, (PSP_CLASSINSTALL_HEADER)&propChangeParams, sizeof(SP_PROPCHANGE_PARAMS)); // This call should change the state of the device if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &deviceInfoData)) { //we have not been able to change the setting so clean up and throw an error SetupDiDestroyDeviceInfoList(hDevInfo); throw(std::exception("Error enabling/disabling the device")); } } } } // Get rid of the handle SetupDiDestroyDeviceInfoList(hDevInfo); } return TRUE; }