# CoEnumUPnPDevices

```
#include <upnp.h>

HRESULT CoEnumUPnPDevices(VOID)
{
    HRESULT Result;
    IUPnPDeviceFinder* Finder = NULL;
    IUPnPDevices* Devices = NULL;
    BSTR UPnPType = NULL;
    IUnknown* Enum = NULL;
    IEnumVARIANT* EnumVariant = NULL;
    VARIANT UPnPObject; VariantInit(&UPnPObject);

    Result = CoInitialize(NULL);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = CoCreateInstance(CLSID_UPnPDeviceFinder, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPDeviceFinder, (PVOID*)&Finder);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    UPnPType = SysAllocString(L"ssdp:all");
    if (UPnPType == NULL)
        goto EXIT_ROUTINE;

    Result = Finder->FindByType(UPnPType, 0, &Devices);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = Devices->get__NewEnum(&Enum);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    Result = Enum->QueryInterface(IID_IEnumVARIANT, (PVOID*)&EnumVariant);
    if (!SUCCEEDED(Result))
        goto EXIT_ROUTINE;

    while (EnumVariant->Next(1, &UPnPObject, NULL) == S_OK)
    {
        IUPnPDevice* DeviceObject = (IUPnPDevice*)UPnPObject.pdispVal;
        
        /*
            Stuff to do goes here....
            https://learn.microsoft.com/en-us/windows/win32/api/upnp/nn-upnp-iupnpdevice
            callback routine, or something, whatever
        */

        //EXAMPLE
        BSTR FriendlyName = 0;
        DeviceObject->get_FriendlyName(&FriendlyName); //FriendlyName is BSTR, which is OLECHAR* which is just WCHAR lol
        //EXAMPLE END

        if (DeviceObject)
            DeviceObject->Release();

        if (UPnPObject.vt != VT_EMPTY)
            VariantClear(&UPnPObject);
    }

EXIT_ROUTINE:

    if (UPnPObject.vt != VT_EMPTY)
        VariantClear(&UPnPObject);

    if (UPnPType != NULL)
        SysFreeString(UPnPType);

    if (EnumVariant)
        EnumVariant->Release();

    if (Devices)
        Devices->Release();

    if (Finder)
        Finder->Release();

    CoUninitialize();

    return Result;
}
```

