Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV
Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


SetupDiGetClassDevs does not returns present device

OSR_Community_UserOSR_Community_User Member Posts: 110,217
For some reason some USB(in my case) devices in not returned by SetupDiGetClassDevs with (DIGCF_ALLCLASSES | DIGCF_PRESENT) falgs. Device is present in system and is showed up as connected by device manager and devcon. And the device is returned without DIGCF_PRESENT flag.
I see the same problem for USBDeview prograam it also showed device as not connected. What can be the reason?

My sample code:

/*****************************************************************************
I N C L U D E S
*****************************************************************************/
#include <windows.h>
#include <setupapi.h>
#include <usbioctl.h>

#include <stdio.h>
#include <tchar.h>
#include <usbiodef.h>

#ifdef DEBUG
#undef DBG
#define DBG 1
#endif

#if DBG
#define OOPS() Oops(__FILE__, __LINE__)
#else
#define OOPS()
#endif

#define ALLOC(dwBytes) GlobalAlloc(GPTR,(dwBytes))

#define REALLOC(hMem, dwBytes) GlobalReAlloc((hMem), (dwBytes), (GMEM_MOVEABLE|GMEM_ZEROINIT))

#define FREE(hMem) GlobalFree((hMem))

#define CHECKFORLEAKS()


_Success_(return == TRUE)
BOOL
GetDeviceProperty(
_In_ HDEVINFO DeviceInfoSet,
_In_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD Property,
_Outptr_ LPTSTR *ppBuffer
)
{
BOOL bResult;
DWORD requiredLength = 0;
DWORD lastError;

if(ppBuffer == NULL)
{
return FALSE;
}

*ppBuffer = NULL;

bResult = SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
DeviceInfoData,
Property,
NULL,
NULL,
0,
&requiredLength);
lastError = GetLastError();

if((requiredLength == 0) || (bResult != FALSE && lastError != ERROR_INSUFFICIENT_BUFFER))
{
return FALSE;
}

*ppBuffer = ALLOC(requiredLength);

if(*ppBuffer == NULL)
{
return FALSE;
}

bResult = SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
DeviceInfoData,
Property,
NULL,
(PBYTE)*ppBuffer,
requiredLength,
&requiredLength);
if(bResult == FALSE)
{
FREE(*ppBuffer);
*ppBuffer = NULL;
return FALSE;
}

return TRUE;
}

void
EnumerateAllDevices(
)
{
HDEVINFO deviceInfo = NULL;
LPGUID Guid = (LPGUID)&GUID_DEVINTERFACE_USB_DEVICE;
// Getting all present devices
deviceInfo = SetupDiGetClassDevs(Guid,
NULL,
NULL,
(DIGCF_ALLCLASSES | DIGCF_PRESENT));

if(deviceInfo != INVALID_HANDLE_VALUE)
{
ULONG index;
DWORD error;

error = 0;
index = 0;

while(error != ERROR_NO_MORE_ITEMS)
{

BOOL success;
SP_DEVINFO_DATA infoData;
ZeroMemory(&infoData, sizeof(SP_DEVINFO_DATA));
PWSTR DeviceDescName = NULL;
infoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

success = SetupDiEnumDeviceInfo(deviceInfo,
index,
&infoData);

index++;

if(success == FALSE)
{
error = GetLastError();

if(error != ERROR_NO_MORE_ITEMS)
{
//OOPS();
}
}
else
{
BOOL bResult;

bResult = GetDeviceProperty(deviceInfo,
&infoData,
SPDRP_DEVICEDESC,
&DeviceDescName);
if(bResult == FALSE)
{
//OOPS();
break;
}

printf("DeviceDescName = %s\n", DeviceDescName);
}
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
EnumerateAllDevices();
return 0;
}

Comments

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,776
    [email protected] wrote:
    > For some reason some USB(in my case) devices in not returned by SetupDiGetClassDevs with (DIGCF_ALLCLASSES | DIGCF_PRESENT) falgs. Device is present in system and is showed up as connected by device manager and devcon. And the device is returned without DIGCF_PRESENT flag.
    > I see the same problem for USBDeview prograam it also showed device as not connected. What can be the reason?

    What kind of devices are missing? Do you get different results if you
    run the app as administrator? Is it possible the device has some kind
    of protocol issue that is causing it to come and go?

    Devcon uses those exact same SetupDi APIs, so if Devcon can see it, then
    it is "seeable". You might need to check the source code for the
    operation you're doing to see if they are setting some other flag.

    --
    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • Pavel_APavel_A Member Posts: 2,756
    On 18-Aug-2015 16:50, [email protected] wrote:
    > For some reason some USB(in my case) devices in not returned by SetupDiGetClassDevs with (DIGCF_ALLCLASSES | DIGCF_PRESENT) falgs. Device is present in system and is showed up as connected by device manager and devcon. And the device is returned without DIGCF_PRESENT flag.
    > I see the same problem for USBDeview prograam it also showed device as not connected. What can be the reason?

    If your Windows in 64-bit, make sure to build your program also as 64-bit.

    -- pa
  • MikaeMikae Member Posts: 371
    Does the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\YOUR_VID&YOUR_PID exist? Do you see DeviceDesc value there? Which type is it?
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    <What kind of devices are missing? Do you get different results if you
    < run the app as administrator? Is it possible the device has some kind
    < of protocol issue that is causing it to come and go?

    I am talking about SAMSUNG Android ADB Interface - Android Debug Bridge(it is a child of SAMSUNG Mobile USB Composite Device). The adb is working properly, devices is not come and go.
    I have run as admin also with the same result.


    < If your Windows in 64-bit, make sure to build your program also as 64-bit.
    the same for x64 app

    < Does the registry key
    < HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\YOUR_VID&YOUR_PID exist? Do you see DeviceDesc value there? Which type is it?

    Yes, it is @oem36.inf,%compositeadbinterface%;SAMSUNG Android ADB Interface

    And as I say I can find device without DIGCF_PRESENT flag.. i can't get why.
  • Doron_HolanDoron_Holan Member - All Emails Posts: 10,553
    Perhaps the adb driver is not exposing this device interface for itself or children. The interface you had in your code is only enabled by the Usb core drivers, it doesn't extend to the entire device subtree



    Sent from Outlook Mail for Windows 10





    From: [email protected]
    Sent: Wednesday, August 19, 2015 10:28 AM
    To: Windows System Software Devs Interest List
    Subject: RE:[ntdev] SetupDiGetClassDevs does not returns present device





    <What kind of devices are missing? Do you get different results if you
    < run the app as administrator? Is it possible the device has some kind
    < of protocol issue that is causing it to come and go?

    I am talking about SAMSUNG Android ADB Interface - Android Debug Bridge(it is a child of SAMSUNG Mobile USB Composite Device). The adb is working properly, devices is not come and go.
    I have run as admin also with the same result.


    < If your Windows in 64-bit, make sure to build your program also as 64-bit.
    the same for x64 app

    < Does the registry key
    < HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\YOUR_VID&YOUR_PID exist? Do you see DeviceDesc value there? Which type is it?

    Yes, it is @oem36.inf,%compositeadbinterface%;SAMSUNG Android ADB Interface

    And as I say I can find device without DIGCF_PRESENT flag.. i can't get why.


    ---
    NTDEV is sponsored by OSR

    Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

    OSR is HIRING!! See http://www.osr.com/careers

    For our schedule of WDF, WDM, debugging and other seminars visit:
    http://www.osr.com/seminars

    To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
    d
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    I have tried also
    deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL,
    (DIGCF_ALLCLASSES | DIGCF_PRESENT));

    (with NULL ClassGuid parameter) - that should "return devices for all device setup classes" but the same no such device with DIGCF_PRESENT.
  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,776
    [email protected] wrote:
    > I have tried also
    > deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL,
    > (DIGCF_ALLCLASSES | DIGCF_PRESENT));
    >
    > (with NULL ClassGuid parameter) - that should "return devices for all device setup classes" but the same no such device with DIGCF_PRESENT.

    Does "devcon find" find the device? If so, then you're doing something
    wrong. It uses SetupDiGetClassDevs.

    --
    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • Paul_YPaul_Y Member Posts: 1

    (I know it's been a few years, but even after all that time
    this question manages to appear on the first page of some
    search engines when "DIGCF_PRESENT" is included.
    THIS is a great example of what an "Obscure question" is! :smile: )
    .
    For just USB devices, I'd go with:

        hDevInfo = SetupDiGetClassDevsW(NULL, L"USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
    
  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 8,253

    Sigh.

    Discussion closed. Start a new discussion if you want to continue this issue.

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

This discussion has been closed.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Writing WDF Drivers 7 Dec 2020 LIVE ONLINE
Internals & Software Drivers 25 Jan 2021 LIVE ONLINE
Developing Minifilters 8 March 2021 LIVE ONLINE