potisanのプログラミングメモ

趣味のプログラマーがプログラミング関係で気になったことや調べたことをいつでも忘れられるようにメモするブログです。はてなブログ無料版なので記事の上の方はたぶん広告です。記事中にも広告挿入されるみたいです。

C++/WinRT バッテリーのデバイスセレクター文字列とレポートを取得する

MicrosoftのWinRTリファレンス

動作確認環境

ソースコード

名前空間の省略、元の型の優先

#include "winrt/Windows.Devices.Power.h"

using namespace winrt;
using namespace Windows::Devices::Power;

#include <iostream>

int main()
{
    // WinRTをマルチスレッド用に初期化
    init_apartment();

    // 接続中のすべてのバッテリーコントローラーを表すバッテリーオブジェクトの取得
    Battery battery{ Battery::AggregateBattery() };

    // デバイスセレクタの文字列表現とバッテリーレポートの取得
    hstring device_selector{ battery.GetDeviceSelector() };
    BatteryReport report{ battery.GetReport() };

    // 出力
    const std::array<const wchar_t*, 4> str_BatteryStatus{ L"NoPresent", L"Discharging", L"Idle", L"Charging" };
    std::wcout << (std::wstring_view)device_selector << std::endl;
    std::wcout << str_BatteryStatus[static_cast<int>(report.Status())] << std::endl;

    return 0;
}

名前空間の省略、auto型の優先

#include "winrt/Windows.Devices.Power.h"

using namespace winrt;
using namespace Windows::Devices::Power;

#include <iostream>

int main()
{
    // WinRTをマルチスレッド用に初期化
    init_apartment();

    // 接続中のすべてのバッテリーコントローラーを表すバッテリーオブジェクトの取得
    auto battery{ Battery::AggregateBattery() };

    // デバイスセレクタの文字列表現とバッテリーレポートの取得
    auto device_selector{ battery.GetDeviceSelector() };
    auto report{ battery.GetReport() };

    // 出力
    const std::array<const wchar_t*, 4> str_BatteryStatus{ L"NoPresent", L"Discharging", L"Idle", L"Charging" };
    std::wcout << (std::wstring_view)device_selector << std::endl;
    std::wcout << str_BatteryStatus[static_cast<int>(report.Status())] << std::endl;

    return 0;
}

名前空間の明記、元の型の優先

#include "winrt/Windows.Devices.Power.h"

#include <iostream>

int main()
{
    // WinRTをマルチスレッド用に初期化
    winrt::init_apartment();

    // 接続中のすべてのバッテリーコントローラーを表すバッテリーオブジェクトの取得
    winrt::Windows::Devices::Power::Battery battery{ winrt::Windows::Devices::Power::Battery::AggregateBattery() };

    // デバイスセレクタの文字列表現とバッテリーレポートの取得
    winrt::hstring device_selector{ battery.GetDeviceSelector() };
    winrt::Windows::Devices::Power::BatteryReport report{ battery.GetReport() };

    // 出力
    const std::array<const wchar_t*, 4> str_BatteryStatus{ L"NoPresent", L"Discharging", L"Idle", L"Charging" };
    std::wcout << (std::wstring_view)device_selector << std::endl;
    std::wcout << str_BatteryStatus[static_cast<int>(report.Status())] << std::endl;

    return 0;
}

説明

WinRTの使用に関する事項

  • #include <winrt/Windows.Devices.Power.h>ではなく#include "winrt/Windows.Devices.Power.h"を使用します。C++/WinRTのファイルはMicrosoft.Windows.CppWinRT NuGetパッケージによりプロジェクト以下に最新版が展開されます。<...>を使用するとSDKの古いファイルが参照されてしまいます。
  • #include "winrt/Windows.Devices.Power.h"は内部で公開するクラスが必要とするヘッダーファイルを一通りインクルードします。
  • C++/WinRTのメンバーはwinrt名前空間に定義されます。公式ドキュメントのサンプルコードにならい、ここでもusing namespace ...;を使用しています。
  • using namespace Windows::Devices::Power;using namespace winrt::Windows::Devices::Power;を先のusing namespace winrt;により省略したものです。競合がなければ後者を使用しても同じです。

WinRTの一般事項

  • winrt::init_apartmentはWinRTを初期化します。引数にスレッドモデルを指定でき、既定値はマルチスレッドです。
  • winrt::hstringはWinRTのカスタム文字列型です。構造体として定義されており、STL準拠のメンバー関数を持ちます。概要はC++/WinRT での文字列の処理 - Microsoft Docsを参照ください。

コード特有の事項

  • const std::array<const wchar_t*, 4> str_BatteryStatus[]演算子の範囲チェックのために使用しています。