WPDのデバイスIDの取得、WPDデバイスへ接続してデバイス名を取得する

WPDのデバイスIDの取得、WPDデバイスへ接続してデバイス名を取得するコードを紹介します。

概要

WPDのデバイスIDを取得するコードと、取得したWPDデバイスIDを利用してWPDデバイスに接続してデバイス情報を取得しデバイス名を取得するコードを紹介します。

事前準備

こちらの記事を参照して、PortableDeviceApiのCOMオブジェクトを参照に追加してください。

プログラム例

Windows Form アプリケーションを作成します。

UI

下図のUIを作成します。フォームに複数行のテキストボックスを1つ配置します。
WPDのデバイスIDの取得、WPDデバイスへ接続してデバイス名を取得する:画像1

コード

下記のコードを記述します。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PortableDeviceApiLib;

namespace GetWpdDevice
{
  public partial class FormMain : Form
  {
    public uint WPD_OBJECT_NAME = 4;

    private PortableDeviceManager deviceManager;

    public FormMain()
    {
      InitializeComponent();
    }

    private void FormMain_Load(object sender, EventArgs e)
    {
      deviceManager = new PortableDeviceManager();

      deviceManager.RefreshDeviceList();
      uint count = 0;
      deviceManager.GetDevices(null, ref count);

      textBox1.Text += "WPDデバイスの数:"+count.ToString()+"\r\n";
      textBox1.Text += "----\r\n";
      textBox1.Text += "\r\n";

      string[] devicesIDs = new string[count];
      PortableDeviceClass[] devices = new PortableDeviceClass[count];

      deviceManager.GetDevices(devicesIDs, ref count);
      for (int i = 0; i < count; i++) {
        textBox1.Text += "WPDデバイスID:" + devicesIDs[i] + "\r\n";
        devices[i] = new PortableDeviceClass();
        textBox1.Text += "\r\n";
      }
      
      IPortableDeviceValues clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();

      for (int i = 0; i < count; i++) {
        devices[i] = new PortableDeviceClass();
        devices[i].Open(devicesIDs[i], clientInfo);

        IPortableDeviceContent content;
        devices[i].Content(out content);

        IPortableDeviceProperties properties;
        content.Properties(out properties);

        IPortableDeviceValues propertyValues;
        properties.GetValues("DEVICE", null, out propertyValues);

        devices[i].Close();

        string name;
        _tagpropertykey property = new _tagpropertykey();
        property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        property.pid = WPD_OBJECT_NAME;
        try {
          propertyValues.GetStringValue(property, out name);

          textBox1.Text += string.Format("WPDデバイス名({0:d}):{1}\r\n", i, name);
        }
        catch (System.Runtime.InteropServices.COMException) {
          textBox1.Text += string.Format("WPDデバイス名({0:d}):なし\r\n", i);
        }
        textBox1.Text += "\r\n";
      }

      textBox1.SelectionStart = textBox1.Text.Length;
    }
  }
}

解説

下記コードで PortableDeviceManager オブジェクトを作成します。

  deviceManager = new PortableDeviceManager();


PortableDeviceManagerオブジェクトのRefreshDeviceList メソッドを呼び出しデバイスのリストを更新します。

  deviceManager.RefreshDeviceList();


PortableDeviceManagerオブジェクトのGetDevicesメソッドを呼び出し、接続されている WPDデバイスの数を取得します。GetDevicesメソッドの第一引数にnull、第二引数に0を与えてGetDevicesメソッドを呼び出し、デバイスの数を取得します。

  uint count = 0;
  deviceManager.GetDevices(null, ref count);


GetDevicesメソッドの第二引数に接続されているデバイス数が代入されます。デバイス数をテキストボックスに表示します。

  textBox1.Text += "WPDデバイスの数:"+count.ToString()+"\r\n";

デバイス数が判明したので、デバイスIDを取得します。string型の配列を作成し、接続されているWPDデバイスの数だけ配列を準備します。再度 GetDevicesメソッドを呼び出します。第一引数にデバイスIDを格納するstring型の配列、第二引数にdevicesIDsの配列数を与えます。与えた配列数に格納できるデバイスIDを返します。

  string[] devicesIDs = new string[count];
  deviceManager.GetDevices(devicesIDs, ref count);


GetDevices メソッドが正常終了すると第一引数の deviceIDs 配列にデバイスIDが格納されます。deviceIDsの内容をテキストボックスに表示し画面にデバイスIDを表示します。

  for (int i = 0; i < count; i++) {
    textBox1.Text += "WPDデバイスID:" + devicesIDs[i] + "\r\n";
    textBox1.Text += "\r\n";
  }


ループ処理で取得したデバイスIDごとにデバイスの情報を取得します。

  IPortableDeviceValues clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();

  for (int i = 0; i < count; i++) {
  /* 中略 */
  }


PortableDeviceClass オブジェクトを作成しOpen メソッドを呼び出しWPDデバイスを開きます。Openメソッドの第一引数に先に取得したWPDデバイスのデバイスIDを与えます。第二引数にはPortableDeviceValuesClass オブジェクトを与えます。

  devices[i] = new PortableDeviceClass();
  devices[i].Open(devicesIDs[i], clientInfo);


デバイスを開いた後、Contentメソッドを呼び出しデバイスの情報を取得します。Contentメソッドの第一引数にIPortableDeviceContent オブジェクトを与えます。

  IPortableDeviceContent content;
  devices[i].Content(out content);

IPortableDeviceContent オブジェクトのProperties メソッドを呼び出しプロパティ情報を取得します。

  IPortableDeviceProperties properties;
  content.Properties(out properties);

Properties オブジェクトのGetValues メソッドを呼び出しデバイス情報の値を取得します。

  IPortableDeviceValues propertyValues;
  properties.GetValues("DEVICE", null, out propertyValues);

デバイス情報の取得が完了しましたので、Close メソッドを呼び出しデバイスを閉じます。

  devices[i].Close();

_tagpropertykey オブジェクトを作成し、取得する情報の項目を設定します。デバイス名を取得する場合はGUIDで"0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C"すなわち、{ef6b490d-5cd8-437a-affc-da8b60ee4a3c}を与えます。またpidには取得する項目のフラグ値を設定します。今回はオブジェクトの名称を取得するため、WPD_OBJECT_NAME を与えます。

  string name;
  _tagpropertykey property = new _tagpropertykey();
  property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
  property.pid = WPD_OBJECT_NAME;


IPortableDeviceValues オブジェクトのGetStringValue メソッドを呼び出しデバイス名を取得します。デバイス名が無いデバイスの場合は COMException 例外が返るため、COMException例外の場合はデバイス名を「なし」とします。取得したデバイス名をテキストボックスに表示します。

  try {
    propertyValues.GetStringValue(property, out name);

    textBox1.Text += string.Format("WPDデバイス名({0:d}):{1}\r\n", i, name);
  }
  catch (System.Runtime.InteropServices.COMException) {
    textBox1.Text += string.Format("WPDデバイス名({0:d}):なし\r\n", i);
  }
  textBox1.Text += "\r\n";

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。テキストボックスにWPDのデバイスIDとWPDのデバイス名が表示されていることが確認できます。
WPDのデバイスIDの取得、WPDデバイスへ接続してデバイス名を取得する:画像2

AuthorPortraitAlt
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
作成日: 2018-08-12
Copyright © 1995–2025 iPentec all rights reserverd.