> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XUserAddOptions

> XUserAddOptions

# XUserAddOptions

ユーザーを追加するためのオプションを指定します。

## 構文

```cpp theme={null}
enum class XUserAddOptions  : uint32_t  
{  
    None = 0x00,  
    AddDefaultUserSilently = 0x01,  
    AllowGuests = 0x02,  
    AddDefaultUserAllowingUI = 0x04,
}  
```

## 定数

| 定数                       | 説明                                                                                                                                    |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
| None                     | オプションなし。                                                                                                                              |
| AddDefaultUserSilently   | UI を表示せずに、ゲームを起動したユーザーを取得します。                                                                                                         |
| AllowGuests              | ユーザー ピッカー UI にゲストが含まれます。                                                                                                              |
| AddDefaultUserAllowingUI | ゲームを起動した最初のユーザーをサイレントに取得しようとします。システムが UI を表示せずにそのユーザーを取得できる場合はそれを試みます。そのユーザーが同意を必要とする場合やアカウントの問題を修正する必要がある場合には、問題を解決するために UI が表示されます。 |

## 解説

*options* に XUserAddOptions::AddDefaultUserSilently を設定して [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) を繰り返し呼び出す場合、開発者が知っておくべきエッジ ケースがいくつかあります:

* ゲームを起動した既定のユーザーが判明している状態でこれを繰り返し呼び出すと、同じユーザーが返されます。
* 以前に判明していた既定のユーザーがサインアウトしており、デバイスに 1 人のユーザーだけがサインインしている場合、そのユーザーが新しい「既定」のユーザーとしてマークされ、そのユーザーが返されます。
* 以前に判明していた既定のユーザーがサインアウトしており、デバイスに複数のユーザーがサインインしている場合、E\_GAMEUSER\_NO\_DEFAULT\_USER が返されます。
* 開発者による起動時は、既定のユーザーとなるのは自動サインイン ユーザーです。これは DevHome でそのユーザーを自動サインイン ユーザーとして設定するか、[xbconfig (NDA トピック)](/tools/tools-console/commandlinetools/xbconfig) の DefaultUser を使用することで指定できます。

既定のユーザーが利用できない場合、[XUserAddResult](/reference/system/xuser/functions/xuseraddresult) は E\_GAMEUSER\_NO\_DEFAULT\_USER を返します。*options* に XUserAddOptions::AddDefaultUserSilently を設定せずに [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) を呼び出す必要があります。

*options* に XUserAddOptions::AddDefaultUserAllowingUI を設定して [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) を繰り返し呼び出す場合にも、開発者が知っておくべきエッジ ケースがいくつかあります。これらは、サイレント UI の場合と非常に似ています (ただし同一ではありません):

* ゲームを起動した既定のユーザーが判明している状態でこれを繰り返し呼び出すと、同じユーザーが返されます。
* 以前に判明していた既定のユーザーがサインアウトしており、デバイスに 1 人のユーザーだけがサインインしている場合、そのユーザーが新しい「既定」のユーザーとしてマークされ、そのユーザーが返されます。
* 最初にゲームを起動したユーザーがサインアウトしており、ユーザーの数が 0 人または 2 人以上の場合、システムはユーザーを取得するために UI を表示し、そのユーザーを既定として設定します。

[XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) では、XUserAddOptions::AllowGuests を XUserAddOptions::AddDefaultUserSilently と一緒に使用することはできません。ゲストは既定のユーザーになれません。

現在のプラットフォームがゲストをサポートしていない場合でも、XUserAddOptions::AllowGuests を使用できます。

次の例では、ゲーム セッションにユーザーを非同期的に追加する方法を示します。

```cpp theme={null}
HRESULT AddUserComplete(XAsyncBlock* ab)
{
    unique_user_handle user;
    RETURN_IF_FAILED(XUserAddResult(ab, &user));

    XUserLocalId userLocalId;
    XUserGetLocalId(user.get(), &userLocalId);

    auto iter = std::find_if(
        _users.begin(),
        _users.end(),
        [&userLocalId](const User& candidate)
    {
        XUserLocalId candidateUserLocalId;
        XUserGetLocalId(candidate.Handle(), &candidateUserLocalId);
        return candidateUserLocalId == userLocalId;
    });

    // User already known
    if (iter != _users.end())
    {
        appLog.AddLog("User already in list\n");
        return S_OK;
    }

    try
    {
        _users.emplace_back(user.get());
        _users.back().LoadGamerPicAsync(_queue);
    }
    CATCH_RETURN();

    return S_OK;
}

HRESULT AddUser(bool allowGuests, bool silent)
{
    auto asyncBlock = std::make_unique<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = _queue;
    asyncBlock->context = this;
    asyncBlock->callback = [](XAsyncBlock* ab)
    {
        auto asyncBlock = std::unique_ptr<XAsyncBlock>(ab);
        LOG_IF_FAILED(static_cast<UserWindow*>(ab->context)->AddUserComplete(ab));
    };

    XUserAddOptions options = XUserAddOptions::None;

    if (allowGuests)
    {
        WI_SET_FLAG(options, XUserAddOptions::AllowGuests);
    }

    if (silent)
    {
        WI_SET_FLAG(options, XUserAddOptions::AddDefaultUserSilently);
    }

    if (SUCCEEDED_LOG(XUserAddAsync(
        options,
        asyncBlock.get())))
    {
        // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* since the callback will take over ownership.
        // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*
        asyncBlock.release();
    }

    return S_OK;
}
```

## 要件

**ヘッダー:** XUser.h

**サポートされているプラットフォーム:** Windows、XBOX One 本体、XBOX Series 本体

## 関連項目

[XUser](/reference/system/xuser/xuser_members)

[XUserAddAsync](/reference/system/xuser/functions/xuseraddasync)

[XUserAddResult](/reference/system/xuser/functions/xuseraddresult)


## Related topics

- [XUserAddResult](/ja-jp/reference/system/xuser/functions/xuseraddresult.md)
- [XUserAddAsync](/ja-jp/reference/system/xuser/functions/xuseraddasync.md)
- [XUser](/ja-jp/reference/system/xuser/xuser_members.md)
- [ゲスト ユーザーの概要](/ja-jp/build/core-features/common/user/users-guest-overview.md)
- [XUserPrivilegeOptions](/ja-jp/reference/system/xuser/enums/xuserprivilegeoptions.md)
