> ## 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.

# XUserAddResult

> XUserAddResult

# XUserAddResult

[XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) が作成したユーザーへのハンドルを取得します。

## 構文

```cpp theme={null}
HRESULT XUserAddResult(  
         XAsyncBlock* async,  
         XUserHandle* newUser  
)  
```

### パラメーター

*async*   \_Inout\_\
Type: [XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

[XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) に渡された非同期ブロックです。

*newUser*   \_Out\_\
Type: XUserHandle\*

新しいユーザーへのハンドルを格納します。

### 戻り値

Type: HRESULT

HRESULT の成功またはエラー コード。

| 戻りコード                                       | 説明                                                                                                                                                                                             |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| S\_OK                                       | 操作が成功しました。                                                                                                                                                                                     |
| E\_GAMEUSER\_NO\_DEFAULT\_USER              | 既定のユーザーが利用できません。[XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) を [XUserAddOptions::AddDefaultUserSilently](/reference/system/xuser/enums/xuseraddoptions) を指定せずに呼び出す必要があります。 |
| E\_GAMEUSER\_RESOLVE\_USER\_ISSUE\_REQUIRED | ユーザーは UI を使用して問題を解決する必要があります。[XUserResolveIssueWithUiAsync](/reference/system/xuser/functions/xuserresolveissuewithuiasync) を呼び出して、ユーザーに UI を表示します。                                            |
| E\_ABORT                                    | ユーザーが操作をキャンセルしました。                                                                                                                                                                             |

## 解説

**XUserAddAsync** は、ユーザーをゲームに追加するための非同期操作を開始します。操作の結果を取得するには、[XUserAddResult](/reference/system/xuser/functions/xuseraddresult) を使用します。

**XUserAddAsync** は、*options* パラメーターに [XUserAddOptions::AddDefaultUserSilently](/reference/system/xuser/enums/xuseraddoptions) を渡さない限り、常にアカウント ピッカー UI を表示します。

[XUserAddOptions::AddDefaultUserSilently](/reference/system/xuser/enums/xuseraddoptions) を使用する場合、**XUserAddAsync** は UI を表示しません。**XUserAddOptions::AddDefaultUserSilently** が返すユーザーは、そのユーザーがサインアウトするまで引き続き返されます。既定のユーザーが利用できない場合、[XUserAddResult](/reference/system/xuser/functions/xuseraddresult) は **E\_GAMEUSER\_NO\_DEFAULT\_USER** を返します。これは、ユーザーを選択するために **XUserAddOptions::AddDefaultUserSilently** を指定せずに **XUserAddAsync** を呼び出す必要があることを示します。

XBOX Live からユーザーが禁止 (BAN) されている場合、ゲームはそのユーザーの XUserHandle を取得できません。**XUserAddOptions::AddDefaultUserSilently** を使用し、ゲームが BAN されたユーザーによって起動された場合、**XUserAddResult** は E\_GAMEUSER\_NO\_DEFAULT\_USER を返します。それ以外の場合、UI が表示され、BAN されていないユーザーがサインインするか、ユーザーが UI をキャンセルする必要があり、**XUserAddResult** は E\_ABORT を返します。

[XUserAddOptions::AllowGuests](/reference/system/xuser/enums/xuseraddoptions) を **XUserAddOptions::AddDefaultUserSilently** と一緒に使用することはできません。ゲストは既定のユーザーになれません。現在のプラットフォームがゲストをサポートしているかどうかに関係なく、**XUserAddOptions::AllowGuests** を安全に使用できます。

**XUsers** API から取得したそれぞれの **XUserHandle** ハンドルは、[XUserCloseHandle](/reference/system/xuser/functions/xuserclosehandle) を呼び出して 1 回だけ閉じる必要があります。

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

```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

**ライブラリ:** xgameruntime.lib

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

## 概念ドキュメント

* [Microsoft Game Development Kit (GDK) API タスクを実行する](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task)
* [非同期プログラミングの設計目標と改善点](/build/core-features/common/async/async-whitepaper)

## 関連項目

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

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

[XUserAddOptions](/reference/system/xuser/enums/xuseraddoptions)

[XUserCloseHandle](/reference/system/xuser/functions/xuserclosehandle)


## Related topics

- [XUserAddOptions](/ja-jp/reference/system/xuser/enums/xuseraddoptions.md)
- [Microsoft Game Development Kit API タスクの実行例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
- [ユーザー ID と XUser](/ja-jp/build/core-features/common/user/player-identity-xuser.md)
- [XUserAddAsync](/ja-jp/reference/system/xuser/functions/xuseraddasync.md)
- [XUser](/ja-jp/reference/system/xuser/xuser_members.md)
