SetupApi : SetupDiGetSelectedDriver always returns ERROR_NO_DRIVER_SELECTED

Hi all,

I’m trying to write a simple routine to walk through the device nodes on
my system, and for each of them find out which driver is currently
installed (just as the device manager is able to do).

I’m using SetupApi to enumerate devices just like described in
‘Reinstalling an Unplugged Device’ on MSDN
(http://www.osronline.com/DDKx/install/custom-install_7n53.htm), but
rather than modifying device flags I call SetupDiGetSelectedDriver(…),
and for every device I examine GetLastError() returns me
ERROR_NO_DRIVER_SELECTED (0x203)

Am I approaching this from the wrong angle, or is there something wrong
with my code example? (How does device manager do it?)

Thanks,
Nick

int i;
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

DeviceInfoSet = SetupDiGetClassDevs(NULL, “USB”,
NULL, DIGCF_ALLCLASSES);

for (i=0; SetupDiEnumDeviceInfo(DeviceInfoSet, i, OUT &DeviceInfoData); i++)
{
BOOL bRes;
DWORD err;
SP_DRVINFO_DATA DriverInfoData;

bRes = SetupDiBuildDriverInfoList(DeviceInfoSet,
&DeviceInfoData, SPDIT_COMPATDRIVER);

if (!bRes) {
err = GetLastError();
return;
}

DriverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);

bRes = SetupDiGetSelectedDriver(DeviceInfoSet,
&DeviceInfoData, &DriverInfoData);

if (!bRes) {
err = GetLastError();
// always returns ERROR_NO_DRIVER_SELECTED! >:-(
if (err == ERROR_NO_DRIVER_SELECTED) continue;
return;
}
}

for (i=0; SetupDiEnumDeviceInfo(DeviceInfoSet, i, OUT &DeviceInfoData); i++)
{
BOOL bRes;
DWORD err;
SP_DRVINFO_DATA DriverInfoData;

bRes = SetupDiBuildDriverInfoList(DeviceInfoSet,
&DeviceInfoData, SPDIT_COMPATDRIVER);

I believe you want to be using SetupDiGetDeviceRegistryProperty (either SPDRP_DRIVER if you want to dig for details like binary names, or SPDRP_SERVICE if you just want the service name). The API you are using is for use in installation, and that’s already happened for your device element.

Hi,

Just to note there is devcon.exe with source code in the DDK that does this
and alot more.

William Michael Jones

“Nick Dowell” wrote in message news:xxxxx@ntdev…
> Hi all,
>
> I’m trying to write a simple routine to walk through the device nodes on
> my system, and for each of them find out which driver is currently
> installed (just as the device manager is able to do).
>
> I’m using SetupApi to enumerate devices just like described in
> ‘Reinstalling an Unplugged Device’ on MSDN
> (http://www.osronline.com/DDKx/install/custom-install_7n53.htm), but
> rather than modifying device flags I call SetupDiGetSelectedDriver(…),
> and for every device I examine GetLastError() returns me
> ERROR_NO_DRIVER_SELECTED (0x203)
>
> Am I approaching this from the wrong angle, or is there something wrong
> with my code example? (How does device manager do it?)
>
> Thanks,
> Nick
>
>
>
> int i;
> HDEVINFO DeviceInfoSet;
> SP_DEVINFO_DATA DeviceInfoData;
>
> DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
>
> DeviceInfoSet = SetupDiGetClassDevs(NULL, “USB”,
> NULL, DIGCF_ALLCLASSES);
>
> for (i=0; SetupDiEnumDeviceInfo(DeviceInfoSet, i, OUT &DeviceInfoData);
> i++)
> {
> BOOL bRes;
> DWORD err;
> SP_DRVINFO_DATA DriverInfoData;
>
> bRes = SetupDiBuildDriverInfoList(DeviceInfoSet,
> &DeviceInfoData, SPDIT_COMPATDRIVER);
>
> if (!bRes) {
> err = GetLastError();
> return;
> }
>
> DriverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
>
>
>
> bRes = SetupDiGetSelectedDriver(DeviceInfoSet,
> &DeviceInfoData, &DriverInfoData);
>
> if (!bRes) {
> err = GetLastError();
> // always returns ERROR_NO_DRIVER_SELECTED! >:-(
> if (err == ERROR_NO_DRIVER_SELECTED) continue;
> return;
> }
> }
>