Information can be obtained from the device problem code. There are two ways that I could find to get it.
- Use
SetupDiGetDeviceProperty() to query DEVPKEY_Device_ProblemCode . - Use
CM_Get_DevNode_Status() the problem code will be present in the second argument after the call.
Problem code 22 ( CM_PROB_DISABLED ) means that the device was explicitly disconnected by the user using the device manager or other such utility.
Code example
#include "stdafx.h" #include <Windows.h> #include <SetupAPI.h> #include <Cfgmgr32.h> #include <devguid.h> #include <initguid.h> #include "devpkey.h" #include <algorithm> #include <iostream> using namespace std; int main() { HDEVINFO hDevInfo = ::SetupDiGetClassDevs(&GUID_DEVCLASS_DISPLAY, NULL, NULL, 0); //only getting for GPUs on the machine if (INVALID_HANDLE_VALUE != hDevInfo) { SP_DEVINFO_DATA data; data.cbSize = (DWORD)sizeof(data); for (unsigned int nIndex = 0; ::SetupDiEnumDeviceInfo(hDevInfo, nIndex, &data); nIndex++) { ULONG status = 0, problem = 0; CONFIGRET cr = ::CM_Get_DevNode_Status(&status, &problem, data.DevInst, 0); //after the call 'problem' variable will have the problem code if (CR_SUCCESS == cr) { cout << " problem " << problem <<endl; if(problem == CM_PROB_DISABLED) { /*Do Something*/ } DEVPROPTYPE propertyType; const DWORD propertyBufferSize = 100; BYTE propertyBuffer[propertyBufferSize]; std::fill(begin(propertyBuffer), end(propertyBuffer), BYTE(0)); DWORD requiredSize = 0; if (SetupDiGetDeviceProperty(hDevInfo, &data, &DEVPKEY_Device_ProblemCode, &propertyType, propertyBuffer, propertyBufferSize, &requiredSize, 0)) //after the call 'propertyBuffer' will have error codes { unsigned long deviceProblemCode = *((unsigned long*)propertyBuffer); cout << " deviceProblemCode " << deviceProblemCode << endl; if(problem == CM_PROB_DISABLED) { /*Do Something*/ } } } } } return 0; }
Output result
problem 0 deviceProblemCode 0 problem 22 deviceProblemCode 22
In the question, you can see that Intel(R) HD Graphics 530 turned on, and the NVIDIA Quadro M1000M turned off. Therefore, at the output, we received problem code 0 and problem code 22 ( CM_PROB_DISABLED ).
source share