gpt4 book ai didi

c++ - 在 C++ 中如何访问从 COM 对象返回的 VARIANT 数据类型中的 SAFE ARRAY?

转载 作者:行者123 更新时间:2023-11-30 03:22:29 24 4
gpt4 key购买 nike

我正在使用 COM 对象。我调用 COM 对象的函数,此函数返回 VARIANT 数据类型,其中包含我的设备列表的安全数组。我如何使用此 VARIANT 访问我的设备的 SAFEARRY。

  VARIANT namList; 
SAFEARRAY* myequip;
namList=str->GetNames();

最佳答案

为了像这样在 VARIANT 中使用 SAFEARRAY,您需要进行大量验证和错误检查。这是您需要遵循的粗略模式。请仔细阅读评论,因为我对您正在使用的 COM API 做了一些假设。

// verify that it's an array
if (V_ISARRAY(&namList))
{
// get safe array
LPSAFEARRAY pSafeArray = V_ARRAY(&namList);

// determine the type of item in the array
VARTYPE itemType;
if (SUCCEEDED(SafeArrayGetVartype(pSafeArray, &itemType)))
{
// verify it's the type you expect
// (The API you're using probably returns a safearray of VARIANTs,
// so I'll use VT_VARIANT here. You should double-check this.)
if (itemType == VT_VARIANT)
{
// verify that it's a one-dimensional array
// (The API you're using probably returns a one-dimensional array.)
if (SafeArrayGetDim(pSafeArray) == 1)
{
// determine the upper and lower bounds of the first dimension
LONG lBound;
LONG uBound;
if (SUCCEEDED(SafeArrayGetLBound(pSafeArray, 1, &lBound)) && SUCCEEDED(SafeArrayGetUBound(pSafeArray, 1, &uBound)))
{
// determine the number of items in the array
LONG itemCount = uBound - lBound + 1;

// begin accessing data
LPVOID pData;
if (SUCCEEDED(SafeArrayAccessData(pSafeArray, &pData)))
{
// here you can cast pData to an array (pointer) of the type you expect
// (The API you're using probably returns a safearray of VARIANTs,
// so I'll use VARIANT here. You should double-check this.)
VARIANT* pItems = (VARIANT*)pData;

// use the data here.

// end accessing data
SafeArrayUnaccessData(pSafeArray);
}
}
}
}
}
}

关于c++ - 在 C++ 中如何访问从 COM 对象返回的 VARIANT 数据类型中的 SAFE ARRAY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51108054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com