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

# XStoreGetUserPurchaseIdAsync

> XStoreGetUserPurchaseIdAsync

# XStoreGetUserPurchaseIdAsync

获取客户购买 ID。此 ID 检索一个 Microsoft Store ID 密钥，该密钥可用于代表当前用户授予免费产品的权益。此 API 用于[通过你的服务管理产品](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/service-to-service/service-to-service-nav)中概述的流程

## 语法

```cpp theme={null}
HRESULT XStoreGetUserPurchaseIdAsync(  
         const XStoreContextHandle storeContextHandle,  
         const char* serviceTicket,  
         const char* publisherUserId,  
         XAsyncBlock* async  
)  
```

### 参数

*storeContextHandle*   \_In\_\
类型：XStoreContextHandle

由 [XStoreCreateContext](/reference/system/xstore/functions/xstorecreatecontext) 返回的用户的应用商店上下文句柄。

*serviceTicket*   \_In\_z\_\
类型：char\*

与购买关联的服务票证。

*publisherUserId*   \_In\_z\_\
类型：char\*

包含发行商用户 ID 的字符串。

*async*   \_Inout\_\
类型：[XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

定义正在执行的异步工作的 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)。可使用 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock) 轮询调用状态并检索调用结果。有关详细信息，请参阅 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)。

### 返回值

类型：HRESULT

HRESULT 成功或错误代码。

## 备注

要检索客户的购买 ID 以及此函数的执行结果，请在调用此函数后调用 [XStoreGetUserPurchaseIdResult](/reference/system/xstore/functions/xstoregetuserpurchaseidresult)。要检索客户购买 ID 的大小，请在调用此函数后调用 [XStoreGetUSerPurchaseIdResultSize](/reference/system/xstore/functions/xstoregetuserpurchaseidresultsize)。知道结果大小将有助于你更高效地读取结果。

以下代码片段演示了检索客户购买 ID 的示例。

```cpp theme={null}
void CALLBACK GetUserPurchaseIdCallback(XAsyncBlock* asyncBlock)
{
    size_t size;
    HRESULT hr = XStoreGetUserPurchaseIdResultSize(
        asyncBlock,
        &size);

    if (FAILED(hr))
    {
        printf("Failed retrieve the user purchase ID size: 0x%x\r\n", hr);
        return;
    }

    char* result = new char[size];
    hr = XStoreGetUserPurchaseIdResult(
        asyncBlock,
        size,
        result);

    if (FAILED(hr))
    {
        printf("Failed retrieve the user purchase ID result: 0x%x\r\n", hr);
        delete[] result;
        return;
    }

    printf("result: %s\r\n", result);

    delete[] result;
}

void GetUserPurchaseId(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle, const char* serviceTicket, const char* publisherUserId)
{
    auto asyncBlock = std::make_unique<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = taskQueueHandle;

    asyncBlock->callback = GetUserPurchaseIdCallback;

    HRESULT hr = XStoreGetUserPurchaseIdAsync(
        storeContextHandle,
        serviceTicket,
        publisherUserId,
        asyncBlock.get());

    if (FAILED(hr))
    {
        printf("Failed to get user purchase ID: 0x%x\r\n", hr);
        return;
    }
}
```

## 要求

**头文件：** XStore.h（包含于 XGameRuntime.h 中）

**库：** xgameruntime.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 概念文档

* [请求用于服务到服务身份验证的 User Store ID](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/service-to-service/xstore-requesting-a-userstoreid)

## 另请参阅

[XStore](/reference/system/xstore/xstore_members)\
[XStoreGetUserPurchaseIdResult](/reference/system/xstore/functions/xstoregetuserpurchaseidresult)\
[XStoreGetUSerPurchaseIdResultSize](/reference/system/xstore/functions/xstoregetuserpurchaseidresultsize)\
[通过你的服务管理产品](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/service-to-service/service-to-service-nav)


## Related topics

- [XStoreGetUserPurchaseIdResult](/zh-CN/reference/system/xstore/functions/xstoregetuserpurchaseidresult.md)
- [XStoreGetUserPurchaseIdResultSize](/zh-CN/reference/system/xstore/functions/xstoregetuserpurchaseidresultsize.md)
- [为服务到服务身份验证请求 User Store ID](/zh-CN/publishing/xstore-commerce/xstore-requesting-userstoreid.md)
- [XStore 商务概述](/zh-CN/publishing/xstore-commerce/xstore-commerce-overview.md)
- [XStore](/zh-CN/reference/system/xstore/xstore_members.md)
