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

# 處理社交關係變更

> 用於訂閱 XBOX Live 社交關係變更事件，以及在使用者好友清單更新時新增或移除處理常式的 XSAPI 程式碼。

本主題提供處理社交關係變更的範例程式碼。

## 訂閱關係變更

**平面 C API**

```cpp theme={null}
HRESULT hr = XblSocialSubscribeToSocialRelationshipChange(
    xboxLiveContext, 
    xboxUserId,
    &state.socialSubscriptionHandle
);
```

## 取消訂閱關係變更

**平面 C API**

```cpp theme={null}
HRESULT hr = XblSocialUnsubscribeFromSocialRelationshipChange(
    xboxLiveContext, 
    state.socialSubscriptionHandle
);

state.socialSubscriptionHandle = nullptr;
```

## 新增關係變更處理常式

**平面 C API**

```cpp theme={null}
state.socialRelationshipChangedHandlerToken = XblSocialAddSocialRelationshipChangedHandler(
    xboxLiveContext,
    [](const XblSocialRelationshipChangeEventArgs* args, void* context)
    {
        UNREFERENCED_PARAMETER(context);
        LogToFile("Social relationship changed:");
        std::stringstream ss;
        for (size_t i = 0; i < args->xboxUserIdsCount; ++i)
        {
            if (i > 0) 
            {
                ss << ", ";
            }
            ss << args->xboxUserIds[i];
        }
        LogToFile("socialNotification = %u, affectedXuids = %s", args->socialNotification, ss.str().data());
    },
    nullptr
);
```

## 移除關係變更處理常式

**平面 C API**

```cpp theme={null}
HRESULT hr = XblSocialRemoveSocialRelationshipChangedHandler(xboxLiveContext, state.socialRelationshipChangedHandlerToken);
state.socialRelationshipChangedHandlerToken = 0;
```

## 另請參閱

[XblSocialAddSocialRelationshipChangedHandler](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler)
[XblSocialRemoveSocialRelationshipChangedHandler](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler)


## 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)
- [XblSocialRemoveSocialRelationshipChangedHandler](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler.md)
- [XblSocialAddSocialRelationshipChangedHandler](/zh-TW/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler.md)
- [XblSocialRelationshipChangeEventArgs](/zh-TW/reference/live/xsapi-c/social_c/structs/xblsocialrelationshipchangeeventargs.md)
