Windows设备驱动判断
2020-12-28 21:27
标签:roc global log LLC eem start pap via array Windows设备驱动判断 标签:roc global log LLC eem start pap via array 原文地址:https://www.cnblogs.com/fabric-summoner/p/13304534.htmlWindows驱动
// type.h
#ifndef TYPE_H
#define TYPE_H
#include
#include "type.h"
bool Widget::isDevConnected(wdi_device_info **list, wdi_options_create_list *options)
{
bool b = false; // true:usb设备连接正常 false:usb设备驱动未安装
DWORD size, reg_type;
HDEVINFO dev_info;
SP_DEVINFO_DATA dev_info_data;
HKEY key;
char strbuf[STR_BUFFER_SIZE], drv_version[] = "xxxxx.xxxxx.xxxxx.xxxxx";
struct wdi_device_info *start = NULL, *device_info = NULL;
const char* usbhub_name[] = { "usbhub", "usbhub3", "usb3hub", "nusb3hub", "rusb3hub", "flxhcih", "tihub3",
"etronhub3", "viahub3", "asmthub3", "iusb3hub", "vusb3hub", "amdhub30", "vhhub" };
const char usbccgp_name[] = "usbccgp";
BOOL is_hub;
*list = NULL;
// List all connected USB devices
dev_info = SetupDiGetClassDevsA(NULL, "USB", NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
// Find the ones that are driverless
for (int i = 0; ; i++)
{
// Free any invalid previously allocated struct
free_di(device_info);
dev_info_data.cbSize = sizeof(dev_info_data); // sizeof(dev_info_data)在不同电脑返回不同值(28,32)
if (!SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data))
{
b = true;
break;
}
// Allocate a driver_info struct to store our data
device_info = (struct wdi_device_info*)calloc(1, sizeof(struct wdi_device_info));
if (device_info == NULL)
{
wdi_destroy_list(start);
SetupDiDestroyDeviceInfoList(dev_info);
}
// SPDRP_DRIVER seems to do a better job at detecting driverless devices than
// SPDRP_INSTALL_STATE
drv_version[0] = 0;
if (SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER,
®_type, (BYTE*)strbuf, STR_BUFFER_SIZE, &size))
{
if ((options == NULL) || (!options -> list_all))
{
continue;
}
// While we have the driver key, pick up the driver version
key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_READ);
size = sizeof(drv_version);
if (key != INVALID_HANDLE_VALUE)
{
RegQueryValueExA(key, "DriverVersion", NULL, ®_type, (BYTE*)drv_version, &size);
}
}
// Eliminate USB hubs by checking the driver string
strbuf[0] = 0;
if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_SERVICE,
®_type, (BYTE*)strbuf, STR_BUFFER_SIZE, &size))
{
device_info -> driver = NULL;
}
else
{
device_info -> driver = safe_strdup(strbuf);
}
is_hub = FALSE;
for (int j = 0; j list_hubs)))
{
continue;
}
// Also eliminate composite devices parent drivers, as replacing these drivers
if (safe_stricmp(strbuf, usbccgp_name) == 0)
{
if ((options == NULL) || (!options -> list_hubs))
{
continue;
}
}
// Retrieve the first hardware ID
if (SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_HARDWAREID,
®_type, (BYTE*)strbuf, STR_BUFFER_SIZE, &size))
{
b = false;
break;
}
else
{
strbuf[0] = 0;
}
}
return b;
}
if (devlist != NULL)
{
wdi_destroy_list(devlist);
}
cl_options.trim_whitespaces = TRUE;
bool ret;
ret = isDevConnected(&devlist, &cl_options);
if(ret)
{
// 驱动正常
}
else
{
// 设备连接但是驱动未安装
}