您的位置:首页 > 编程语言 > C语言/C++

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

2017-05-27 15:20 274 查看
#include <cfgmgr32.h>  
#include <SetupAPI.h>
#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\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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: