Get mobile device info with Windows API

34 Views Asked by At

I want to detect any USB storage device connected to Windows.

With this function, I can detect Hard disks and USB flash drives:

HDEVINFO deviceInfoSet = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, NULL, NULL,
                                             DIGCF_PRESENT | DIGCF_DEVICEINTERFACE | DIGCF_PROFILE);                                         
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
                                             
for (DWORD dwDeviceIndex = 0; SetupDiEnumDeviceInfo(deviceInfoSet, dwDeviceIndex, &deviceInfoData); dwDeviceIndex++)
{
    DEVINST hDevInst = deviceInfoData.DevInst;
    DEVINST hParentDevInst;
    CONFIGRET cRet = CM_Get_Parent(&hParentDevInst, hDevInst, 0);
    TCHAR wsParentDeviceID[MAX_DEVICE_ID_LEN];
    cRet = CM_Get_Device_ID(hParentDevInst, wsParentDeviceID, sizeof(wsParentDeviceID) / sizeof(TCHAR), 0);
    TCHAR wsDeviceID[MAX_DEVICE_ID_LEN];
    cRet = CM_Get_Device_ID(hDevInst, wsDeviceID, sizeof(wsDeviceID) / sizeof(TCHAR), 0);
    
    // Get device friendly name
    DEVPROPTYPE propertyType;
    WCHAR deviceName[MAX_PATH];
    DWORD bufferSize = sizeof(deviceName);
    if (SetupDiGetDeviceProperty(deviceInfoSet, &deviceInfoData, &DEVPKEY_Device_FriendlyName, &propertyType,
        reinterpret_cast<PBYTE>(deviceName), bufferSize, NULL, 0))
    {
        std::wcout << L"Device friendly name = " << deviceName << std::endl;
    }
    else
    {
        std::wcout << L"No device friendly name detected\n";
    }

    // Get manufacturer
    WCHAR manufacturer[MAX_PATH];
    bufferSize = sizeof(manufacturer);
    if (SetupDiGetDeviceRegistryProperty(deviceInfoSet, &deviceInfoData, SPDRP_MFG, NULL,
        reinterpret_cast<PBYTE>(manufacturer), bufferSize, NULL))
    {
        std::wcout << L"Device manufacturer = " << manufacturer << std::endl;
    }
    else
    {
        std::wcout << L"No device manufacturer detected\n";
    }
}
if (deviceInfoSet)
{
    SetupDiDestroyDeviceInfoList(deviceInfoSet);
}

I get this info for a USB flash and the OS hard disk:

Device friendly name = VendorCo ProductCode USB Device
Device manufacturer = (Standard disk drives)
DeviceId = USBSTOR\DISK&VEN_VENDORCO&PROD_PRODUCTCODE&REV_2.00\3755241199071418340&0
Parent DeviceId = USB\VID_FFFF&PID_5678\3755241199071418340

Device friendly name = VBOX HARDDISK
Device manufacturer = (Standard disk drives)
DeviceId = SCSI\DISK&VEN_VBOX&PROD_HARDDISK\4&2621AEAE&0&000000
Parent DeviceId = PCI\VEN_8086&DEV_2829&SUBSYS_00000000&REV_02\3&267A616A&0&68

Using the deviceId of the disks, I can call other functions and get the volumes, and then the volume names, which are friendly enough to show to the users (e.g. C: Programs, D: My data), but the thing is with these filters I can't detect mobile devices (smartphones) connected using USB port, and I want to detect them too.

I can detect mobiles using the same functions but with this generic USB port filters:

HDEVINFO deviceInfoSet = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE,
                               NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

Now I can detect the mobile, the disk and the flash drive:

No device friendly name detected
Device manufacturer = Huawei
DeviceId = USB\VID_2717&PID_FF40\D323C6439871
Parent DeviceId = USB\ROOT_HUB30\4&24054718&0&0

No device friendly name detected
Device manufacturer = (Standard system devices)
DeviceId = USB\VID_80EE&PID_0021\5&12C8F4C0&0&1
Parent DeviceId = USB\ROOT_HUB30\4&24054718&0&0

No device friendly name detected
Device manufacturer = Compatible USB storage device
DeviceId = USB\VID_FFFF&PID_5678\3755241199071418340
Parent DeviceId = USB\ROOT_HUB30\4&24054718&0&0

However, now the info I get is even more cryptic. I want to get some "friendly name" of the USB storage in the same way Windows does, if not for any USB device at least for mobiles (e.g. Huawei model X), which I could list to the user combined with the previous disks interface filter info.

Can I use some kind of filter specific for mobiles to get this info? How can I list only mobiles, or get device name for mobiles?

EDITED

Ok, the best I have found is the next call filtering by Windows Portable Devices (WPD).

HDEVINFO deviceInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_WPD, NULL, NULL, DIGCF_PRESENT);

With this I get information of all the external hard disks, USB flash drives and Android mobiles I have connected by now, although I need to do more tests. I get the device manufacturer of all the devices I have tested, and the friendly name of the major part, excepting some mobiles. The device model is undefined for all the devices and gives me no information. If someone has a solution which gives more details about the devices, please add it as an answer below.

0

There are 0 best solutions below