0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SetupAPIを使用してデバイスを列挙する

Posted at

課題:特定のカテゴリーのデバイスを列挙したい

ここでは例として[生体認証デバイス]下にあるデバイスを列挙します。
デバイスマネージャー:生体認証デバイス一覧.png

生体認証デバイスは、クラスGUIDが"{53d29ef7-377c-4d14-864b-eb3a85769359}"になります。これを利用して列挙します。
生体認証デバイスのプロパティ:クラスGUID.png

サンプルコード

下記が、クラスGUIDを使用して生体認証デバイスを列挙するサンプルコードになります。

// ====================================================================================================
//  インクルード
// ====================================================================================================
#include <conio.h>
#include <stdio.h>
#include <combaseapi.h>
#include <Setupapi.h>

// ====================================================================================================
//  ライブラリ
// ====================================================================================================
#pragma comment(lib, "Setupapi.lib")

// ====================================================================================================
//  エントリーポイント
// ====================================================================================================
int main()
{
    HRESULT hr = S_OK;
    HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;

    try
    {
        // 生体認証デバイスのGUID
        GUID BiometricGuid;
        CLSIDFromString(L"{53d29ef7-377c-4d14-864b-eb3a85769359}", &BiometricGuid);

        // デバイス情報セットを取得
        hDevInfo = SetupDiGetClassDevs(&BiometricGuid, NULL, NULL, 0);
        if (hDevInfo == INVALID_HANDLE_VALUE)
            throw HRESULT_FROM_WIN32(GetLastError());

        // デバイス情報セットを確認
        SP_DEVINFO_DATA stDevInfo;
        ::ZeroMemory(&stDevInfo, sizeof(stDevInfo));
        stDevInfo.cbSize = sizeof(stDevInfo);

        DWORD dwIndex = 0;

        while (SetupDiEnumDeviceInfo(hDevInfo, dwIndex, &stDevInfo) == TRUE)
        {
            // デバイス名の取得
            DWORD dwRegType = 0;

            wchar_t pwcDeviceName[_MAX_PATH];
            ::ZeroMemory(pwcDeviceName, sizeof(pwcDeviceName));

            DWORD dwSize = _MAX_PATH;

            if (SetupDiGetDeviceRegistryProperty(hDevInfo, &stDevInfo, SPDRP_DEVICEDESC, &dwRegType, (BYTE*)pwcDeviceName, dwSize, &dwSize) == FALSE)
                throw HRESULT_FROM_WIN32(GetLastError());

            // デバイス名を標準出力へ表示
            wprintf(pwcDeviceName);
            wprintf(L"\n");

            // 次のデバイスへ
            ::ZeroMemory(&stDevInfo, sizeof(stDevInfo));
            stDevInfo.cbSize = sizeof(stDevInfo);
 
            dwIndex++;
        }
    }
    catch (HRESULT hrError) { hr = hrError; }
    catch (...) { hr = E_UNEXPECTED; }

    if (hDevInfo == INVALID_HANDLE_VALUE)
        SetupDiDestroyDeviceInfoList(hDevInfo);

    _getch();
    return 0;
}

これを実行すると、下記画像のように生体認証デバイスが列挙できます。
実行結果.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?