You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Win32(Windows App SDK)应用中从std::vector创建Iterable<HSTRING>实例?

在Win32+Windows App SDK中创建ABI::Windows::Foundation::Collection::Iterable实例

在Win32结合Windows App SDK的场景下,ABI::Windows::Foundation::Collection::Iterable<HSTRING>通常由IVector<HSTRING>实例实现(因为IVector<T>继承自Iterable<T>)。以下是两种常见的实现方式:

方式一:使用C++/WinRT(推荐,Windows App SDK官方首选)

Windows App SDK对C++/WinRT有原生支持,C++/WinRT的容器类型可自动适配ABI层接口,无需手动处理COM对象生命周期。

代码示例

#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Services.Store.h>
#include <winrt/Windows.Foundation.h>

// 异步查询应用内购产品
winrt::Windows::Foundation::IAsyncAction QueryInAppProductsAsync()
{
    // 构建需要查询的产品ID列表
    winrt::vector<winrt::hstring> productIds;
    productIds.Append(L"your_product_id_1");
    productIds.Append(L"your_product_id_2");

    // 构建产品类型列表(例如Durable、Consumable)
    auto productTypes = winrt::single_threaded_vector<winrt::hstring>({ L"Durable", L"Consumable" });

    // 获取StoreContext并调用API,productIds和productTypes可直接作为Iterable<HSTRING>传入
    auto storeContext = winrt::Windows::Services::Store::StoreContext::GetDefault();
    auto queryResult = co_await storeContext.GetStoreProductsAsync(productTypes, productIds);

    // 处理查询结果
    if (queryResult.ExtendedError().Value != S_OK)
    {
        co_return;
    }

    auto products = queryResult.Products();
    for (auto const& product : products)
    {
        winrt::hstring productName = product.Value().Name();
        // 处理产品信息
    }
}

关键说明

  • winrt::vector<T>和winrt::single_threaded_vector<T>自动实现ABI::Windows::Foundation::Collection::IVector<T>接口,而IVVector继承自Iterable,可直接传递给需要Iterable的API。
  • C++/WinRT自动管理HSTRING和COM对象的生命周期,无需手动调用WindowsDeleteString或Release。

方式二:纯COM ABI实现(无C++/WinRT依赖)

若项目未使用C++/WinRT,可直接通过COM接口实例化Vector<HSTRING>对象,手动管理对象和字符串生命周期。

代码示例

#include <windows.foundation.h>
#include <windows.foundation.collections.h>
#include <windows.services.store.h>
#include <wrl/client.h>
#include <combaseapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::Services::Store;
using namespace ABI::Windows::Foundation;

// 创建包含产品ID的IVector<HSTRING>实例(即Iterable<HSTRING>)
ComPtr<IVector<HSTRING>> CreateProductIdVector()
{
    ComPtr<IVector<HSTRING>> vector;
    HRESULT hr = RoActivateInstance(
        HStringReference(RuntimeClass_Windows_Foundation_Collections_Vector).Get(),
        &vector
    );
    if (FAILED(hr))
    {
        return nullptr;
    }

    // 添加产品ID,手动管理HSTRING生命周期
    HSTRING productId1;
    if (SUCCEEDED(WindowsCreateString(L"your_product_id_1", wcslen(L"your_product_id_1"), &productId1)))
    {
        vector->Append(productId1);
        WindowsDeleteString(productId1);
    }

    HSTRING productId2;
    if (SUCCEEDED(WindowsCreateString(L"your_product_id_2", wcslen(L"your_product_id_2"), &productId2)))
    {
        vector->Append(productId2);
        WindowsDeleteString(productId2);
    }

    return vector;
}

// 调用Store API示例
void QueryInAppProducts()
{
    ComPtr<IStoreContext> storeContext;
    HRESULT hr = RoActivateInstance(
        HStringReference(RuntimeClass_Windows_Services_Store_StoreContext).Get(),
        &storeContext
    );
    if (FAILED(hr))
    {
        return;
    }

    // 创建产品ID和产品类型的Iterable实例
    ComPtr<IVector<HSTRING>> productIds = CreateProductIdVector();
    ComPtr<IVector<HSTRING>> productTypes;
    RoActivateInstance(
        HStringReference(RuntimeClass_Windows_Foundation_Collections_Vector).Get(),
        &productTypes
    );

    // 添加产品类型
    HSTRING durableType;
    if (SUCCEEDED(WindowsCreateString(L"Durable", wcslen(L"Durable"), &durableType)))
    {
        productTypes->Append(durableType);
        WindowsDeleteString(durableType);
    }

    // 调用异步API,需自行处理异步回调
    ComPtr<IAsyncOperation<StoreProductQueryResult*>> asyncOp;
    hr = storeContext->GetStoreProductsAsync(productTypes.Get(), productIds.Get(), &asyncOp);
    if (SUCCEEDED(hr))
    {
        // 设置异步完成回调(示例省略回调实现)
        // asyncOp->put_Completed(...);
    }
}

关键说明

  • 使用RoActivateInstance创建Windows.Foundation.Collections.Vector的COM实例,该实例实现IVector<HSTRING>接口,继承自Iterable<HSTRING>,符合API参数要求。
  • 必须手动管理HSTRING生命周期,使用WindowsCreateString创建后,需调用WindowsDeleteString释放。

内容的提问来源于stack exchange,提问作者JaSHin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 22:57:28