# 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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://malwaresourcecode.com/home/component-object-model/coenumupnpdevices.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
