関連する PnP オブジェクトの取得 (HTML)

このトピックでは、相互に関連するプラグ アンド プレイ (PnP) オブジェクトを検索する方法について説明します。 たとえば、デバイス情報オブジェクトに関連するデバイス コンテナー オブジェクトを取得できます。 デバイス コンテナーは、ユーザーから見た物理デバイスを表します。デバイス コンテナーを使うと、デバイス ハードウェア製品の機能インターフェイスの 1 つに限定された情報ではなく、デバイス ハードウェア製品全体に関連する情報にアクセスできます。デバイス コンテナーのプロパティの例として、製造元やモデル名があります。

PnP オブジェクト間の関係とは

このトピックの表に、相互に移動するときに使う必要があるさまざまな PnpObjectType オブジェクトとプロパティの関係を示します。 DeviceInformation オブジェクトは、PnpObjectTypedeviceInterfacePnpObject と同じです。

多対 1 の関係

このセクションの表に、他のオブジェクトと N 対 1 の関係があるオブジェクトからの移動方法を示します。たとえば、1 つ以上のデバイス インターフェイス (DeviceInformation オブジェクトで表される) が同じデバイス コンテナーに属している場合があります。 先頭から 2 列目までの列見出しには、開始するプラグ アンド プレイ (PnP) オブジェクトの種類が示されています。3 列目には、検索する関連の PnP オブジェクトが示されています。表内のセルには (入力されている場合)、関連する PnP オブジェクトの照会に使うことができるプロパティが示されています。

列見出しにリストされているオブジェクトから 3 列目にリストされているオブジェクトまで移動するには、次の手順を実行します。

  1. 開始したオブジェクトに対応する見出しと同じ列内にあり、検索する関連オブジェクトと同じ行にあるプロパティを検索します。
  2. 関連する PnP オブジェクトの取得」で説明した手順でプロパティを取得します。
  3. 取得した値を、PnpObject.createFromIdAsync の呼び出しで ID パラメーターとして使い、関連する型のオブジェクトを作成します。
デバイス インターフェイス (DeviceInformation) デバイス 関連オブジェクト
System.Devices.ContainerId System.Devices.ContainerId deviceContainer
System.Devices.DeviceInstancePath device
System.Devices.DeviceInterfaceClassGuid deviceInterfaceClass

 

1 対多の関係

このセクションの表に、他のオブジェクトと 1 対 N の関係があるオブジェクトからの移動方法を示します。 開始するオブジェクト (先頭行) から右側にリストされている関連オブジェクトまで移動するには、次を実行します。

  1. 開始したオブジェクトの見出しと同じ列内にあり、検索する関連オブジェクトと同じ行にあるプロパティを検索します。

  2. 開始オブジェクト (見出し行) の ID と表に示されているプロパティを使って、形式 "<property>:=<id>" の AQS 文字列を作ります。

    たとえば、ID が "{1451362b-4b9c-4780-a4bf-6c001619646c}" の、コンテナー内のすべてのインターフェイスを検索する場合、AQS 文字列は "System.Devices.ContainerId:={1451362b-4b9c-4780-a4bf-6c001619646c}" となります。

  3. AQS 文字列を Windows.Devices.Enumeration.Pnp.PnpObject.findAllAsync 呼び出しのパラメーターとして使い、関連する型のオブジェクトを検索します。

      GUID を含むプロパティを使う場合、AQS 文字列を作るときに GUID 値を中かっこで囲む必要があります。

     

deviceContainer device deviceInterfaceClass 関連オブジェクト
System.Devices.ContainerId System.Devices.DeviceInstancePath System.Devices.DeviceInterfaceClassGuid deviceInterface
System.Devices.ContainerId System.Devices.DeviceInstancePath device

 

DeviceInformation ID からコンテナーを取得する

デバイス インターフェイスが属するコンテナーのプロパティの取得が必要な場合があります。たとえば、選んだデバイスのデバイス ID があっても、モデル名を取得する必要がある場合、System.Devices.ModelName はデバイス インターフェイス プロパティではなくコンテナー プロパティであるため、コンテナー オブジェクトを作る必要があります。

このコードでは、デバイス ID から始まるコンテナー プロパティを取得します。


// GetModelNameFromDeviceID gets the ModelName property from the
// device interface's container.
// The parameter devID is assumed to be a valid device interface ID.
string GetModelNameFromDeviceID (devID)
{
// First create a DeviceInformation object from the ID.
// Create the argument that specifies that additional properties
// returned should include the System.Devices.ContainerId property.
   var propertiesToGet = new Array();
   propertiesToGet.push("System.Devices.ContainerId");


// Create a DeviceInformation object from the ID.
   var Enum = Windows.Devices.Enumeration;
   var DevInfo = Enum.DeviceInformation;
   var contID;   // The container ID
   DevInfo.createFromIdAsync(devID, propertiesToGet).then(
       function(devInf) {             
            var prop = devInf.properties;
            if (prop) {
                contID = prop.lookup("System.Devices.ContainerID");               
            }
       },
       function (e) {
           displayError("Failed to create DeviceInformation: " + e.message);
       });

// Use the container ID to create a PnPObject representing the container,
// and specify that the created object should have a 
// System.Devices.ModelId property.


// Create the argument that specifies that the additional properties to
// return should include the System.Devices.ContainerId property.
   var containerPropertiesToGet = new Array();
   containerPropertiesToGet.push("System.Devices.ModelId");

   var modelID;


var Pnp = Enum.Pnp;
var pnpObjType = Pnp.PnpObjectType;
var deviceType = pnpObjType.device;

// NOTE: We need to add extra braces "{}" back to the container ID before
// passing it to createIdAsync (a pair of braces is lost in the implicit
// conversion from GUID to string).
   contID = "{" + contID + "}";

// Create the container from its ID.
   Pnp.createFromIdAsync(deviceType, contID, containerPropertiesToGet).then(
       function(contInf) {
           var prop = contInf.properties;
           if (prop) {
               modelID = prop.lookup("System.Devices.ModelID");
           });
   return modelID;
}

  

前の例に示されているように、プロパティ参照の GUID 結果を、GUID を取得する関数に文字列引数として渡す場合は、関数に渡す前に GUID 引数を囲む中かっこ "{}" を追加する必要があります。これは、プロパティ参照の結果が文字列ではなくバリアント型であるため、引数が文字列を受け取る関数に渡されると、中かっこのペアが暗黙的な変換で失われるためです。

 

特定のデバイス コンテナー内のすべての DeviceInformation オブジェクトを取得する

この例では、指定されたデバイス コンテナー内のすべての DeviceInformation オブジェクトを検索する方法を示します。これを行うには、AQS クエリを作成して、そのクエリを findAllAsync に渡します。

function findInterfacesInContainer(containerId) {

    var Enum = Windows.Devices.Enumeration;
    var aqsString = "System.Devices.ContainerId:=\"" + containerId + "\"";

    Enum.DeviceInformation.findAllAsync(aqsString, null).then(
            successCallback, 
            errorCallback);
}

// Handles successful completion of the findAllAsync method.
function successCallback(deviceInformationCollection) {
    // Add your code here for processing the enumerated device collection.
}

// Handles an error completion of the findAllAsync method.
function errorCallback(e) {
    // Add your error-handling code here.
}

関連トピック

デバイスまたは PnP オブジェクトの追加のプロパティを取得する方法

AQS クエリ