void ListDevices()

in BusTools/MinComm/main.cpp [229:291]


void ListDevices ()
{
    ULONG length;
    CONFIGRET cr = CM_Get_Device_Interface_List_SizeW(
            &length,
            const_cast<GUID*>(&GUID_DEVINTERFACE_COMPORT),
            nullptr,        // pDeviceID
            CM_GET_DEVICE_INTERFACE_LIST_PRESENT);

    if ((cr != CR_SUCCESS) || (length == 0)) {
        throw wexception(
            L"Failed to get size of device interface list. "
            L"(length = %lu, cr = 0x%x)",
            length,
            cr);
    }

    std::vector<WCHAR> buf(length);
    cr = CM_Get_Device_Interface_ListW(
            const_cast<GUID*>(&GUID_DEVINTERFACE_COMPORT),
            nullptr,        // pDeviceID
            buf.data(),
            static_cast<ULONG>(buf.size()),
            CM_GET_DEVICE_INTERFACE_LIST_PRESENT);

    if ((cr != CR_SUCCESS) || (length == 0)) {
        throw wexception(
            L"Failed to get device interface list. (length = %lu, cr = 0x%x)",
            length,
            cr);
    }

    if (!buf[0]) {
        wprintf(L"No serial devices were found.\n");
        return;
    }

    *buf.rbegin() = UNICODE_NULL;

    for (PCWSTR deviceInterface = buf.data();
        *deviceInterface;
        deviceInterface += wcslen(deviceInterface) + 1) {

        auto portName = GetPortName(deviceInterface);
        bool isRestricted = IsRestricted(deviceInterface);
        bool isInSystemContainer = IsInSystemContainer(deviceInterface);
        bool isUwpAccessible = !isInSystemContainer ||
                               (isInSystemContainer && !isRestricted);

        wprintf(
            L"%s\n"
            L"    PortName: %s\n"
            L"    In System Container: %s\n"
            L"    Restricted: %s\n"
            L"    UWP Accessible: %s\n"
            L"\n",
            deviceInterface,
            portName.empty() ? L"(not set)" : portName.c_str(),
            isInSystemContainer ? L"TRUE" : L"FALSE",
            isRestricted ? L"TRUE" : L"FALSE",
            isUwpAccessible ? L"TRUE" : L"FALSE");
    }
}