win c++列挙デバイス駆動状態

2992 ワード

#include   
#include 
#pragma comment(lib,"Setupapi.lib")
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
", 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
",buffer, dwStatuts); if (buffer) LocalFree(buffer); break; } if (buffer) LocalFree(buffer); } // step4. Cleanup SetupDiDestroyDeviceInfoList(hDevInfo); return dwStatuts; }