> ## 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](/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler)
[XblSocialRemoveSocialRelationshipChangedHandler](/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler)


## Related topics

- [用户系统（好友列表）示例代码](/zh-CN/services/xbox-services/community/people-system/how-to/live-pplsys-howto-nav.md)
- [XblSocialRemoveSocialRelationshipChangedHandler](/zh-CN/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler.md)
- [操作指南](/zh-CN/services/xbox-services/community/people-system/how-to/index.md)
- [XblSocialAddSocialRelationshipChangedHandler](/zh-CN/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler.md)
- [XblSocialRelationshipChangeEventArgs](/zh-CN/reference/live/xsapi-c/social_c/structs/xblsocialrelationshipchangeeventargs.md)
