0

嗨 Stack Overflow,对不起我的非母语英语水平

目前在 Visual Studio 2010 上使用 .NET 4.0 为 C# 项目编写 UPnP 发现服务。

我正在使用官方的 Microsoft UPnP COM API,这是我第一次使用 UPnP。我的问题是我正在尝试迭代库发现的设备的服务,并且抛出了 COM HRESUT:0X80040500 异常。

这是我的代码示例:

IList<UpnpDevice> result = new List<UpnpDevice>();

UPnPDevices upnpDiscoveryResult = m_UPnPFinder.FindByType(upnpType, 0);

var upnpDiscoveryResultEnumerator = upnpDiscoveryResult.GetEnumerator();

while (upnpDiscoveryResultEnumerator.MoveNext())
{
    var upnpDiscoveryDevice = (IUPnPDevice)upnpDiscoveryResultEnumerator.Current;

    UPnPServices services = upnpDiscoveryDevice.Services;
    var allServices = services.GetEnumerator();

    // ------ Exception is thrown just below
    while (allServices.MoveNext())
    {
        UPnPService service = allServices.Current as UPnPService;
        if (service.Id == "urn:schemas-upnp-org:service:WANIPConnection:1")
        {
        }
        else if (service.Id == "urn:schemas-upnp-org:service:WANPPPConnection:1")
        {
        }
    }

我不知道该怎么做。

根据这些人的说法,我认为我可能有同样的错误......

...问题可能来自官方DLL,我想我应该最好使用一个新的,但我想先在这里问。对我来说,这样一个明显的错误确实可能来自 API,这似乎很奇怪。

4

1 回答 1

0

我将为后代回答我自己的问题,“UpnpDevice”是我自己项目中的一个类。

    /// <summary>
    /// Converts the native class UPnP device to a generic UPnP device.
    /// </summary>
    /// <param name="nativeDevice">The native device.</param>
    /// <returns>
    /// The converted <see cref="UpnpDevice"/>
    /// </returns>
    private UpnpDevice ConvertNativeUPnPDeviceToGenericUpnpDevice(IUPnPDevice nativeDevice)
    {
        UpnpDevice genericDevice = null;

        if (nativeDevice != null)
        {
            IList<UpnpDevice> genericDeviceChildren = new List<UpnpDevice>();
            IList<String> genericDeviceServices = new List<String>();

            // Converting recursively the children of the native device
            if (nativeDevice.HasChildren)
            {
                foreach (IUPnPDevice nativeDeviceChild in nativeDevice.Children)
                {
                    genericDeviceChildren.Add(ConvertNativeUPnPDeviceToGenericUpnpDevice(nativeDeviceChild));
                }
            }

            try
            {
                // Converting the services, it may break on some modems like old Orange Liveboxes thus the try/catch
                foreach (IUPnPService nativeDeviceService in nativeDevice.Services)
                {
                    genericDeviceServices.Add(nativeDeviceService.ServiceTypeIdentifier);
                }
            }
            catch (Exception exception)
            {
                string msg = this.GetType().Name + " - Method ConvertNativeUPnPDeviceToGenericUpnpDevice - Reading the services threw an exception: " + exception.Message;
                m_Logger.Error(msg);
            }

            genericDevice = new UpnpDevice(nativeDevice.UniqueDeviceName,
            nativeDevice.Description,
            nativeDevice.FriendlyName,
            nativeDevice.HasChildren,
            nativeDevice.IsRootDevice,
            nativeDevice.ManufacturerName,
            nativeDevice.ManufacturerURL,
            nativeDevice.ModelName,
            nativeDevice.ModelNumber,
            nativeDevice.ModelURL,
            nativeDevice.PresentationURL,
            nativeDevice.SerialNumber,
            nativeDevice.Type,
            nativeDevice.UPC,
            genericDeviceServices,
            genericDeviceChildren);
        }

        return genericDevice;
    }

不是一个例外的答案,但这是我从设备获得所有服务的唯一方法。它会在某些设备上皱眉并继续前进,但至少它会在不破坏整个发现的情况下尽其所能。

于 2013-09-30T13:49:11.417 回答