SOL9 2.0 Class: UPnPDevices

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * UPnPDevices.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/12/02

#pragma once
#include <sol/upnp/UPnPCollection.h>
#include <sol/upnp/UPnPServices.h>
#include <sol/upnp/UPnPDevice.h>

namespace SOL {

class UPnPDevices :public UPnPCollection {

public:
    /**
     * Constructor
     */
    UPnPDevices(IDispatch* pDev=NULL)
    :UPnPCollection(pDev)
    {
    }


public:
    /**
     * Destructor
     */
    ~UPnPDevices()
    {
    }

public:
    IUPnPDevices* get() 
    {
        return (IUPnPDevices*)getIDispatch();
    }

public:
    /**
     * Extract a device specified by uniqueDevice name parameter from devices of IUPnPDevices (pDevices),
     * and return it.
     *
     */
    IUPnPDevice* getDeviceFromDeviceName(__in const wchar_t* name)
    {
        HRESULT hr = E_FAIL;
        // Invalid arguments?
        if (name == NULL) {
            throw E_POINTER;
        }
            
        IUPnPDevice * pDevice = NULL;
            
        IUPnPDevices* pDevices = get();

        EnumVariant enumVariant = getNewEnum();

        IDispatch* pdispDevice = NULL;

        try {
            while ((pdispDevice = enumVariant.next()) !=NULL) {
                UPnPDevice dispDevice(pdispDevice);

                try {                
                    pDevice = (IUPnPDevice*)dispDevice.queryInterface(IID_IUPnPDevice);

                    UPnPDevice device = pDevice;                    
                
                    StringT<wchar_t> fname=L"";
            
                    device.getUniqueDeviceName(fname);
                    if (fname.equals(name)) {
                        //OK! We found it, return pDevice breaking this loop.
                        device.set(NULL); 
                        break;
                    }
                    //If we had children of devices.            
                    IUPnPDevices* children = NULL;
                    if (SUCCEEDED(device.getChildren(&children))) {
                        // If this has children devices, do get a device of name from the children.
                        UPnPDevices subDevices(children);
    
                        pDevice = subDevices.getDeviceFromDeviceName(name);
                    }
                } catch (...) {
                }
            }
        } catch (...) {

        }
                    
        return pDevice;
    }

public:
    long getCount()
    {
        IUPnPDevices* pDevices = get();
        long count = -1;
            
        if (FAILED( pDevices->get_Count(&count))) {
            throw -1;    
        }
        return count;
    }

public:
    HRESULT getCount(long* count)
    {
        IUPnPDevices* pDevices = get();
    
        return pDevices->get_Count(count);
    }


public:
    void display()
    {
        printf("UPnPDevices Count = %d\n", getCount());
            
        IUPnPDevices* pDevices = get();
    
        HRESULT hr = E_FAIL;

        try {
            EnumVariant enumVariant = getNewEnum();

            IDispatch * pDisp = NULL;

            while ((pDisp = enumVariant.next()) ) {
                UPnPDevices devices = pDisp;    
                UPnPDevice upnDevice = devices.queryInterface(IID_IUPnPDevice);

                upnDevice.display();
                                
                printf(".....Children\n");
                IUPnPDevices* children = NULL;

                if (SUCCEEDED(upnDevice.getChildren(&children))) {
                    UPnPDevices subDevices(children);
                        //Display properties of children devices
                    subDevices.display();
                }
            }    
        } catch (...) {

        }
    }
};

}
//

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.