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

# ユーザーのプレゼンスの取得

> XblPresenceGetPresenceAsync を呼び出して XBOX Live ユーザーのプレゼンス レコードを取得し、返されたオンライン ステータス データを読み取る XSAPI フラット C の例です。

このトピックでは、ユーザーのプレゼンスを取得するサンプル コードを提供します。

## 単一のユーザーのプレゼンスの取得

#### フラット C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    HRESULT hr = XblPresenceGetPresenceResult(asyncBlock, &state.presenceRecord);

    // Be sure to call XblPresenceRecordCloseHandle when the presence record is no longer needed.
};

HRESULT hr = XblPresenceGetPresenceAsync(xboxLiveContext, xboxUserId, asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}
```

詳細については、以下を参照してください。

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblPresenceGetPresenceAsync](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceasync)
* [XblPresenceGetPresenceResult](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceresult)
* [XblPresenceRecordCloseHandle](/reference/live/xsapi-c/presence_c/functions/xblpresencerecordclosehandle)

## 複数のユーザーのプレゼンス文字列の取得

#### フラット C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    size_t resultCount{ 0 };
    HRESULT hr = XblPresenceGetPresenceForMultipleUsersResultCount(asyncBlock, &resultCount);

    if (SUCCEEDED(hr) && resultCount > 0)
    {
        std::vector<XblPresenceRecordHandle> handles(resultCount, nullptr);
        hr = XblPresenceGetPresenceForMultipleUsersResult(asyncBlock, handles.data(), resultCount);

        // Be sure to call XblPresenceRecordCloseHandle for each presence record when they're
        // no longer needed.
    }
};

std::vector<uint64_t> xuids{ 123, 456 };

// Filter results to only online users.
XblPresenceQueryFilters filters{};
filters.onlineOnly = true;

HRESULT hr = XblPresenceGetPresenceForMultipleUsersAsync(xboxLiveContext, xuids.data(), xuids.size(), &filters, asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}
```

詳細については、以下を参照してください。

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblPresenceGetPresenceForMultipleUsersAsync](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceformultipleusersasync)
* [XblPresenceGetPresenceForMultipleUsersResult](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceformultipleusersresult)
* [XblPresenceGetPresenceForMultipleUsersResultCount](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceformultipleusersresultcount)
* [XblPresenceQueryFilters](/reference/live/xsapi-c/presence_c/structs/xblpresencequeryfilters)
* [XblPresenceRecordCloseHandle](/reference/live/xsapi-c/presence_c/functions/xblpresencerecordclosehandle)

## ソーシャル グループからのユーザーのプレゼンスの取得

#### フラット C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    size_t resultCount{ 0 };
    HRESULT hr = XblPresenceGetPresenceForSocialGroupResultCount(asyncBlock, &resultCount);

    if (SUCCEEDED(hr) && resultCount > 0)
    {
        std::vector<XblPresenceRecordHandle> handles(resultCount, nullptr);
        hr = XblPresenceGetPresenceForSocialGroupResult(asyncBlock, handles.data(), resultCount);

        // Be sure to call XblPresenceRecordCloseHandle for each presence record when they're
        // no longer needed.
    }

};

HRESULT hr = XblPresenceGetPresenceForSocialGroupAsync(xboxLiveContext, "Favorites", nullptr, nullptr, asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}
```

詳細については、以下を参照してください。

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblPresenceGetPresenceForSocialGroupAsync](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceforsocialgroupasync)
* [XblPresenceGetPresenceForSocialGroupResult](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceforsocialgroupresult)
* [XblPresenceGetPresenceForSocialGroupResultCount](/reference/live/xsapi-c/presence_c/functions/xblpresencegetpresenceforsocialgroupresultcount)
* [XblPresenceRecordCloseHandle](/reference/live/xsapi-c/presence_c/functions/xblpresencerecordclosehandle)

## ユーザーのプレゼンスの表示

#### フラット C API

```cpp theme={null}
const XblPresenceDeviceRecord* deviceRecords{ nullptr };
size_t deviceRecordsCount{ 0 };
HRESULT hr = XblPresenceRecordGetDeviceRecords(state.presenceRecord, &deviceRecords, &deviceRecordsCount);

for (auto i = 0u; i < deviceRecordsCount; ++i)
{
    auto& deviceRecord{ deviceRecords[i] };
    LogToFile("Got XblDeviceRecord with device type %u and %u title records", deviceRecord.deviceType, deviceRecord.titleRecordsCount);

    for (auto j = 0u; j < deviceRecord.titleRecordsCount; ++j)
    {
        auto& titleRecord{ deviceRecord.titleRecords[j] };
        // Display the Rich Presence string.
        LogToFile("Rich presence string for titleId %u: %s", titleRecord.titleId, titleRecord.richPresenceString);
    }
}
```

詳細については、以下を参照してください。

* [XblPresenceDeviceRecord](/reference/live/xsapi-c/presence_c/structs/xblpresencedevicerecord)
* [XblPresenceRecordGetDeviceRecords](/reference/live/xsapi-c/presence_c/functions/xblpresencerecordgetdevicerecords)


## Related topics

- [リッチ プレゼンスのサンプル コード](/ja-jp/services/xbox-services/community/presence/how-to/live-presence-howto-nav.md)
- [ハウツー](/ja-jp/services/xbox-services/community/presence/how-to/index.md)
- [XSAPI C を介したユーザーのプレゼンスの更新](/ja-jp/services/xbox-services/community/presence/how-to/live-updating-user-presence.md)
- [REST を介したユーザーのプレゼンスの更新](/ja-jp/services/xbox-services/community/presence/how-to/live-updating-presence-rest.md)
- [XblPresenceStopTrackingUsers](/ja-jp/reference/live/xsapi-c/presence_c/functions/xblpresencestoptrackingusers.md)
