Share via


Quickstart: Enumerating device containers (HTML)

The Plug and Play (PnP) object types in the PnpObjectType enumeration store device information associated with a particular device interface, the device that the interface is part of, a class of device interfaces, or the device container that represents the entire hardware product. The device container describes the visible aspects of a device, such as manufacturer or model name. Windows.Devices.Enumeration.DeviceInformation represents the same type as PnpObjectType.DeviceInterface.

The Windows.Devices.Enumeration.PnP namespace allows you to enumerate devices and device containers, as well as devices and device interfaces. This topic shows you how to use the Windows.Devices.Enumeration.PnP namespace to enumerate device containers.

Objective: To enumerate properties of device containers.

Prerequisites

You should be familiar with JavaScript and HTML.

Time to complete: 20 minutes.

Instructions

1. Open Microsoft Visual Studio

Open an instance of Microsoft Visual Studio.

2. Create a new project

In the New Project dialog box, from the JavaScript > Windows Store apps project types, click Blank Application.

3. Insert the application HTML

Open your Default.html and copy this code into the file, replacing the file's contents.


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="/js/default.js"></script>
</head>
<body data-design-activate="defaultPage.activated">
    <h1>Device Enumeration Sample</h1>

    <h2 >Input</h2>

    <div>            

       <div id="Input">
         <p>This scenario demonstrates enumerating device containers.</p>
          <p>Pressing the enumerate button will start a
             search for device containers.
             The containers will be listed below.</p>
          <input onclick="onEnumerateDeviceContainers()" type="button" value="Enumerate" />
                <br /><br />
       </div>
    </div>


    <h2> Output</h2>

            <div id="statusMessage"></div>

            <!-- Container Enumeration results are displayed in this element -->
            <div  id="output"></div>
</body>
</html>

4. Insert the Javascript

In Default.js, insert this code.


function onEnumerateDeviceContainers() {
    try {

        document.getElementById("output").innerHTML = "";

        var propertiesToRetrieve = new Array();
        propertiesToRetrieve.push("System.ItemNameDisplay");
        propertiesToRetrieve.push("System.Devices.ModelName");
        propertiesToRetrieve.push("System.Devices.Manufacturer");

        var DevEnum = Windows.Devices.Enumeration;
        var Pnp = DevEnum.Pnp;
        var pnpObjType = Pnp.PnpObjectType;
        var deviceContainerType = pnpObjType.deviceContainer;

        Pnp.PnpObject.findAllAsync(
            deviceContainerType, 
            propertiesToRetrieve).then(
                function (devinfoCollection) {
                    var numDevices = devinfoCollection.length;
                    document.getElementById("statusMessage").innerHTML = 
                        numDevices + " device containers(s) found";
                    if (numDevices) {
                        for (var i = 0; i < numDevices; i++) {
                            printDeviceContainer(devinfoCollection[i], 
                            document.getElementById("output"));
                        }
                    } else {
                    document.getElementById("statusMessage").innerHTML = 
                        ("No devices found");
                    }
                },
                function (e) {
                    document.getElementById("statusMessage").innerHTML = 
                        ("Failed to find devices, error: " + e.message);
                });
    } catch (e) {
        document.getElementById("statusMessage").innerHTML = 
            ("Failed to enumerate devices, error: " + e.message);
    }
}


function printProperties(log, prop) {
    log.innerHTML += "property store count is: " + prop.size;
    var pt = prop.first();
    while (pt.hasCurrent) {
        log.innerHTML += "<br />" + pt.current.key + " := " + pt.current.value;
        pt.moveNext();
    }
    log.innerHTML += "<br />";
}

function printDeviceContainer(deviceContainer, log) {
    var prop = deviceContainer.properties;
    if (prop) {
        log.innerHTML += "<h3>" + prop.lookup("System.ItemNameDisplay") + "<h3/>";
        log.innerHTML += "<p>Container ID: " + deviceContainer.id + "<p/>";
        printProperties(log, prop);
    }
    log.innerHTML += "<br /><br />";
}

Summary

You've just enumerated device container properties. Note that the requestedProperties parameter to Windows.Devices.Enumeration.Pnp.findAllAsync is required for the results of a container enumeration to contain properties.