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

# XUserAddByIdWithUiAsync

> XUserAddByIdWithUiAsync

# XUserAddByIdWithUiAsync

ゲーム招待から取得した XUID に基づいて、ゲーム セッションにユーザーを非同期に追加します。この関数はゲーム招待への応答の場合にのみ使用してください。

## 構文

```cpp theme={null}
HRESULT XUserAddByIdWithUiAsync(  
         uint64_t userId,  
         XAsyncBlock* async  
)  
```

### パラメーター

*userId*   \_In\_\
Type: uint64\_t

追加するユーザーの XUID。この XUID は、[XGameInviteRegisterForEvent](/reference/system/xgameinvite/functions/xgameinviteregisterforevent) で登録されたゲームのコールバックで受信した inviteUri を解析することで取得する必要があります。

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

呼び出しの状態のポーリングおよび呼び出し結果の取得に使用する [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)。

### 戻り値

Type: HRESULT

HRESULT の成功またはエラー コード。
エラー コードの一覧については、[エラー コード](/reference/errorcodes) を参照してください。

## 解説

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

ゲーム招待を扱う際、本体では、ゲーム招待で指定された人物の XUID が、既にゲームにサインインしている人と一致しない可能性が常にあります。**XUserAddByIdWithUiAsync** を使用すると、ゲームは招待 URI から解析された XUID を使用してそのユーザーを追加できます。

この関数は、UI を表示せずにユーザーを追加しようとします。UI が表示される唯一の理由は、ユーザーがトークンの問題を解決する必要がある場合です。本体では、UI が表示されることはまれです。

ユーザーがデバイスにサインインしていない場合、**XUserAddByIdWithUiResult** は E\_GAMEUSER\_USER\_NOT\_FOUND を返します。

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

ゲーム招待を受け取るためには、開発者は [XGameInviteRegisterForEvent](/reference/system/xgameinvite/functions/xgameinviteregisterforevent) を呼び出して [XGameInviteCallback](/reference/system/xgameinvite/functions/xgameinviteeventcallback) を購読する必要があります。このコールバックが呼び出されたとき、ゲームは受信した inviteUri を解析し、その招待を受け取るユーザーの XUID を特定する必要があります。その時点で、ゲームは招待を受け取る人物の XUserHandle を取得することを検討する場合があります。次のコードでは、これがどのように機能するかを示します。

```cpp theme={null}
// Assumptions:
// 1. The game has previously registered for this callback using XGameInviteRegisterForEvent
// 2. The game has function: uint64_t GetXuidFromInviteUri(const char* inviteUri)
// 3. There is already a global task queue, g_GlobalQueue
 
void MyGameInviteEventCallback(void* context, const char* inviteUri)
{
   XAsyncBlock* asyncBlock = new XAsyncBlock();
   asyncBlock->queue = g_GlobalQueue;
   asyncBlock->callback = [](XAsyncBlock* ab)
   {
      XUserHandle userHandle;
      if(SUCCEEDED(XUserAddByIdWithUiResult(ab, &userHandle)))
      {
         // Copy this XUserHandle to some location that the game will use later or
         // Do whatever actions you want with this user

         XUserCloseHandle(userHandle);
      }
      else
      {
         // Since you didn't successfully add the user, setup your game state so that 
         // you are in a good place, likely a start menu
      }
      delete ab;
   }

   
   if(FAILED(XUserAddByIdWithUiAysnc(GetXuidFromInviteUri(inviteUri), asyncBlock)))
   {
      delete asyncBlock;
   }
}
```

## 要件

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

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

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

## 関連項目

[XUser](/reference/system/xuser/xuser_members)\
[XUserCloseHandle](/reference/system/xuser/functions/xuserclosehandle)


## Related topics

- [PFAuthenticationLoginWithXUserRequest](/ja-jp/services/playfab/api-references/c/pfauthenticationtypes/structs/pfauthenticationloginwithxuserrequest.md)
- [XUser](/ja-jp/reference/system/xuser/xuser_members.md)
- [GDK 向け Unity C# API ラッパー](/ja-jp/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [XUserAddByIdWithUiResult](/ja-jp/reference/system/xuser/functions/xuseraddbyidwithuiresult.md)
- [ユーザー ID と XUser](/ja-jp/build/core-features/common/user/player-identity-xuser.md)
