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

# XUserCloseHandle

> XUserCloseHandle

# XUserCloseHandle

關閉特定的使用者控制代碼。

## 語法

```cpp theme={null}
void XUserCloseHandle(  
         XUserHandle user  
)  
```

### 參數

*user*   \_In\_\
類型：XUserHandle

要關閉的使用者控制代碼。

### 傳回值

類型：void

## 備註

對於從 **XUser** API 擷取的每個 **XUserHandle**，您都必須呼叫 XUserCloseHandle 函式一次。
您也可以呼叫 XUserCloseHandle 函式來關閉代表該使用者的所有控制代碼。

下列範例示範如何以非同步方式將使用者新增至遊戲工作階段。

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

## 概念文件

* [時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

[XUser](/zh-TW/reference/system/xuser/xuser_members)

[XUserCloseSignOutDeferralHandle](/zh-TW/reference/system/xuser/functions/xuserclosesignoutdeferralhandle)

[XUserCompare](/zh-TW/reference/system/xuser/functions/xusercompare)


## Related topics

- [XUserDuplicateHandle](/zh-TW/reference/system/xuser/functions/xuserduplicatehandle.md)
- [XUserCompare](/zh-TW/reference/system/xuser/functions/xusercompare.md)
- [XUserAddAsync](/zh-TW/reference/system/xuser/functions/xuseraddasync.md)
- [XUserAddByIdWithUiAsync](/zh-TW/reference/system/xuser/functions/xuseraddbyidwithuiasync.md)
- [XUserAddByIdWithUiResult](/zh-TW/reference/system/xuser/functions/xuseraddbyidwithuiresult.md)
