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

Retrieves a handle to a user that [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) creates.

## Syntax

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

### Parameters

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

The async block sent to [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync).

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

Contains a handle to the new user.

### Return value

Type: HRESULT

HRESULT success or error code.

| Return Code                                 | Description                                                                                                                                                                                                            |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| S\_OK                                       | The operation succeeded.                                                                                                                                                                                               |
| E\_GAMEUSER\_NO\_DEFAULT\_USER              | A default user is not available. [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync) needs to be called without [XUserAddOptions::AddDefaultUserSilently](/reference/system/xuser/enums/xuseraddoptions). |
| E\_GAMEUSER\_RESOLVE\_USER\_ISSUE\_REQUIRED | The user must use a UI to resolve the issue. Call [XUserResolveIssueWithUiAsync](/reference/system/xuser/functions/xuserresolveissuewithuiasync) to display the UI to the user.                                        |
| E\_ABORT                                    | The user canceled the operation.                                                                                                                                                                                       |

## Remarks

**XUserAddAsync** starts an asynchronous operation to add a user to the game. Use
[XUserAddResult](/reference/system/xuser/functions/xuseraddresult) to retrieve the results of the operation.

**XUserAddAsync** always shows an account picker UI unless you pass
[XUserAddOptions::AddDefaultUserSilently](/reference/system/xuser/enums/xuseraddoptions)
to the *options* parameter.

If you use [XUserAddOptions::AddDefaultUserSilently](/reference/system/xuser/enums/xuseraddoptions), **XUserAddAsync** does not show
a UI. The user that **XUserAddOptions::AddDefaultUserSilently** returns continues to return until the user
signs out. If a default user is not available, [XUserAddResult](/reference/system/xuser/functions/xuseraddresult) returns
**E\_GAMEUSER\_NO\_DEFAULT\_USER**. This indicates that you must call  **XUserAddAsync** without
**XUserAddOptions::AddDefaultUserSilently** to select a user.

If a user is banned from XBOX Live, the game will not be able to get an XUserHandle for that user.
If **XUserAddOptions::AddDefaultUserSilently** is used and the game was launched by a banned user,
**XUserAddResult** will return E\_GAMEUSER\_NO\_DEFAULT\_USER. Otherwise, if UI is shown, either a non-banned
user will need to sign-in, or the user will need to cancel out of the UI and **XUserAddResult** will
return E\_ABORT.

You cannot use [XUserAddOptions::AllowGuests](/reference/system/xuser/enums/xuseraddoptions) with **XUserAddOptions::AddDefaultUserSilently**. A guest
cannot be the default user. You can use **XUserAddOptions::AllowGuests** safely regardless of whether the current platform
supports guests.

You must close each **XUserHandle** handle that you retrieve from an **XUsers** API once by calling
[XUserCloseHandle](/reference/system/xuser/functions/xuserclosehandle).

The following example demonstrates how to asynchronously add a user to a game session.

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

## Requirements

**Header:** XUser.h

**Library:** xgameruntime.lib

**Supported platforms:** Windows,Steam Deck, XBOX One family consoles and XBOX Series consoles

## Conceptual documentation

* [Run Microsoft Game Development Kit (GDK) API task](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task)
* [Asynchronous programming design goals and improvements](/build/core-features/common/async/async-whitepaper)

## See also

[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](/reference/system/xuser/enums/xuseraddoptions.md)
- [User identity and XUser](/build/core-features/common/user/player-identity-xuser.md)
- [Run Microsoft Game Development Kit API task example](/build/core-features/common/async/async-libraries/async-library-xasync-example-run-gdk-task.md)
- [XUserAddAsync](/reference/system/xuser/functions/xuseraddasync.md)
- [Initializing the GDK](/build/steam-porting-guide/initializing-the-gdk.md)
