1

I have a printer connected with USB port and I want to get some information about it. I am using SetupDiEnumDeviceInfo function from setupapi to get information. I am doing everything as it is described in MSDN.

#include <string>
#include <windows.h>
#include <vector>
#include <iostream>
#include <setupapi.h>
#include <winerror.h>

#pragma comment (lib, "SetupAPI.lib")
static GUID GUID_DEVCLASS_PORTS = { 0x4d36e978, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };

int main()
{
    SP_DEVINFO_DATA devInfoData;

    HDEVINFO deviceInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);
    devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    DWORD nDevice = 0;

    if (SetupDiEnumDeviceInfo(deviceInfo, nDevice, &devInfoData))
    {
    }
    return 0;
}

The problem is that I am always getting false result. GetLastError() function retruns 259. What am I doing wrong?

Gor Asatryan
  • 904
  • 7
  • 24
  • Not sure if it will fix it, but your GUID definition maybe needs a pair of extra braces; try: `static GUID GUID_DEVCLASS_PORTS = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };` – Adrian Mole Sep 20 '19 at 15:00
  • 1
    It is a documented error code, it simply means that there are no items to enumerate. GUID_DEVCLASS_PORTS is not likely to be correct, printers have not used serial ports for a very long time. You need GUID_DEVCLASS_USB to find USB devices back. – Hans Passant Sep 20 '19 at 15:00
  • @Adrian I have tried. result is the same. – Gor Asatryan Sep 20 '19 at 15:03
  • Have you tried to google your question? https://social.msdn.microsoft.com/Forums/en-US/ed104bce-b874-42a3-b284-c54c03d4394c/setupdienumdeviceinfo-returns-false-with-last-error-0x103-no-more-items?forum=wdk – 273K Sep 20 '19 at 15:08
  • @HansPassant Where can we find the definition of `GUID_DEVCLASS_USB`? – Adrian Mole Sep 20 '19 at 15:08
  • Same place where GUID_DEVCLASS_PORTS came from, devguid.h. Be sure to #include it instead of declaring them yourself. – Hans Passant Sep 20 '19 at 15:10
  • @HansPassant - Never mind, I found it in the Windows DDK: `static GUID GUID_DEVCLASS_USB = { 0x36fc9e60, 0xc465, 0x11cf, { 0x80, 0x56, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 } };` OP - Try this! – Adrian Mole Sep 20 '19 at 15:11
  • 1
    I won't mind you doing this wrong, just keep in mind that it gets to be pretty hard to get help at SO when you do. – Hans Passant Sep 20 '19 at 15:12
  • @HansPassant - Yes, really should use definitions from `devguid.h` - but that header is not always easily accessible, unless DDK is installed. – Adrian Mole Sep 20 '19 at 15:14
  • @HansPassant I have tried with included devguid.h too. – Gor Asatryan Sep 20 '19 at 15:14
  • Did you try using `GUID_DEVCLASS_USB`? – Adrian Mole Sep 20 '19 at 15:17
  • @Adrian, Yes I have. result is the same – Gor Asatryan Sep 20 '19 at 15:18
  • 1
    have you fix it? I add devguid.h, use `GUID_DEVCLASS_USB`,it returns true. – Jeffreys Sep 23 '19 at 04:33
  • @JeffreyShao-MSFT Yes. It works. But now I can't get information from registry. In registry editor I found HardwareID and ClassGUID values, but `SetupDiGetDeviceRegistryProperty(devInfo, devInfoData, SPDRP_CLASSGUID, &propertyRegDataType, (PBYTE)& bfr[0], bfr.size(), &requiredSize)` returns me chinese hieroglyphs "㑻㍤收㜹ⴸ㍥㔲ㄭ挱ⵥ晢ㅣ〭〸㈰敢〱ㄳ紸" – Gor Asatryan Sep 23 '19 at 08:35

1 Answers1

1

this is my sample. I add the devguid.h, and use GUID_DEVCLASS_USB.

#include <string>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#pragma comment (lib, "SetupAPI.lib")

int main()
{
    int res = 0;
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData = { sizeof(DeviceInfoData) };

    // get device class information handle
    hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_USB, 0, 0, DIGCF_PRESENT);
    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
        res = GetLastError();
        return res;
    }

    // enumerute device information
    DWORD required_size = 0;
    for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
    {
        DWORD DataT;
        char friendly_name[2046] = { 0 };
        DWORD buffersize = 2046;
        DWORD req_bufsize = 0;

        // get device description information
        if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_CLASSGUID, &DataT, (PBYTE)friendly_name, buffersize, &req_bufsize))
        {
            res = GetLastError();
            continue;
        }

        char temp[512] = { 0 };
        sprintf_s(temp, 512, "USB device %d: %s", i, friendly_name);
        puts(temp);
    }

    return 0;
}
Jeffreys
  • 431
  • 2
  • 8