SetupAPI根据硬件ID获取驱动INF文件和驱动日期版本信息
2022-01-25
279
0
SetupAPI根据硬件ID获取驱动INF文件和驱动日期版本信息。
注释掉的代码是获取该硬件的所有可用驱动信息。
代码是逐渐完善的,没想到可以直接从注册表信息中获取到。
SetupDiOpenDevRegKey打开的注册表路径示例为:
计算机\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{36fc9e60-c465-11cf-8056-444553540000}\0002
完整的SetupAPI根据硬件ID获取驱动INF文件和驱动日期版本信息代码如下
BOOL CDriver::GetDeviDriverInfo(TCHAR* hwid, std::wstring& info)
{
HDEVINFO devs = INVALID_HANDLE_VALUE;
do
{
//获取某类设备集合
devs = SetupDiGetClassDevsEx(NULL,
NULL,
NULL,
DIGCF_ALLCLASSES | DIGCF_PRESENT,
NULL,
NULL,
NULL);
if (devs == INVALID_HANDLE_VALUE)
{
break;
}
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if (!SetupDiGetDeviceInfoListDetail(devs, &devInfoListDetail))
{
break;
}
SP_DEVINFO_DATA devInfo;
devInfo.cbSize = sizeof(devInfo);
for (DWORD devIndex = 0; SetupDiEnumDeviceInfo(devs, devIndex, &devInfo); devIndex++)
{
//获取硬件ID
std::wstring HardId = GetDevcieProperty(devs, &devInfo, SPDRP_HARDWAREID);
TCHAR* pHardId = NULL;
if (HardId.size())
{
pHardId = (TCHAR*)&HardId.at(0);
}
//printf("%d %ws\n", devIndex, pHardId);
if (pHardId != NULL
&& _tcsicmp(hwid, pHardId) == 0)
{
SP_DEVINSTALL_PARAMS deviceInstallParams;
{
HKEY hKey = SetupDiOpenDevRegKey(devs, &devInfo,
DICS_FLAG_GLOBAL,
0,
DIREG_DRV,
KEY_READ);
if (hKey == INVALID_HANDLE_VALUE)
{
RegCloseKey(hKey);
return FALSE;
}
TCHAR buff[512] = { 0 };
DWORD RegDataType;
DWORD RegDataLength = sizeof(buff); // bytes!!!
long regerr = RegQueryValueEx(hKey,
TEXT("InfPath"),
NULL,
&RegDataType,
(PBYTE)buff,
&RegDataLength
);
if ((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ))
{
RegCloseKey(hKey);
return FALSE;
}
printf("InfPath:%S\n", buff);
RegDataLength = sizeof(buff);
regerr = RegQueryValueEx(hKey,
TEXT("DriverDesc"),
NULL,
&RegDataType,
(PBYTE)buff,
&RegDataLength
);
if ((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ))
{
RegCloseKey(hKey);
return FALSE;
}
printf("DriverDesc:%S\n", buff);
RegDataLength = sizeof(buff);
regerr = RegQueryValueEx(hKey,
TEXT("DriverDate"),
NULL,
&RegDataType,
(PBYTE)buff,
&RegDataLength
);
if ((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ))
{
RegCloseKey(hKey);
return FALSE;
}
printf("DriverDate:%S\n", buff);
RegDataLength = sizeof(buff);
regerr = RegQueryValueEx(hKey,
TEXT("DriverVersion"),
NULL,
&RegDataType,
(PBYTE)buff,
&RegDataLength
);
if ((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ))
{
RegCloseKey(hKey);
return FALSE;
}
printf("DriverVersion:%S\n", buff);
RegCloseKey(hKey);
}
//ZeroMemory(&deviceInstallParams, sizeof(deviceInstallParams));
//deviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
//if (!SetupDiGetDeviceInstallParams(devs, &devInfo, &deviceInstallParams))
//{
// break;
//}
//deviceInstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
//if (!SetupDiSetDeviceInstallParams(devs, &devInfo, &deviceInstallParams))
//{
// break;
//}
//if (SetupDiBuildDriverInfoList(devs, &devInfo, SPDIT_COMPATDRIVER))
//{
// ULONG index = 0;
// SP_DRVINFO_DATA driverInfoData;
// ZeroMemory(&driverInfoData, sizeof(driverInfoData));
// driverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
// while (SetupDiEnumDriverInfo(devs, &devInfo, SPDIT_COMPATDRIVER, index, &driverInfoData))
// {
// SP_DRVINFO_DETAIL_DATA driverInfoDetail;
// driverInfoDetail.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
// if (SetupDiGetDriverInfoDetail(devs, &devInfo, &driverInfoData, &driverInfoDetail, sizeof(SP_DRVINFO_DETAIL_DATA), NULL))
// {
// index = index;
// }
// printf("driverInfoData.ProviderName:%S\n", driverInfoData.ProviderName);
// printf("driverInfoData.MfgName:%S\n", driverInfoData.MfgName);
// printf("driverInfoDetail.InfFileName:%S\n", driverInfoDetail.InfFileName);
// SYSTEMTIME SystemTime;
// if (FileTimeToSystemTime(&driverInfoData.DriverDate, &SystemTime))
// {
// TCHAR Buffer[MAX_PATH];
// if (GetDateFormat(LOCALE_USER_DEFAULT,
// DATE_SHORTDATE,
// &SystemTime,
// NULL,
// Buffer,
// sizeof(Buffer) / sizeof(TCHAR)
// ) != 0)
// {
// printf("driverInfoData.DriverDate:%S\n", Buffer);
// }
// }
// ULARGE_INTEGER Version;
// Version.QuadPart = driverInfoData.DriverVersion;
// printf("Version:%d.%d.%d.%d\n",
// HIWORD(Version.HighPart),
// LOWORD(Version.HighPart),
// HIWORD(Version.LowPart),
// LOWORD(Version.LowPart)
// );
// DWORD e = GetLastError();
// e = e;
// index++;
// }
//}
}
}
} while (0);
if (devs != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(devs);
}
return TRUE;
}
std::string GetDevcieProperty(HDEVINFO Devs, PSP_DEVINFO_DATA DevInfo, DWORD Prop)
{
std::string Buffer;
DWORD reqSize = 0;
DWORD dataType = 0;;
do
{
SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Prop, &dataType, NULL, NULL, &reqSize);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
break;
}
Buffer.resize(reqSize);
SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Prop, &dataType, (LPBYTE)&Buffer.at(0), reqSize, &reqSize);
} while (0);
return Buffer;
}
PS:
另一种方法是通过实现,详情见devcon的FindCurrentDriver源代码
deviceInstallParams.FlagsEx |= (DI_FLAGSEX_INSTALLEDDRIVER | DI_FLAGSEX_ALLOWEXCLUDEDDRVS);