win c++ 枚舉設備驅動狀態

  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;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章