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

# 取得社交關係

> 使用 XblSocialGetSocialRelationshipsAsync 擷取 XBOX Live 使用者好友及其公開關係屬性的 XSAPI 平面 C 範例程式碼。

<Note>本主題示範進階 API 用法。如果您在社交管理員中發現不支援的案例，請告知您的開發人員客戶經理 (DAM)。</Note>

本主題提供程式碼範例，示範如何使用社交管理員 API 擷取使用者的社交關係及其公開屬性。

若要開始使用社交管理員，請參閱[社交管理員概觀](/zh-TW/services/xbox-services/community/social-manager/live-social-manager-overview)。
社交管理員 API 大幅簡化了追蹤線上好友及其遊戲活動的開發工作。

## 取得第一位使用者的社交關係

下列程式碼範例示範如何使用 XBOX 服務擷取社交關係。

此程式碼範例會執行下列動作：

1. 產生系統上所有使用者的清單，然後擷取第一位使用者。
2. 擷取該使用者的所有社交關係。
3. 顯示每個關係的公開屬性。

**平面 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 = XblSocialGetSocialRelationshipsResult(asyncBlock, &state.socialResultHandle);

    // Be sure to call XblSocialRelationshipResultCloseHandle when the result object is no longer needed.
};

HRESULT hr = XblSocialGetSocialRelationshipsAsync(
    xboxLiveContext,
    xboxUserId,
    socialRelationshipFilter,
    0,
    0,
    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](/zh-TW/reference/system/xasync/structs/xasyncblock)
* [XblSocialGetSocialRelationshipsAsync](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialgetsocialrelationshipsasync)
* [XblSocialGetSocialRelationshipsResult](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialgetsocialrelationshipsresult)
* [XblSocialRelationshipResultCloseHandle](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultclosehandle)

## 取得單一關係

**平面 C API**

```cpp theme={null}
const XblSocialRelationship* relationships = nullptr;
size_t relationshipsCount = 0;
HRESULT hr = XblSocialRelationshipResultGetRelationships(state.socialResultHandle, &relationships, &relationshipsCount);

LogToFile("Got %u SocialRelationships:", relationshipsCount);
for (size_t i = 0; i < relationshipsCount; ++i)
{
    LogToFile("Xuid = %u, isFollowingCaller = %u", relationships[i].xboxUserId, relationships[i].isFollowingCaller);
}
```

如需詳細資訊，請參閱下列內容：

* [XblSocialRelationship](/zh-TW/reference/live/xsapi-c/social_c/structs/xblsocialrelationship)
* [XblSocialRelationshipResultGetRelationships](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultgetrelationships)

## 取得下一頁的關係

**平面 C API**

```cpp theme={null}
bool hasNext{ false };
HRESULT hr = XblSocialRelationshipResultHasNext(state.socialResultHandle, &hasNext);

if (hasNext)
{
    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*.
        // Close the handle to the previous page of results.
        if (state.socialResultHandle)
        {
            XblSocialRelationshipResultCloseHandle(state.socialResultHandle);
        }
        HRESULT hr = XblSocialRelationshipResultGetNextResult(asyncBlock, &state.socialResultHandle);
    };

    uint32_t maxItems = 100;
    HRESULT hr = XblSocialRelationshipResultGetNextAsync(xboxLiveContext, state.socialResultHandle, maxItems, 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](/zh-TW/reference/system/xasync/structs/xasyncblock)
* [XblSocialRelationshipResultCloseHandle](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultclosehandle)
* [XblSocialRelationshipResultGetNextAsync](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultgetnextasync)
* [XblSocialRelationshipResultGetNextResult](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultgetnextresult)
* [XblSocialRelationshipResultHasNext](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresulthasnext)


## Related topics

- [人員系統 (好友清單) 範例程式碼](/zh-TW/services/xbox-services/community/people-system/how-to/live-pplsys-howto-nav.md)
- [操作說明](/zh-TW/services/xbox-services/community/people-system/how-to/index.md)
- [處理社交關係變更](/zh-TW/services/xbox-services/community/people-system/how-to/live-handling-a-relationship-change.md)
- [排行榜](/zh-TW/build/steam-porting-guide/features/steam-leaderboards.md)
- [社交管理員概觀](/zh-TW/services/xbox-services/community/social-manager/live-social-manager-overview.md)
