将 CComSafeArray 传递给需要 SAFEARRAY 的方法时,正确的语法是什么**
这很可能有一个非常简单的答案,但我无法弄清楚。
我正在尝试重构一些如下所示的代码:
SAFEARRAY* psa;
long* count;
HRESULT hr = pSomeInterface->ListSomething(&psa, &count);
if (SUCCEEDED(hr))
{
CComSafeArray<BSTR> sa;
if (*count > 0)
{
sa.Attach(psa);
}
}
// perform operations on sa
// allow CComSafeArray to destroy the object
return hr;
我想将代码更改为以下内容:
CComSafeArray<BSTR> sa;
long* count;
hr = pSomeInterface->ListSomething(&(sa.m_psa), &count);
if (SUCCEEDED(hr))
{
// perform operations on sa
}
但是当我执行此操作时,sa 包含垃圾。 发生了什么事以及为什么? 正确的语法是什么?
This most likely has a very simple answer, but I can't figure it out.
I'm trying to refactor some code that looks like this:
SAFEARRAY* psa;
long* count;
HRESULT hr = pSomeInterface->ListSomething(&psa, &count);
if (SUCCEEDED(hr))
{
CComSafeArray<BSTR> sa;
if (*count > 0)
{
sa.Attach(psa);
}
}
// perform operations on sa
// allow CComSafeArray to destroy the object
return hr;
I would like to change the code to something like:
CComSafeArray<BSTR> sa;
long* count;
hr = pSomeInterface->ListSomething(&(sa.m_psa), &count);
if (SUCCEEDED(hr))
{
// perform operations on sa
}
But when I execute this, sa contains garbage. What's happening and why? What is the correct syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过有效地绕过 CComSafeArray 并直接访问其内部 SAFEARRAY,您就破坏了使用 CComSafeArray 的全部意义。 CComSafeArray 定义了显式 LPSAFEARRAY 运算符,因此您应该能够执行此操作:
You're defeating the whole point of using CComSafeArray by effectively bypassing it and accessing its internal SAFEARRAY directly. CComSafeArray has an explicit LPSAFEARRAY operator defined, so you should be able to do this instead:
我在您的代码中没有看到任何此类问题。 如果您可以分享 ListSomething(..) 方法的代码,那么我们也许能够找到一些东西,但是像这样的类似代码非常适合我。
I don't see any such problem in your code. If you can share the code of ListSomething(..) method, then we might be able to find something but a similar code like this works perfectly with me.
您应该使用
CComSafeArray::
GetSafeArrayPtr()
。 但是 阿米尔 使用&(sa.m_psa)
的方式也应该有效。You should use
CComSafeArray<T>::
GetSafeArrayPtr()
. However Aamir's way of using&(sa.m_psa)
should work too.