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 3 years have passed since last update.

【WBF】Windowsのサービス上で指紋認証を行う

Last updated at Posted at 2020-06-12

概要

Windowsのサービス等、画面UIを持たないプログラム上で指紋認証を行う場合は、"WinBioAcquireFocus"関数を使用してフォーカスを取得する必要があります。

サンプルコード

  1. Windowsのサービスを作成します。ここでは下記の記事で作成したプロジェクトを使用します。
    [ATLを使用してWindowsのサービスを作成する]
    https://qiita.com/Leeya/items/1f8f8db7e8cb7dbbbcf2

  2. WBFが使用できるように、必要なファイルをインクルード及びリンクします。

ATLServiceSample.cpp
# include <Winbio.h>
# pragma comment(lib, "Winbio.lib")
  1. サービスのメイン処理に下記のコードを実装します。
ATLServiceSample.cpp
// メイン処理
void RunMessageLoop() throw()
{
# if 1 // 追加 ここから
    // WBFサンプルコード
    WBFSampleCode();
# endif // 追加 ここまで

    MSG msg;
    while (GetMessage(&msg, 0, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

# if 1 // 追加 ここから
// WBFサンプルコード
HRESULT WBFSampleCode()
{
    HRESULT hr = S_OK, hrFocus = S_OK;
    WINBIO_SESSION_HANDLE sessionHandle = NULL;

    try
    {
        // システムプールに接続する
        hr = WinBioOpenSession(WINBIO_TYPE_FINGERPRINT, WINBIO_POOL_SYSTEM, WINBIO_FLAG_DEFAULT, NULL, 0, NULL, &sessionHandle);
        if (FAILED(hr))
            throw hr;

        // フォーカスの取得
        hrFocus = WinBioAcquireFocus();
        if (FAILED(hrFocus))
            throw hrFocus;

        // 指紋認証
        WINBIO_UNIT_ID unitId = 0;
        WINBIO_IDENTITY identity = { 0 };
        WINBIO_BIOMETRIC_SUBTYPE subFactor = 0;
        WINBIO_REJECT_DETAIL rejectDetail = 0;

        hr = WinBioIdentify(sessionHandle, &unitId, &identity, &subFactor, &rejectDetail);
        if (FAILED(hr))
            throw hr;
    }
    catch (HRESULT hrError) { hr = hrError; }
    catch (...) { hr = E_UNEXPECTED; }

    if (SUCCEEDED(hrFocus))
        WinBioReleaseFocus();

    if (sessionHandle != NULL)
        WinBioCloseSession(sessionHandle);

    // イベントログに結果を出力する
    wchar_t pwcLog[_MAX_PATH + 1] = { 0 };
    _snwprintf_s(pwcLog, _MAX_PATH, L"指紋認証結果 [0x%08X]", hr);
    OutputEventLog(pwcLog);

    return hr;
}

// イベントログ
HRESULT OutputEventLog(wchar_t *pwcLog)
{
    HRESULT hr = S_OK;
    HANDLE hEventSource = NULL;

    try
    {
        // イベントログのオープン
        hEventSource = RegisterEventSource(NULL, L"ATLServiceSample");

        // イベントログに出力
        const wchar_t* pwcMsg = &pwcLog[0];
        ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, &pwcMsg, NULL);
    }
    catch (HRESULT hrError) { hr = hrError; }
    catch (...) { hr = E_UNEXPECTED; }

    if(hEventSource != NULL)
        DeregisterEventSource(hEventSource);

    return hr;
}
# endif // 追加 ここまで

実行

  1. このサンプルコードを実行する前に、Windowsの[設定]→[アカウント]→[サインインオプション]→[指紋認識(Windows Hello)]で指紋を登録してください。

  2. ビルドしたEXEを下記コマンドでサービスとして登録します。
    "D:\Work\ATLServiceSample\x64\Release\ATLServiceSample.exe" /Service
    ※ 実行ファイルのパスはそれぞれの環境のものを使用してください。

  3. スタートメニューから[Windowsツール]→[サービス]を開き、[ATLServiceSample]を開始すると指紋センサーが認証の待機状態になるので、指紋センサーをスワイプorタッチしてください。

  4. 指紋認証が完了すると、イベントログに結果が出力されます。
    イベントビューアー.png
    結果が[0x00000000]なら認証成功、[0x80098003]なら未登録の指紋です。

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?