win c++ 枚举设备驱动状态

来源:互联网 发布:云计算的就业前景 编辑:程序博客网 时间:2024/06/04 14:56
  1. #include <cfgmgr32.h>  #include <SetupAPI.h>#pragma comment(lib,"Setupapi.lib")
  2. bool IsDeviceDisabled(DWORD dwDevID, HDEVINFO hDevInfo, DWORD &dwStatus){    SP_DEVINFO_DATA DevInfoData = {sizeof(SP_DEVINFO_DATA)};    DWORD dwDevStatus,dwProblem;    if(!SetupDiEnumDeviceInfo(hDevInfo,dwDevID,&DevInfoData))    {        return FALSE;    }    //查询设备状态    if(CM_Get_DevNode_Status(&dwDevStatus,&dwProblem,DevInfoData.DevInst,0)!=CR_SUCCESS)    {        return FALSE;    }        dwStatus = dwProblem;//    return ( (dwProblem == CM_PROB_FAILED_INSTALL));    return true;}int IsInstallDriver(){    HDEVINFO hDevInfo;     SP_DEVINFO_DATA DeviceInfoData;     DWORD i;     bool bRet = false;    bool bOk = false;      //step1. Create a HDEVINFO with all present devices.     hDevInfo = SetupDiGetClassDevs(NULL,         0, // Enumerator         0,         DIGCF_PRESENT | DIGCF_ALLCLASSES );     if (hDevInfo == INVALID_HANDLE_VALUE)     {         // Insert error handling here.         return bRet;     }          DWORD dwStatuts = -1;    //step2. Enumerate through all devices in Set.     DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);     for (i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)     {         DWORD DataT;         LPTSTR buffer = NULL;         DWORD buffersize = 0;            // Call function with null to begin with,         // then use the returned buffer size         // to Alloc the buffer. Keep calling until         // success or an unknown failure.         //         while (!SetupDiGetDeviceRegistryProperty(             hDevInfo,             &DeviceInfoData,             SPDRP_HARDWAREID,             &DataT,             (PBYTE)buffer,             buffersize,             &buffersize))         {             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)             {                 // Change the buffer size.                 if (buffer) LocalFree(buffer);                 buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);             }             else             {                 // Insert error handling here.                 break;             }         }         printf("%S\n", buffer);        //step3. find devices status        if (buffer && (!wcscmp(buffer,L"USB\\Vid_0955&Pid_7103") || !wcscmp(buffer,L"USB\\Vid_0955&Pid_7102")) )        {                        if (IsDeviceDisabled(i, hDevInfo, dwStatuts) && dwStatuts == 0)            {                }            //printf( "SPDRP_DEVICEDESC:[%S] %d\n ",buffer, dwStatuts);             if (buffer)                LocalFree(buffer);             break;        }        if (buffer)            LocalFree(buffer);     }     // step4. Cleanup     SetupDiDestroyDeviceInfoList(hDevInfo);        return dwStatuts;}


原创粉丝点击